说明:

首先需要添加TableStore的依赖

<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>tablestore</artifactId>
    <version>4.10.2</version>
</dependency>

运行项目时可能会出现SLF4J相关的红字提示:

该提示不影响使用,如果觉得不爽的话,可以添加下面依赖去除:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.26</version>
</dependency>

TableStore的主键含自增类型,该类型为Interger,但是值很大,不能通过这中自增主键查询数据,因此下面的SDK查询操作都是不允许有自增主键的。

另外。表中的数据操作,对Column或者说属性是有版本号这个概念的,该版本号以时间戳为准。当然,如果属性需要存储多个版本号的数据,在建表的时候要注意最大版本号这么一项,需要设置大于1

1、根据主键查询数据

package com.example.demo;

import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.Column;
import com.alicloud.openservices.tablestore.model.GetRowRequest;
import com.alicloud.openservices.tablestore.model.GetRowResponse;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.Row;
import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;

public class Query {

    public static void main(String[] args) {

        final String endPoint = "实例访问地址";
        final String accessKeyId = "accessKeyId";
        final String accessKeySecret = "accessKeySecret";
        // 实例名
        final String instanceName = "owlforest";

        SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

        // 构造主键
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
        PrimaryKey primaryKey = primaryKeyBuilder.build();
        SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("note", primaryKey);
        // 设置读取最新版本
        criteria.setMaxVersions(1);        // 读取所有数据
        GetRowResponse getRowResponse = client.getRow(new GetRowRequest(criteria));
        Row row = getRowResponse.getRow();
        // 遍历所有列
        Column[] columns = row.getColumns();
        for (Column column : columns) {
            System.out.println("Name:" + column.getName() + " Value:" + column.getValue());
        }
    }

}

如果只需要读取指定列,可以这样设置:

// 设置读取某些列
String[] a = { "author" };
criteria.addColumnsToGet(a);
GetRowResponse getRowResponse2 = client.getRow(new GetRowRequest(criteria));
Row row2 = getRowResponse2.getRow();
Column[] columns2 = row2.getColumns();
for (Column column : columns2) {
    System.out.println("Name:" + column.getName() + " Value:" + column.getValue());
}

经测试,criteria.addColumnsToGet("author");这么设置运行也不会报错,不过仍然会读出所有属性。

2、根据主键查询数据-设置过滤器

可以表示的列与值的比较关系包括:EQUAL(=), NOT_EQUAL(!=), GREATER_THAN(>), GREATER_EQUAL(>=), LESS_THAN(<)以及LESS_EQUAL(<=)。

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
PrimaryKey primaryKey = primaryKeyBuilder.build();

// 读一行
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("note", primaryKey);
// 设置读取最新版本
criteria.setMaxVersions(1);

//设置过滤器
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
"author", CompareOperator.EQUAL, ColumnValue.fromString("I am zhangsan"));
//如果不存在此列,也不返回
singleColumnValueFilter.setPassIfMissing(false);
criteria.setFilter(singleColumnValueFilter);

GetRowResponse getRowResponse = client.getRow(new GetRowRequest(criteria));
Row row = getRowResponse.getRow();

if(row != null) {
    Column[] columns = row.getColumns();
    for(Column column : columns) {
System.out.println("Name:" + column.getName() + " Value:" + column.getValue() + "\n");
    }
}else {
    System.out.println("Row为空");
}

3、根据主键修改一行数据

修改前

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
PrimaryKey primaryKey = primaryKeyBuilder.build();

RowUpdateChange rowUpdateChange = new RowUpdateChange("note", primaryKey);
//修改author属性
rowUpdateChange.put(new Column("author", ColumnValue.fromString("我叫张三")));
//新增属性阅读量view
rowUpdateChange.put(new Column("view", ColumnValue.fromLong(10)));//删除title属性rowUpdateChange.deleteColumns("title");// 删除某列的某一版本//rowUpdateChange.deleteColumn(属性名, 版本号);
 
UpdateRowResponse response = client.updateRow(new UpdateRowRequest(rowUpdateChange));

修改后

4、新增一行数据

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
PrimaryKey primaryKey = primaryKeyBuilder.build();

RowPutChange rowPutChange = new RowPutChange("note", primaryKey);
long ts = System.currentTimeMillis();

rowPutChange.addColumn(new Column("year", ColumnValue.fromLong(2019), ts));
rowPutChange.addColumn(new Column("month", ColumnValue.fromLong(3), ts));

rowPutChange.addColumn(new Column("day", ColumnValue.fromString("天"), ts + 1));
rowPutChange.addColumn(new Column("day", ColumnValue.fromString("日"), ts + 2));

client.putRow(new PutRowRequest(rowPutChange));

note表当中原先就有一条noteid为1的数据,执行PutRow之后,发现原数据被新数据直接覆盖了。

5、期望原行不存在时写入

经测试:当主键1存在时,添加条件RowExistenceExpectation.EXPECT_NOT_EXIST,执行PutRow会出现异常Failed RetriedCount:0 [ErrorCode]:OTSConditionCheckFail, [Message]:Condition check failed.

如果构造的主键不存在于数据库,则可以正常插入。这个条件的使用场景比较让人费解,先记下,兴许后续会用上。

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
try {
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
    PrimaryKey primaryKey = primaryKeyBuilder.build();
    RowPutChange rowPutChange = new RowPutChange("note", primaryKey);
        rowPutChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST));
        rowPutChange.addColumn(new Column("attr", ColumnValue.fromString("主键为1,看不到我"),System.currentTimeMillis()));
    client.putRow(new PutRowRequest(rowPutChange));
} catch (Exception e) {
    System.out.println(e.getMessage());
}

