|
@@ -4,6 +4,7 @@ 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.TableStoreEntity;
|
|
|
import com.abi.task.common.tablestore.common.TableStoreReq;
|
|
|
import com.abi.task.common.tablestore.common.TableStoreRes;
|
|
|
import com.alicloud.openservices.tablestore.SyncClient;
|
|
@@ -18,10 +19,13 @@ import com.alicloud.openservices.tablestore.model.search.sort.Sort;
|
|
|
import com.alicloud.openservices.tablestore.model.search.sort.SortOrder;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.ListUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -47,6 +51,26 @@ public class TableStoreUtils {
|
|
|
@Value("${tableStore.instanceName}")
|
|
|
private String instanceName;
|
|
|
|
|
|
+
|
|
|
+ * 单词交互条数 200条
|
|
|
+ */
|
|
|
+ private static final Integer MAX_COUNT_ONCE = 200;
|
|
|
+
|
|
|
+
|
|
|
+ * 批量新增时用到得线程池
|
|
|
+ */
|
|
|
+ private ThreadPoolTaskExecutor taskExecutor;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void initTaskExecutor(){
|
|
|
+ taskExecutor = new ThreadPoolTaskExecutor();
|
|
|
+
|
|
|
+ taskExecutor.setCorePoolSize(1);
|
|
|
+ taskExecutor.setMaxPoolSize(20);
|
|
|
+ taskExecutor.setQueueCapacity(10000);
|
|
|
+ taskExecutor.initialize();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 创建SyncClient
|
|
|
*
|
|
@@ -100,14 +124,6 @@ public class TableStoreUtils {
|
|
|
client().putRow(new PutRowRequest(rowPutChange));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * 批量插入
|
|
|
- *
|
|
|
- * @param primaryKeyName
|
|
|
- * @param pkValueList
|
|
|
- * @param tableName
|
|
|
- * @param columnsList
|
|
|
- */
|
|
|
public void batchPutRow(String primaryKeyName, List<String> pkValueList, String tableName, List<List<Column>> columnsList) {
|
|
|
if (CollectionUtil.isEmpty(pkValueList)) {
|
|
|
throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "主键内容为空");
|
|
@@ -119,6 +135,36 @@ public class TableStoreUtils {
|
|
|
throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "主键与字段数量不匹配");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ if(CollectionUtil.isEmpty(pkValueList) && pkValueList.size()<=MAX_COUNT_ONCE){
|
|
|
+ doBatchPutRow(primaryKeyName,pkValueList,tableName,columnsList);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ List<List<String>> pkValuePartitionList = ListUtils.partition(pkValueList, MAX_COUNT_ONCE);
|
|
|
+ List<List<List<Column>>> columnsPartitionList = ListUtils.partition(columnsList, MAX_COUNT_ONCE);
|
|
|
+
|
|
|
+
|
|
|
+ for(int i=0;i<pkValuePartitionList.size();i++){
|
|
|
+ List<String> tempPkList = pkValuePartitionList.get(i);
|
|
|
+ List<List<Column>> tempColumnsList = columnsPartitionList.get(i);
|
|
|
+ taskExecutor.execute(()->{
|
|
|
+ doBatchPutRow(primaryKeyName,tempPkList,tableName,tempColumnsList);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 真正批量插入
|
|
|
+ * @param primaryKeyName
|
|
|
+ * @param pkValueList
|
|
|
+ * @param tableName
|
|
|
+ * @param columnsList
|
|
|
+ */
|
|
|
+ public void doBatchPutRow(String primaryKeyName, List<String> pkValueList, String tableName, List<List<Column>> columnsList) {
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
+
|
|
|
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
|
|
|
|
|
|
for (int i = 0; i < pkValueList.size(); i++) {
|
|
@@ -126,10 +172,11 @@ public class TableStoreUtils {
|
|
|
|
|
|
batchWriteRowRequest.addRowChange(rowPutChange);
|
|
|
}
|
|
|
-
|
|
|
BatchWriteRowResponse response = client().batchWriteRow(batchWriteRowRequest);
|
|
|
|
|
|
- log.info("是否全部成功:{}", response.isAllSucceed());
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+
|
|
|
+ log.info("是否全部成功:{} ,耗时{}毫秒", response.isAllSucceed(),end-begin);
|
|
|
if (!response.isAllSucceed()) {
|
|
|
for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
|
|
|
log.info("失败的行:{}", batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
|