首页 » 技术分享 » 阿里OTS表格存储单挑与批量多行插入--javaSDK

阿里OTS表格存储单挑与批量多行插入--javaSDK

 

@Autowired

private SyncClient syncClient;

----------------------------单条写入-------------------------------

/**
* ots单条写入
*/
 
     PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_CONNECTOR_ID, PrimaryKeyValue.fromString(info.getConnectorID()));
        primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_OPERATOR_ID, PrimaryKeyValue.fromString(operatorId));
        primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_STATION_ID, PrimaryKeyValue.fromString(stationId));
        primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_DATA_TIME, PrimaryKeyValue.fromLong(dataTime));
       PrimaryKey primaryKey = primaryKeyBuilder.build();
        RowPutChange rowPutChange = new RowPutChange(AliTSConfigs.TB_NAME_CONNECTOR_RT, primaryKey);

        rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_STATUS, ColumnValue.fromLong(info.getStatus())));
       
rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_PARKSTATUS, ColumnValue.fromLong(info.getParkStatus())));
        rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_LOCKSTATUS, ColumnValue.fromLong(info.getLockStatus())));
        PutRowRequest request = new PutRowRequest();
        rowPutChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST)); 
        request.setRowChange(rowPutChange);
        syncClient.putRow(request);


----------------------------批量写入-------------------------------

/**
* ots批量操作
*/
BatchWriteRowRequest request = new BatchWriteRowRequest();
try {
  for (int i = 0; i < list.size(); i++) {
      PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
 primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_CONNECTOR_ID, PrimaryKeyValue.fromString(list.get(i).getConnectorID()));
 primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_OPERATOR_ID, PrimaryKeyValue.fromString(operatorId));
  primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_STATION_ID, PrimaryKeyValue.fromString(stationId));
    primaryKeyBuilder.addPrimaryKeyColumn(AliTSConfigs.TB_PK_NAME_DATA_TIME, PrimaryKeyValue.fromLong(dataTime));

      PrimaryKey primaryKey = primaryKeyBuilder.build();

RowPutChange rowPutChange = new RowPutChange(AliTSConfigs.TB_NAME_CONNECTOR_RT, primaryKey);
rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_STATUS, ColumnValue.fromLong(list.get(i).getStatus())));
        rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_PARKSTATUS, ColumnValue.fromLong(list.get(i).getParkStatus())));
       rowPutChange.addColumn(new Column(AliTSConfigs.TB_COLUMN_LOCKSTATUS, ColumnValue.fromLong(list.get(i).getLockStatus())));

       // 构造插入数据的请求对象
       request.addRowChange(rowPutChange);
   }

// batchWriteRow接口会返回一个结果集 结果集中包含的结果个数与插入的行数相同,结果集中的结果不一定都是成功,用户需要自己对不成功的操作进行重试。
BatchWriteRowResponse  response  = syncClient.batchWriteRow(request);
// 结果异常处理
if (!response.isAllSucceed()) {
               for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
               log.error("失败的行:" + request.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
               log.error("失败原因:" + rowResult.getError().toString());
              }
}

 // 重试逻辑类似于上面的batchGetRow示例中的逻辑,这里省略

   } catch (Exception ex) {
    ex.printStackTrace();
    log.error("Batch write row failed.");
   } 

图片看着更清晰

转载自原文链接, 如需删除请联系管理员。

原文链接:阿里OTS表格存储单挑与批量多行插入--javaSDK,转载请注明来源!

0