使用阿里云tablestore查询数据
作者:互联网
最近公司使用了阿里的nosql产品,主要记录车辆gps上传坐标位置,使用过程中遇到几个疑难,最终解决。
客户端连接的创建:
@Configuration
@ConfigurationProperties
@PropertySource(value = {
"classpath:application.properties" }, ignoreResourceNotFound = false, encoding = "UTF-8", name = "application.properties")
public class GlobalConfig {
@Value("${accessId}")
private String accessId;
@Value("${accessKey}")
private String accessKey;
@Value("${endPoint}")
private String endPoint;
@Value("${instanceName}")
private String instanceName;
@Bean
public SyncClient syncClient() {
return new SyncClient(endPoint, accessId, accessKey, instanceName);
}
}
需要自己创建客户端连接工具,用的时候直接依赖就可以了。
简单的例子
package com.huoda.tms.vehicleLocation.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.Row;
import com.alicloud.openservices.tablestore.model.search.SearchQuery;
import com.alicloud.openservices.tablestore.model.search.SearchRequest;
import com.alicloud.openservices.tablestore.model.search.SearchResponse;
import com.alicloud.openservices.tablestore.model.search.query.BoolQuery;
import com.alicloud.openservices.tablestore.model.search.query.MatchQuery;
import com.alicloud.openservices.tablestore.model.search.query.Query;
import com.alicloud.openservices.tablestore.model.search.query.RangeQuery;
import com.alicloud.openservices.tablestore.model.search.query.TermQuery;
import com.alicloud.openservices.tablestore.model.search.sort.FieldSort;
import com.alicloud.openservices.tablestore.model.search.sort.Sort;
import com.alicloud.openservices.tablestore.model.search.sort.SortOrder;
@Service
public class VehicleLocationService {
@Value("${tableName}")
private String tableName;
@Autowired
private SyncClient syncClient;
public long total(String indexName) {
// TODO Auto-generated method stub
return 0;
}
/**
* 查询单条记录
*
* **/
public Row query(String indexName, Map<String, String> map) {
Row result=null;
if (map.isEmpty()) {
return null;
}
SearchQuery searchQuery = new SearchQuery();
// 设置查询类型为TermQuery
TermQuery termQuery = new TermQuery();
for (Map.Entry<String, String> entry : map.entrySet()) {
// 设置要匹配的字段
termQuery.setFieldName(entry.getKey());
// 设置要匹配的值
termQuery.setTerm(ColumnValue.fromString(entry.getValue()));
}
searchQuery.setQuery(termQuery);
searchQuery.setSort(new Sort(Arrays.asList(new FieldSort("date_time", SortOrder.DESC))));
SearchRequest searchRequest = new SearchRequest(tableName, indexName, searchQuery);
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
// 设置返回所有列
columnsToGet.setReturnAll(true);
searchRequest.setColumnsToGet(columnsToGet);
SearchResponse searchResponse = syncClient.search(searchRequest);
if(searchResponse!=null) {
result=searchResponse.getRows().get(0);
}
return result;
}
public SearchResponse matchAndRangeQuery(String indexName,String gpsSerial,RangeQuery rangeQuery) {
SearchQuery searchQuery = new SearchQuery();
BoolQuery boolQuery = new BoolQuery();
MatchQuery matchQuery = new MatchQuery(); // 设置查询类型为MatchQuery
matchQuery.setFieldName("gps_serial"); // 设置要匹配的字段
matchQuery.setText(gpsSerial); // 设置要匹配的值
List<Query> querys=new ArrayList<Query>();
querys.add(matchQuery);
if(rangeQuery!=null) {
querys.add(rangeQuery);
}
boolQuery.setMustQueries(querys);
searchQuery.setQuery(boolQuery);
searchQuery.setGetTotalCount(true);
SearchRequest searchRequest = new SearchRequest(tableName, indexName, searchQuery);
// 设置返回所有列
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
searchRequest.setColumnsToGet(columnsToGet);
SearchResponse searchResponse = syncClient.search(searchRequest);
return searchResponse;
}
}
查询多条记录时候,需要记录位置
标签:tablestore,查询,阿里,openservices,new,import,com,alicloud 来源: https://blog.csdn.net/dushanlitian/article/details/93738732