1、批量写
public static void batchWriteRow(SyncClient client) { BatchWriteRowRequest request = new BatchWriteRowRequest(); //RowPut PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(99)); RowPutChange rowPutChange = new RowPutChange("note",primaryKeyBuilder.build()); //添加一些列 rowPutChange.addColumn(new Column("intattr", ColumnValue.fromLong(123))); rowPutChange.addColumn(new Column("strattr", ColumnValue.fromString("string 1"), System.currentTimeMillis())); rowPutChange.addColumn(new Column("strattr", ColumnValue.fromString("string 23345"),System.currentTimeMillis() + 10)); request.addRowChange(rowPutChange); //RowDelete PrimaryKeyBuilder primaryKeyBuilder2 = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder2.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(2)); RowDeleteChange rowDeleteChange = new RowDeleteChange("note",primaryKeyBuilder2.build()); request.addRowChange(rowDeleteChange); //RowUpdate PrimaryKeyBuilder primaryKeyBuilder3 = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder3.addPrimaryKeyColumn("noteid",PrimaryKeyValue.fromLong(1)); RowUpdateChange rowUpdateChange = new RowUpdateChange("note", primaryKeyBuilder3.build()); rowUpdateChange.put("day",ColumnValue.fromString("修改后的day")); rowUpdateChange.deleteColumns("month"); request.addRowChange(rowUpdateChange); //另一个表的RowPut PrimaryKeyBuilder keyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); keyBuilder.addPrimaryKeyColumn("userid",PrimaryKeyValue.fromLong(20)); keyBuilder.addPrimaryKeyColumn("userguid",PrimaryKeyValue.fromString("121212323")); keyBuilder.addPrimaryKeyColumn("userauto",PrimaryKeyValue.AUTO_INCREMENT); RowPutChange rowPutChange2 = new RowPutChange("user", keyBuilder.build()); request.addRowChange(rowPutChange2); BatchWriteRowResponse batchWriteRowResponse = client.batchWriteRow(request); System.out.println("是否全部成功:" + batchWriteRowResponse.isAllSucceed()); if(!batchWriteRowResponse.isAllSucceed()) { for(RowResult rowResult : batchWriteRowResponse.getFailedRows()) { System.out.println("失败的行:" + request.getRowChange(rowResult.getTableName(),rowResult.getIndex()).getPrimaryKey()); System.out.println("失败原因:" + rowResult.getError()); } } //可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试.这里只给出构造重试请求的部分. //推荐的重试方法是使用SDK的自定义重试策略功能, 支持对batch操作的部分行错误进行重试. 设定重试策略后, 调用接口处即不需要增加重试代码. //BatchWriteRowRequest retryRequest = request.createRequestForRetry(batchWriteRowResponse.getFailedRows());}
2、批量读
private static void batchGetRow(SyncClient client) { BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest(); MultiRowQueryCriteria multiRowQueryCriteria = new MultiRowQueryCriteria("note"); // 加入要读的行 PrimaryKeyBuilder keyBuilder1 = PrimaryKeyBuilder.createPrimaryKeyBuilder(); keyBuilder1.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1)); PrimaryKey key1 = keyBuilder1.build(); PrimaryKeyBuilder keyBuilder2 = PrimaryKeyBuilder.createPrimaryKeyBuilder(); keyBuilder2.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(99)); PrimaryKey key2 = keyBuilder2.build(); multiRowQueryCriteria.addRow(key1); multiRowQueryCriteria.addRow(key2); // 添加条件 multiRowQueryCriteria.setMaxVersions(1); String[] colsStrings = { "noteid", "day", "year" }; multiRowQueryCriteria.addColumnsToGet(colsStrings); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("year", SingleColumnValueFilter.CompareOperator.EQUAL, ColumnValue.fromLong(2019)); singleColumnValueFilter.setPassIfMissing(false); multiRowQueryCriteria.setFilter(singleColumnValueFilter); batchGetRowRequest.addMultiRowQueryCriteria(multiRowQueryCriteria); MultiRowQueryCriteria multiRowQueryCriteria2 = new MultiRowQueryCriteria("testdb"); multiRowQueryCriteria2.setMaxVersions(1); PrimaryKeyBuilder keyBuilder3 = PrimaryKeyBuilder.createPrimaryKeyBuilder(); keyBuilder3.addPrimaryKeyColumn("testid", PrimaryKeyValue.fromLong(1)); PrimaryKey key3 = keyBuilder3.build(); multiRowQueryCriteria2.addRow(key3); batchGetRowRequest.addMultiRowQueryCriteria(multiRowQueryCriteria2); BatchGetRowResponse batchGetRowResponse = client.batchGetRow(batchGetRowRequest); System.out.println("是否全部成功:" + batchGetRowResponse.isAllSucceed()); if (!batchGetRowResponse.isAllSucceed()) { for (RowResult rowResult : batchGetRowResponse.getFailedRows()) { System.out.println( "失败的行:" + batchGetRowRequest.getPrimaryKey(rowResult.getTableName(), rowResult.getIndex())); System.out.println("失败原因:" + rowResult.getError()); } } Listresults = batchGetRowResponse.getSucceedRows(); for (RowResult rowResult : results) { Row row = rowResult.getRow(); if (row != null) { Column[] columns = row.getColumns(); for (Column column : columns) { System.out.println("Name:" + column.getName() + " Value:" + column.getValue() + "\n"); } } } List results1 = batchGetRowResponse.getBatchGetRowResult("note"); for (RowResult rowResult : results1) { Row row = rowResult.getRow(); if (row != null) { Column[] columns = row.getColumns(); for (Column column : columns) { System.out.println("Name:" + column.getName() + " Value:" + column.getValue() + "\n"); } } } List results2 = batchGetRowResponse.getBatchGetRowResult("testdb"); for (RowResult rowResult : results2) { Row row = rowResult.getRow(); if (row != null) { Column[] columns = row.getColumns(); for (Column column : columns) { System.out.println("Name:" + column.getName() + " Value:" + column.getValue() + "\n"); } } } // 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试.这里只给出构造重试请求的部分. // 推荐的重试方法是使用SDK的自定义重试策略功能, 支持对batch操作的部分行错误进行重试. 设定重试策略后, 调用接口处即不需要增加重试代码. // BatchGetRowRequest retryRequest = // batchGetRowRequest.createRequestForRetry(batchGetRowResponse.getFailedRows());}
因为不通过条件的查询都会返回null,务必记得对Row做null检查。
3、范围读
设置起止主键,查找此范围内的数据,当数据量过大无法一次读取完时,会返回下一个主键位置,接着读取。
public static void getRange(SyncClient client) throws IOException { RangeRowQueryCriteria rangeRowQueryCriteria = new RangeRowQueryCriteria("note"); //设置起始主键 PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(20)); rangeRowQueryCriteria.setInclusiveStartPrimaryKey(primaryKeyBuilder.build()); //设置结果主键 primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1)); rangeRowQueryCriteria.setExclusiveEndPrimaryKey(primaryKeyBuilder.build()); //反序读 rangeRowQueryCriteria.setDirection(Direction.BACKWARD); rangeRowQueryCriteria.setMaxVersions(1); while (true) { GetRangeResponse getRangeResponse = client.getRange(new GetRangeRequest(rangeRowQueryCriteria)); for(Row row : getRangeResponse.getRows()) { PrimaryKeyColumn[] pks = row.getPrimaryKey().getPrimaryKeyColumns(); Column pkColumn = pks[0].toColumn(); System.out.println("noteid:" + pkColumn.getValue()); Column[] columns = row.getColumns(); for(Column column : columns) { System.out.println(" Name:" + column.getName() + " Value:" + column.getValue()); } } if(getRangeResponse.getNextStartPrimaryKey() != null) { System.out.println("--------nextStartPrimaryKey不为空,则继续读取---------"); rangeRowQueryCriteria.setInclusiveStartPrimaryKey(getRangeResponse.getNextStartPrimaryKey()); }else { break; } }}
4、迭代读
public static void getRangeByIterator(SyncClient client) throws IOException { RangeIteratorParameter rangeIteratorParameter = new RangeIteratorParameter("note"); //设置起始主键 PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(20)); rangeIteratorParameter.setInclusiveStartPrimaryKey(primaryKeyBuilder.build()); //设置结果主键 primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1)); rangeIteratorParameter.setExclusiveEndPrimaryKey(primaryKeyBuilder.build()); rangeIteratorParameter.setDirection(Direction.BACKWARD); rangeIteratorParameter.setMaxVersions(1); Iteratoriterator = client.createRangeIterator(rangeIteratorParameter); while (iterator.hasNext()) { Row row = iterator.next(); PrimaryKeyColumn[] pks = row.getPrimaryKey().getPrimaryKeyColumns(); Column pkColumn = pks[0].toColumn(); System.out.println("noteid:" + pkColumn.getValue()); Column[] columns = row.getColumns(); for(Column column : columns) { System.out.println(" Name:" + column.getName() + " Value:" + column.getValue()); } }}