日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TableStore:单行操作

發(fā)布時(shí)間:2025/3/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TableStore:单行操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

TableStore數(shù)據(jù)準(zhǔn)備:

首先需要添加TableStore的依賴

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

運(yùn)行項(xiàng)目時(shí)可能會出現(xiàn)SLF4J相關(guān)的紅字提示:

該提示不影響使用,如果覺得不爽的話,可以添加下面依賴去除:

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

TableStore的主鍵含自增類型,該類型為Interger,但是值很大,不能通過這中自增主鍵查詢數(shù)據(jù),因此下面的SDK查詢操作都是不允許有自增主鍵的。

另外。表中的數(shù)據(jù)操作,對Column或者說屬性是有版本號這個(gè)概念的,該版本號以時(shí)間戳為準(zhǔn)。當(dāng)然,如果屬性需要存儲多個(gè)版本號的數(shù)據(jù),在建表的時(shí)候要注意最大版本號這么一項(xiàng),需要設(shè)置大于1。

1、根據(jù)主鍵查詢數(shù)據(jù)

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 = "實(shí)例訪問地址";final String accessKeyId = "accessKeyId";final String accessKeySecret = "accessKeySecret";// 實(shí)例名final String instanceName = "owlforest";SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1));PrimaryKey primaryKey = primaryKeyBuilder.build();SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("note", primaryKey);// 設(shè)置讀取最新版本criteria.setMaxVersions(1);// 讀取所有數(shù)據(jù)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());}}}

如果只需要讀取指定列,可以這樣設(shè)置:

// 設(shè)置讀取某些列 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()); }

經(jīng)測試,criteria.addColumnsToGet("author");這么設(shè)置運(yùn)行也不會報(bào)錯(cuò),不過仍然會讀出所有屬性。

2、根據(jù)主鍵查詢數(shù)據(jù)-設(shè)置過濾器

可以表示的列與值的比較關(guān)系包括:EQUAL(=), NOT_EQUAL(!=), GREATER_THAN(>), GREATER_EQUAL(>=), LESS_THAN(<)以及LESS_EQUAL(<=)。

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("noteid", PrimaryKeyValue.fromLong(1)); PrimaryKey primaryKey = primaryKeyBuilder.build();// 讀一行 SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("note", primaryKey); // 設(shè)置讀取最新版本 criteria.setMaxVersions(1);//設(shè)置過濾器 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、根據(jù)主鍵修改一行數(shù)據(jù)

修改前

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 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、新增一行數(shù)據(jù)

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 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表當(dāng)中原先就有一條noteid為1的數(shù)據(jù),執(zhí)行PutRow之后,發(fā)現(xiàn)原數(shù)據(jù)被新數(shù)據(jù)直接覆蓋了。

5、期望原行不存在時(shí)寫入

經(jīng)測試:當(dāng)主鍵1存在時(shí),添加條件RowExistenceExpectation.EXPECT_NOT_EXIST,執(zhí)行PutRow會出現(xiàn)異常Failed RetriedCount:0 [ErrorCode]:OTSConditionCheckFail, [Message]:Condition check failed.

如果構(gòu)造的主鍵不存在于數(shù)據(jù)庫,則可以正常插入。這個(gè)條件的使用場景比較讓人費(fèi)解,先記下,興許后續(xù)會用上。

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 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、期望存在并某屬性滿足某條件時(shí),覆蓋寫入行

同上,滿足條件是覆蓋該主鍵的數(shù)據(jù),否則出錯(cuò)

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時(shí)插入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、刪除一行數(shù)據(jù)

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 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、根據(jù)條件刪除數(shù)據(jù)

SyncClient client = new SyncClient(endPoint, accessKeyId, accessKeySecret, instanceName);// 構(gòu)造主鍵 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()); }

轉(zhuǎn)載于:https://my.oschina.net/u/4108765/blog/3059588

總結(jié)

以上是生活随笔為你收集整理的TableStore:单行操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。