TableStore+java:單行操做

TableStore數據準備: 數據庫

首先須要添加TableStore的依賴測試

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

運行項目時可能會出現SLF4J相關的紅字提示: ui

該提示不影響使用,若是以爲不爽的話,能夠添加下面依賴去除:3d

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

TableStore的主鍵含自增類型,該類型爲Interger,可是值很大,不能經過這中自增主鍵查詢數據,所以下面的SDK查詢操做都是不容許有自增主鍵的。rest

另外。表中的數據操做,對Column或者說屬性是有版本號這個概念的,該版本號以時間戳爲準。固然,若是屬性須要存儲多個版本號的數據,在建表的時候要注意最大版本號這麼一項,須要設置大於1。 code

一、根據主鍵查詢數據blog

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

}

若是隻須要讀取指定列,能夠這樣設置:get

// 設置讀取某些列
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");這麼設置運行也不會報錯,不過仍然會讀出全部屬性。it

二、根據主鍵查詢數據-設置過濾器io

能夠表示的列與值的比較關係包括: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爲空");
}

三、根據主鍵修改一行數據

修改前

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));

修改後

四、新增一行數據

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以後,發現原數據被新數據直接覆蓋了。

五、指望原行不存在時寫入

經測試:當主鍵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());
}

六、指望存在並某屬性知足某條件時,覆蓋寫入行

同上,知足條件是覆蓋該主鍵的數據,不然出錯

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

七、刪除一行數據

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));

八、根據條件刪除數據

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());
}
相關文章
相關標籤/搜索