|
@@ -1,6 +1,8 @@
|
|
|
package com.abi.task.common.utils;
|
|
|
|
|
|
import com.alicloud.openservices.tablestore.SyncClient;
|
|
|
+import com.alicloud.openservices.tablestore.model.BatchWriteRowRequest;
|
|
|
+import com.alicloud.openservices.tablestore.model.BatchWriteRowResponse;
|
|
|
import com.alicloud.openservices.tablestore.model.CapacityUnit;
|
|
|
import com.alicloud.openservices.tablestore.model.Column;
|
|
|
import com.alicloud.openservices.tablestore.model.ColumnValue;
|
|
@@ -98,8 +100,6 @@ public class TableStoreUtils {
|
|
|
* @param primaryKeyName 主键名称
|
|
|
* @param pkValue 主键值
|
|
|
* @param tableName 表名
|
|
|
-// * @param columnName 列名称
|
|
|
-// * @param columnValue 列值
|
|
|
*/
|
|
|
public void putRow(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
|
|
|
//构造主键。
|
|
@@ -111,8 +111,6 @@ public class TableStoreUtils {
|
|
|
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));
|
|
@@ -207,6 +205,37 @@ public class TableStoreUtils {
|
|
|
client().deleteRow(new DeleteRowRequest(rowDeleteChange));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public void batchWriteRow(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
|
|
|
+ BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
|
|
|
+
|
|
|
+ //构造rowPutChange。
|
|
|
+ PrimaryKeyBuilder pkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|
|
|
+ pkBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
|
|
|
+ RowPutChange rowPutChange = new RowPutChange(tableName, pkBuilder.build());
|
|
|
+ //添加属性列
|
|
|
+ rowPutChange.addColumns(columns);
|
|
|
+ //添加到batch操作中。
|
|
|
+ batchWriteRowRequest.addRowChange(rowPutChange);
|
|
|
+
|
|
|
+
|
|
|
+ BatchWriteRowResponse response = client().batchWriteRow(batchWriteRowRequest);
|
|
|
+
|
|
|
+ log.info("是否全部成功:{}", response.isAllSucceed());
|
|
|
+ if (!response.isAllSucceed()) {
|
|
|
+ for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
|
|
|
+ log.info("失败的行:{}", batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
|
|
|
+ log.info("失败原因:{}", rowResult.getError());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试。此处只给出构造重试请求的部分。
|
|
|
+ * 推荐的重试方法是使用SDK的自定义重试策略功能,支持对batch操作的部分行错误进行重试。设置重试策略后,调用接口处无需增加重试代码。
|
|
|
+ */
|
|
|
+ batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private SingleRowQueryCriteria queryCriteria(String primaryKeyName, String pkValue, String tableName) {
|
|
|
//构造主键。
|
|
|
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
|