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<RowResult> 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<RowResult> 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<RowResult> 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<Row> 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());
        }

    }
}

TableStore:多行数据操作的更多相关文章

  1. MySQL命令行数据操作使用心得(总结版)

    Char 0~255 Varchar 0~65535 text 0~65535(只能保存字符) Longtext 0~4294967295(只能保存字符) CMD登陆mysql mysql -u ro ...

  2. FunDA(2)- Streaming Data Operation:流式数据操作

    在上一集的讨论里我们介绍并实现了强类型返回结果行.使用强类型主要的目的是当我们把后端数据库SQL批次操作搬到内存里转变成数据流式按行操作时能更方便.准确.高效地选定数据字段.在上集讨论示范里我们用集合 ...

  3. hive数据操作

    mdl是数据操作类的语言,包括向数据表加载文件,写查询结果等操作 hive有四种导入数据的方式 >从本地加载数据 LOAD DATA LOCAL INPATH './examples/files ...

  4. Django数据操作F和Q、model多对多操作、Django中间件、信号、读数据库里的数据实现分页

    models.tb.objects.all().using('default'),根据using来指定在哪个库里查询,default是settings中配置的数据库的连接名称. 外话:django中引 ...

  5. SQL不同服务器数据库之间的数据操作整理(完整版)

    ---------------------------------------------------------------------------------- -- Author : htl25 ...

  6. C#利用SqlDataAdapte对DataTable进行批量数据操作

    C#利用SqlDataAdapte对DataTable进行批量数据操作,可以让我们大大简化操作数据的代码量,我们几乎不需要循环和不关心用户到底是新增还是修改,更不用编写新增和修改以及删除的SQL语句, ...

  7. [转]Mysql命令行常用操作

    Mysql命令行常用操作 一.从命令行登录MySQL数据库服务器 1.登录使用默认3306端口的MySQL /usr/local/mysql/bin/mysql -u root -p 2.通过TCP连 ...

  8. oracle-2-sql数据操作和查询

    主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...

  9. (转)SQLLite数据操作

    原文:http://dreamboy.blog.51cto.com/3180937/722352 SQLLite数据操作 一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存 ...

随机推荐

  1. 商品和订单中使用MQ

    一.将Product服务增加到配置中心 1.添加引用 <dependency> <groupId>org.springframework.cloud</groupId&g ...

  2. NET设计模式 第二部分 行为型模式(17):迭代器模式(Iterator Pattern)

    概述 在面向对象的软件设计中,我们经常会遇到一类集合对象,这类集合对象的内部结构可能有着各种各样的实现,但是归结起来,无非有两点是需要我们去关心的:一是集合内部的数据存储结构,二是遍历集合内部的数据. ...

  3. npm是什么

    npm简介 维基百科中npm定义 npm完全用JavaScript写成,最初由艾萨克·施吕特(Isaac Z. Schlueter)开发. 艾萨克表示自己意识到"模块管理很糟糕"的 ...

  4. pyhanlp用户自定义词典添加实例说明

    pyhanlp用户自定义词典添加实例说明 pyhanlp是python版封装的的HanLP,项目地址:https://github.com/hankcs/pyhanlp 经过测试,HanLP比nltk ...

  5. ML平台_Paddle参考

    PaddlePaddle源自于 2013 年百度深度学习实验室创建的 “Paddle”.当时的深度学习框架大多只支持单 GPU 运算,对于百度这样需要对大规模数据进行处理的机构,这显然远远不够,极大拖 ...

  6. 禁用触发器的N种方法

    一.禁用和启用单个触发器 禁用: ALTER TABLE trig_example DISABLE TRIGGER trig1 GO   恢复: ALTER TABLE trig_example EN ...

  7. x230安装黑苹果

    https://forum.51nb.com/forum.php?mod=viewthread&tid=1802786&extra=page%3D1&page=1 下载 主要就 ...

  8. Ubuntu使用操作记录/笔记

    1.如何取得图形界面下的ROOT权限? 先alt+f2,然后 gksu nautilus 或者 kdesudo dolphin 2.如何使用Qt插件在Qt中进行ROS开发 http://blog.cs ...

  9. spring 事务的配置学习

    1.spring事务管理器接口PlatformTransactionManager 接口中的方法 获取事务状态信息 -TransactionStatus getTransaction(Transact ...

  10. ALGO-30_蓝桥杯_算法训练_入学考试DP)

    问题描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...