6、期望存在并某属性满足某条件时,覆盖写入行

同上,满足条件是覆盖该主键的数据,否则出错

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(2));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowPutChange rowPutChange = new RowPutChange("note", primaryKey);

try {
    // 期望原行存在,且属性year=2019时插入
    Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
    condition.setColumnCondition(
    new SingleColumnValueCondition("year", CompareOperator.EQUAL, ColumnValue.fromLong(2019)));
    rowPutChange.setCondition(condition);

    rowPutChange.addColumn(new Column("attr", ColumnValue.fromString("2019年")));
    client.putRow(new PutRowRequest(rowPutChange));
} catch (Exception e) {
    System.out.println(e.getMessage());
}

7、删除一行数据

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(10));
PrimaryKey primaryKey = primaryKeyBuilder.build();

RowDeleteChange rowDeleteChange = new RowDeleteChange("note", primaryKey);
client.deleteRow(new DeleteRowRequest(rowDeleteChange));

8、根据条件删除数据

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);

// 构造主键
try {
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));
    PrimaryKey primaryKey = primaryKeyBuilder.build();

    RowDeleteChange rowDeleteChange = new RowDeleteChange("note", primaryKey);
    Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
    condition.setColumnCondition(new SingleColumnValueCondition("year",
    SingleColumnValueCondition.CompareOperator.EQUAL, ColumnValue.fromLong(2019)));
    rowDeleteChange.setCondition(condition);

    client.deleteRow(new DeleteRowRequest(rowDeleteChange));
} catch (Exception e) {
    System.out.println(e.getMessage());
}

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

  1. aliyun TableStore相关操作汇总

    总结:这个东西本身可能技术还不成熟,使用的人少,有问题很验证解决 遇到的问题:(1)没有一个GUI工具,使用门槛高(2)查询的GetRange不方便,把查询出来的数据使用System.out.prin ...

  2. hive支持事务及单行操作 update delete

    测试环境  Hive 1.2.1000.2.6.0.3-8 set hive.support.concurrency=true; set hive.exec.dynamic.partition.mod ...

  3. python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)

    一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...

  4. hbase简单操作

    hbase有hbase shell以及hbase 客户端api两种方式进行hbase数据库操作: 首先,hbase shell是在linux命令行进行操作,输入hbase shell命令,进入shel ...

  5. SQLAlchemy常用操作

    Models 只是配置和使用比较简单,因为他是Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy 真正算得上全面的ORM框架必然是我们的SQLAlch ...

  6. MySQL之基础操作

    一.安装 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数 ...

  7. python工业互联网应用实战5—Django Admin 编辑界面和操作

    1.1. 编辑界面 默认任务的编辑界面,对于model属性包含"choices"会自动显示下来列表供选择,"datetime"数据类型也默认提供时间选择组件,如 ...

  8. Visual Studio Code 配置指南

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器.在我看来它是「一款完美的编辑器」. 本文是有关 VS Code 的特性介绍与配置指 ...

  9. 2、C#面向对象:封装、继承、多态、String、集合、文件(上)

    面向对象封装 一.面向对象概念 面向过程:面向的是完成一件事情的过程,强调的是完成这件事情的动作. 面向对象:找个对象帮你完成这件事情. 二.面向对象封装 把方法进行封装,隐藏实现细节,外部直接调用. ...

随机推荐

  1. redis实现与分析-多机数据库

    1.复制,主从结构 redis 2.8以前的复制,由2个步骤 1.初始的同步 2.命令传播 存在问题:同步时出现主从 断线,需要重新发送同步sync信号,非常消耗性能 redis2.8以后新版复制 采 ...

  2. Feign 自定义编码器、解码器和客户端

    Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...

  3. yii 生成条码并上传到图片服务器(zimg)

    工具: 生成条码和二维码 https://github.com/codeitnowin/barcode-generator 配置:

  4. Cygwin使用2-心得

    引用:http://www.jb51.net/article/6236.htm 1.在cygwin里访问Windows盘 cd /cygdrive/c cd c: 2.整合cygwin命令到Windo ...

  5. shaderFX一些小心得

    同事需要一个能让贴图流动起来的shader,于是花了两天时间搞了一个,在这里要十分感谢同事的建议和提醒. 参考资料:<Digital Tutors - Implementing a Flow M ...

  6. 【巷子】---flux---【react】

    一.什么是Flux Flux 是一种架构思想,专门解决软件的结构问题.它跟MVC 架构是同一类东西,但是更加简单和清晰. 二.flux的基本概念 (1) .Flux由4部分组成 1.View:视图层 ...

  7. CAD中的各种Polyline

    序号 类 类名 dxf代码 1 Polyline2d AcDb2dPolyline POLYLINE 2 Polyline3d AcDb3dPolyline POLYLINE 3 Polyline A ...

  8. 黄聪:Windows+PHP7+ImageMagick

    需要Apache下才适用,Nginx没测试过~ Step1 把ImageMagick对应版本下载下来(ps: 我这里下载的是这个版本ImageMagick-6.9.3-7-vc14-x86.zip) ...

  9. uoj#158. 【清华集训2015】静态仙人掌

    http://uoj.ac/problem/158 预处理dfs序,询问转为区间1的个数,用可持久化bitset预处理出所有可能的修改对应哪些位置,然后用一个bitset维护当前每个点的状态,修改时可 ...

  10. bzoj4865: [Ynoi2017]由乃运椰子

    在线询问区间众数,传统的分块(记录块间众数和每个权值的出现次数)做法被卡空间(分块用的空间是O(块数*(块数+权值种类数))),因此考虑去掉出现次数较小的数,只用分块维护出现次数较大的数.设K为分界线 ...