|
@@ -1,8 +1,11 @@
|
|
|
-package com.abi.task.common.utils;
|
|
|
+package com.abi.task.common.tablestore;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import com.abi.task.common.api.exception.BusinessException;
|
|
|
import com.abi.task.common.api.exception.ErrorCodeEnum;
|
|
|
+import com.abi.task.common.tablestore.common.TableStoreReq;
|
|
|
+import com.abi.task.common.tablestore.common.TableStoreRes;
|
|
|
import com.alicloud.openservices.tablestore.SyncClient;
|
|
|
import com.alicloud.openservices.tablestore.model.BatchWriteRowRequest;
|
|
|
import com.alicloud.openservices.tablestore.model.BatchWriteRowResponse;
|
|
@@ -29,10 +32,22 @@ import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;
|
|
|
import com.alicloud.openservices.tablestore.model.TableMeta;
|
|
|
import com.alicloud.openservices.tablestore.model.TableOptions;
|
|
|
import com.alicloud.openservices.tablestore.model.UpdateRowRequest;
|
|
|
+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.RangeQuery;
|
|
|
+import com.alicloud.openservices.tablestore.model.search.query.TermsQuery;
|
|
|
+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;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@@ -152,30 +167,6 @@ public class TableStoreUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * 构造RowPutChange
|
|
|
- *
|
|
|
- * @param primaryKeyName
|
|
|
- * @param pkValue
|
|
|
- * @param tableName
|
|
|
- * @param columns
|
|
|
- * @return
|
|
|
- */
|
|
|
- private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
|
|
|
-
|
|
|
- PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
-
|
|
|
- primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
- PrimaryKey primaryKey = primaryKeyBuilder.build();
|
|
|
-
|
|
|
- RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
|
|
|
-
|
|
|
-
|
|
|
- rowPutChange.addColumns(columns);
|
|
|
-
|
|
|
- return rowPutChange;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
* 单行读取一行数据
|
|
|
*
|
|
@@ -202,7 +193,6 @@ public class TableStoreUtils {
|
|
|
* @return 一行某一列的数据
|
|
|
*/
|
|
|
public Row getColumnRow(String primaryKeyName, String pkValue, String tableName, String columnName) {
|
|
|
-
|
|
|
SingleRowQueryCriteria criteria = queryCriteria(primaryKeyName, pkValue, tableName);
|
|
|
|
|
|
|
|
@@ -265,6 +255,112 @@ public class TableStoreUtils {
|
|
|
client().deleteRow(new DeleteRowRequest(rowDeleteChange));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 通过聚集索引分页查询
|
|
|
+ * @param req
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public TableStoreRes listRows(TableStoreReq req){
|
|
|
+
|
|
|
+ if(StringUtils.isBlank(req.getTableName())){
|
|
|
+ throw new BusinessException("TableName为空");
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(req.getIndexName())){
|
|
|
+ throw new BusinessException("IndexName为空");
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(req.getFieldName())){
|
|
|
+ throw new BusinessException("FieldName为空");
|
|
|
+ }
|
|
|
+ if(req.getFieldValue()==null
|
|
|
+ &&(req.getLessThan()==null || req.getGreaterThan()==null)){
|
|
|
+ throw new BusinessException("查询条件为空");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ SearchQuery searchQuery = new SearchQuery();
|
|
|
+
|
|
|
+
|
|
|
+ if(req.getFieldValue()!=null){
|
|
|
+ TermsQuery termsQuery = new TermsQuery();
|
|
|
+
|
|
|
+ termsQuery.setFieldName(req.getFieldName());
|
|
|
+
|
|
|
+ termsQuery.addTerm(toColumnValue(req.getFieldValue()));
|
|
|
+ searchQuery.setQuery(termsQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(req.getLessThan()!=null && req.getGreaterThan()!=null){
|
|
|
+ RangeQuery rangeQuery = new RangeQuery();
|
|
|
+
|
|
|
+ rangeQuery.setFieldName(req.getFieldName());
|
|
|
+
|
|
|
+ rangeQuery.greaterThan(toColumnValue(req.getGreaterThan()));
|
|
|
+ rangeQuery.lessThan(toColumnValue(req.getLessThan()));
|
|
|
+ searchQuery.setQuery(rangeQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(req.getSortByFieldDesc()){
|
|
|
+ FieldSort fieldSort = new FieldSort(req.getFieldName());
|
|
|
+ fieldSort.setOrder(SortOrder.DESC);
|
|
|
+ searchQuery.setSort(new Sort(Arrays.asList((Sort.Sorter)fieldSort)));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int offset = (req.getPageNo()-1)*req.getPageCount();
|
|
|
+ searchQuery.setLimit(req.getPageCount());
|
|
|
+ searchQuery.setOffset(offset);
|
|
|
+
|
|
|
+
|
|
|
+ searchQuery.setGetTotalCount(req.getGetTotalCount());
|
|
|
+
|
|
|
+ SearchRequest searchRequest = new SearchRequest(req.getTableName(), req.getIndexName(), searchQuery);
|
|
|
+
|
|
|
+ SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
|
|
|
+
|
|
|
+ columnsToGet.setReturnAll(true);
|
|
|
+ searchRequest.setColumnsToGet(columnsToGet);
|
|
|
+
|
|
|
+ SearchResponse resp = client().search(searchRequest);
|
|
|
+
|
|
|
+
|
|
|
+ TableStoreRes res = new TableStoreRes();
|
|
|
+ res.setTotalCount(resp.getTotalCount());
|
|
|
+ res.setRowList(resp.getRows());
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private ColumnValue toColumnValue(Object fieldValue){
|
|
|
+ ColumnValue columnValue = null;
|
|
|
+ if (fieldValue instanceof String) {
|
|
|
+ columnValue = ColumnValue.fromString(String.valueOf(fieldValue));
|
|
|
+ } else if (fieldValue instanceof Integer) {
|
|
|
+ columnValue = ColumnValue.fromLong(Long.valueOf((Integer) fieldValue));
|
|
|
+ } else if (fieldValue instanceof Long) {
|
|
|
+ columnValue = ColumnValue.fromLong((Long) fieldValue);
|
|
|
+ } else if (fieldValue instanceof Double) {
|
|
|
+ columnValue = ColumnValue.fromDouble((Double) fieldValue);
|
|
|
+ } else if (fieldValue instanceof Boolean) {
|
|
|
+ columnValue = ColumnValue.fromBoolean((Boolean) fieldValue);
|
|
|
+ } else if (fieldValue instanceof Date) {
|
|
|
+ columnValue = ColumnValue.fromString(DateUtil.format((Date) fieldValue, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ } else if (fieldValue instanceof LocalDateTime) {
|
|
|
+ columnValue = ColumnValue.fromString(DateUtil.format((LocalDateTime) fieldValue, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("不识别的字段类型fieldValue");
|
|
|
+ }
|
|
|
+
|
|
|
+ return columnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private SingleRowQueryCriteria queryCriteria(String primaryKeyName, String pkValue, String tableName) {
|
|
|
|
|
|
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
@@ -287,5 +383,28 @@ public class TableStoreUtils {
|
|
|
return new RowUpdateChange(tableName, primaryKey);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 构造RowPutChange
|
|
|
+ *
|
|
|
+ * @param primaryKeyName
|
|
|
+ * @param pkValue
|
|
|
+ * @param tableName
|
|
|
+ * @param columns
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
|
|
|
+
|
|
|
+ PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
+
|
|
|
+ primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
+ PrimaryKey primaryKey = primaryKeyBuilder.build();
|
|
|
+
|
|
|
+ RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
|
|
|
+
|
|
|
+
|
|
|
+ rowPutChange.addColumns(columns);
|
|
|
+
|
|
|
+ return rowPutChange;
|
|
|
+ }
|
|
|
|
|
|
}
|