博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TableStore:多行数据操作
阅读量:5845 次
发布时间:2019-06-18

本文共 9488 字,大约阅读时间需要 31 分钟。

hot3.png

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());        }    }            List
results = 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);    Iterator
iterator = 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()); } }}

转载于:https://my.oschina.net/u/4108765/blog/3059589

你可能感兴趣的文章
实验四------实验十二
查看>>
一个CSS上中下三行三列结构的Div布局
查看>>
SqlDataAdapter的增加,删除,修改
查看>>
开启、关闭数据库mysql
查看>>
《APUE》读书笔记—第四章文件和目录
查看>>
日记(3)
查看>>
js字符串编码和unicode编码互转
查看>>
android手机导入.cer证书文件的方法
查看>>
烂泥: KVM虚拟机Linux系统增加硬盘
查看>>
Java汉字转成汉语拼音工具类
查看>>
IE6下解决select层级高的问题
查看>>
上架错误问题
查看>>
Python基础之变量和常量
查看>>
世界完全对称日计算(C++)
查看>>
vue全局引入scss文件(推荐)
查看>>
codevs——1436 孪生素数 2
查看>>
Vuejs——(4)v-if、v-for
查看>>
HDU2629 Identity Card【MAP+水题】
查看>>
POJ3438 ZOJ2886 UVALive3822 Look and Say【数列】
查看>>
About scrum reports
查看>>