TableStore:多行数据操作
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:多行数据操作的更多相关文章
- MySQL命令行数据操作使用心得(总结版)
Char 0~255 Varchar 0~65535 text 0~65535(只能保存字符) Longtext 0~4294967295(只能保存字符) CMD登陆mysql mysql -u ro ...
- FunDA(2)- Streaming Data Operation:流式数据操作
在上一集的讨论里我们介绍并实现了强类型返回结果行.使用强类型主要的目的是当我们把后端数据库SQL批次操作搬到内存里转变成数据流式按行操作时能更方便.准确.高效地选定数据字段.在上集讨论示范里我们用集合 ...
- hive数据操作
mdl是数据操作类的语言,包括向数据表加载文件,写查询结果等操作 hive有四种导入数据的方式 >从本地加载数据 LOAD DATA LOCAL INPATH './examples/files ...
- Django数据操作F和Q、model多对多操作、Django中间件、信号、读数据库里的数据实现分页
models.tb.objects.all().using('default'),根据using来指定在哪个库里查询,default是settings中配置的数据库的连接名称. 外话:django中引 ...
- SQL不同服务器数据库之间的数据操作整理(完整版)
---------------------------------------------------------------------------------- -- Author : htl25 ...
- C#利用SqlDataAdapte对DataTable进行批量数据操作
C#利用SqlDataAdapte对DataTable进行批量数据操作,可以让我们大大简化操作数据的代码量,我们几乎不需要循环和不关心用户到底是新增还是修改,更不用编写新增和修改以及删除的SQL语句, ...
- [转]Mysql命令行常用操作
Mysql命令行常用操作 一.从命令行登录MySQL数据库服务器 1.登录使用默认3306端口的MySQL /usr/local/mysql/bin/mysql -u root -p 2.通过TCP连 ...
- oracle-2-sql数据操作和查询
主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...
- (转)SQLLite数据操作
原文:http://dreamboy.blog.51cto.com/3180937/722352 SQLLite数据操作 一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存 ...
随机推荐
- MySQL 中,\g和\G的作用
MySQL 中,\g和\G的作用: \g 的作用是 go (\g) Send command to mysql server. \G 的作用是将查询到的结果,每行显示一个字段和字段值,方便查看 ego ...
- django使用session报错:no such table: django_session
Django版本:1.11.15 使用session的代码:request.session['key'] = value 运行后报错:no such table: django_session 解决办 ...
- golang 原子操作函数
golang中的原子操作在sync/atomic package中. 下文以比较和交换操作函数为例,介绍其使用. CompareAndSwapInt32 比较和交换操作是原子性的. // Compar ...
- java面试题001
1.指针和函数的关系 这里主要谈指针函数和函数指针,在c中指针函数是返回值为指针的函数:函数指针是指向函数的指针变量. 2.什么是事务? 为了完成对数据的操作,要求并发访问在多个构件之间共享的数据.这 ...
- 安装ruby&gem
#安装yaml#------------------------------------------------------- cd /opt tar zxf yaml-0.1.7.tar.gz ./ ...
- 转:区块链开发(一)搭建基于以太坊go-ethereum的私有链环境
区块链开发(一)搭建基于以太坊go-ethereum的私有链环境 wo541075754 · 2016-11-07 13:00:03 · 3730 次点击 · 预计阅读时间 3 分钟 · 约1小时前 ...
- qt编程
http://www.zhihu.com/question/20054048 http://www.cnblogs.com/luoshupeng/archive/2011/05/01/2033743. ...
- 黄聪:PHP如何实现延迟一定时间后自动刷新当前页面、自动跳转header("refresh:1;url={$url}");
//1秒后自动跳转 header("refresh:1;url={$url}"); exit; //1秒后自动刷新当前页面header("refresh:1;" ...
- Centos 安装Sublime text 3
1.下载sublime3 http://www.sublimetext.com/3 2.解压缩,即为安装. # cd /opt # tar xjf /home/alex/Downloads/subli ...
- Windows 使用windump进行循环抓包
准备工作 1.下载tcpdump http://www.winpcap.org/windump/ 2.下载WinPcaphttp://www.winpcap.org/install/bin/WinP ...