|
@@ -0,0 +1,234 @@
|
|
|
+package com.abi.task.common.utils;
|
|
|
+
|
|
|
+import com.alicloud.openservices.tablestore.SyncClient;
|
|
|
+import com.alicloud.openservices.tablestore.model.CapacityUnit;
|
|
|
+import com.alicloud.openservices.tablestore.model.Column;
|
|
|
+import com.alicloud.openservices.tablestore.model.ColumnValue;
|
|
|
+import com.alicloud.openservices.tablestore.model.CreateTableRequest;
|
|
|
+import com.alicloud.openservices.tablestore.model.DeleteRowRequest;
|
|
|
+import com.alicloud.openservices.tablestore.model.GetRowRequest;
|
|
|
+import com.alicloud.openservices.tablestore.model.GetRowResponse;
|
|
|
+import com.alicloud.openservices.tablestore.model.ListTableResponse;
|
|
|
+import com.alicloud.openservices.tablestore.model.PrimaryKey;
|
|
|
+import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
|
|
|
+import com.alicloud.openservices.tablestore.model.PrimaryKeySchema;
|
|
|
+import com.alicloud.openservices.tablestore.model.PrimaryKeyType;
|
|
|
+import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
|
|
|
+import com.alicloud.openservices.tablestore.model.PutRowRequest;
|
|
|
+import com.alicloud.openservices.tablestore.model.ReservedThroughput;
|
|
|
+import com.alicloud.openservices.tablestore.model.Row;
|
|
|
+import com.alicloud.openservices.tablestore.model.RowDeleteChange;
|
|
|
+import com.alicloud.openservices.tablestore.model.RowPutChange;
|
|
|
+import com.alicloud.openservices.tablestore.model.RowUpdateChange;
|
|
|
+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 lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: fangxinjian
|
|
|
+ * @date: 2021/04/15 10:17
|
|
|
+ * @description: 表格存储工具类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class TableStoreUtils {
|
|
|
+
|
|
|
+ @Value("${tableStore.endPoint}")
|
|
|
+ private String endPoint;
|
|
|
+
|
|
|
+ @Value("${tableStore.accessKeyId}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${tableStore.accessKeySecret}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${tableStore.instanceName}")
|
|
|
+ private String instanceName;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建SyncClient
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public SyncClient client() {
|
|
|
+ return new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建数据表
|
|
|
+ *
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ */
|
|
|
+ public void createTable(String tableName, String primaryKeyName) {
|
|
|
+
|
|
|
+ TableMeta tableMeta = new TableMeta(tableName);
|
|
|
+ //为主表添加主键列
|
|
|
+ tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(primaryKeyName, PrimaryKeyType.STRING));
|
|
|
+ //数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
|
|
|
+ int timeToLive = -1;
|
|
|
+ //保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
|
|
|
+ int maxVersions = 3;
|
|
|
+ TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
|
|
|
+ CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
|
|
|
+ //设置预留读写吞吐量,容量型实例中的数据表只能设置为0,高性能实例中的数据表可以设置为非零值。
|
|
|
+ request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0)));
|
|
|
+ SyncClient client = client();
|
|
|
+ client.createTable(request);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询所有表的表名
|
|
|
+ */
|
|
|
+ public List<String> listTable() {
|
|
|
+ ListTableResponse listTableResponse = client().listTable();
|
|
|
+ return listTableResponse.getTableNames();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单行插入一行数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名
|
|
|
+// * @param columnName 列名称
|
|
|
+// * @param columnValue 列值
|
|
|
+ */
|
|
|
+ public void putRow(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.addColumn(new Column(columnName, ColumnValue.fromString(columnValue)));
|
|
|
+// rowPutChange.addColumn(columnName,ColumnValue.fromString(columnValue));
|
|
|
+ rowPutChange.addColumns(columns);
|
|
|
+
|
|
|
+ client().putRow(new PutRowRequest(rowPutChange));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单行读取一行数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名
|
|
|
+ * @return 一行数据
|
|
|
+ */
|
|
|
+ public Row getRow(String primaryKeyName, String pkValue, String tableName) {
|
|
|
+
|
|
|
+ SingleRowQueryCriteria criteria = queryCriteria(primaryKeyName, pkValue, tableName);
|
|
|
+
|
|
|
+ GetRowResponse getRowResponse = client().getRow(new GetRowRequest(criteria));
|
|
|
+ return getRowResponse.getRow();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单行读取一行某一列的数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param columnName 列名称
|
|
|
+ * @return 一行某一列的数据
|
|
|
+ */
|
|
|
+ public Row getColumnRow(String primaryKeyName, String pkValue, String tableName, String columnName) {
|
|
|
+
|
|
|
+ SingleRowQueryCriteria criteria = queryCriteria(primaryKeyName, pkValue, tableName);
|
|
|
+
|
|
|
+ //设置读取某些列。
|
|
|
+ criteria.addColumnsToGet(columnName);
|
|
|
+ GetRowResponse getRowResponse = client().getRow(new GetRowRequest(criteria));
|
|
|
+ return getRowResponse.getRow();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新一行数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名称
|
|
|
+ * @param columnName 列名称
|
|
|
+ * @param columnValue 列值
|
|
|
+ */
|
|
|
+ public void updateRow(String primaryKeyName, String pkValue, String tableName, String columnName, String columnValue) {
|
|
|
+
|
|
|
+ RowUpdateChange rowUpdateChange = updateChange(primaryKeyName, pkValue, tableName);
|
|
|
+ //更新列数据
|
|
|
+ rowUpdateChange.put(new Column(columnName, ColumnValue.fromString(columnValue)));
|
|
|
+
|
|
|
+ client().updateRow(new UpdateRowRequest(rowUpdateChange));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除某列数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名称
|
|
|
+ * @param columnName 列名称
|
|
|
+ */
|
|
|
+ public void deleteColumn(String primaryKeyName, String pkValue, String tableName, String columnName) {
|
|
|
+
|
|
|
+ RowUpdateChange rowUpdateChange = updateChange(primaryKeyName, pkValue, tableName);
|
|
|
+ //删除某一列。
|
|
|
+ rowUpdateChange.deleteColumns(columnName);
|
|
|
+
|
|
|
+ client().updateRow(new UpdateRowRequest(rowUpdateChange));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除一行数据
|
|
|
+ *
|
|
|
+ * @param primaryKeyName 主键名称
|
|
|
+ * @param pkValue 主键值
|
|
|
+ * @param tableName 表名称
|
|
|
+ */
|
|
|
+ public void deleteRow(String primaryKeyName, String pkValue, String tableName) {
|
|
|
+ //构造主键。
|
|
|
+ PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
+ primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
+ PrimaryKey primaryKey = primaryKeyBuilder.build();
|
|
|
+ //设置数据表名称。
|
|
|
+ RowDeleteChange rowDeleteChange = new RowDeleteChange(tableName, primaryKey);
|
|
|
+
|
|
|
+ client().deleteRow(new DeleteRowRequest(rowDeleteChange));
|
|
|
+ }
|
|
|
+
|
|
|
+ private SingleRowQueryCriteria queryCriteria(String primaryKeyName, String pkValue, String tableName) {
|
|
|
+ //构造主键。
|
|
|
+ PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
+ primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
+ PrimaryKey primaryKey = primaryKeyBuilder.build();
|
|
|
+
|
|
|
+ //读取一行数据,设置数据表名称。
|
|
|
+ SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName, primaryKey);
|
|
|
+ //设置读取最新版本。
|
|
|
+ criteria.setMaxVersions(1);
|
|
|
+ return criteria;
|
|
|
+ }
|
|
|
+
|
|
|
+ public RowUpdateChange updateChange(String primaryKeyName, String pkValue, String tableName) {
|
|
|
+
|
|
|
+ //构造主键。
|
|
|
+ PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
+ primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
+ PrimaryKey primaryKey = primaryKeyBuilder.build();
|
|
|
+ //设置数据表名称。
|
|
|
+ return new RowUpdateChange(tableName, primaryKey);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|