阿里云-表格存儲(chǔ)服務(wù)SDK參考手冊(cè)_第1頁
阿里云-表格存儲(chǔ)服務(wù)SDK參考手冊(cè)_第2頁
阿里云-表格存儲(chǔ)服務(wù)SDK參考手冊(cè)_第3頁
阿里云-表格存儲(chǔ)服務(wù)SDK參考手冊(cè)_第4頁
阿里云-表格存儲(chǔ)服務(wù)SDK參考手冊(cè)_第5頁
已閱讀5頁,還剩66頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、表格存儲(chǔ)SDK參考手冊(cè)表格存儲(chǔ)/SDK參考手冊(cè) PAGE 70SDK參考手冊(cè)Java-SDK 前言簡介本文檔主要介紹TableStore(原OTS)Java SDK的安裝和使用,適用2.2.4版本。并且假設(shè)您已經(jīng)開通了阿里云TableStore服務(wù),并創(chuàng)建了AccessKeyId 和AccessKeySecret。如果您還沒有開通或者還不了解阿里云TableStore服務(wù),請(qǐng)登錄TableStore產(chǎn)品主頁了解。如果還沒有創(chuàng)建AccessKeyId和AccessKeySecret,請(qǐng)到阿里云Access Key管理創(chuàng)建Access Key。SDK下載SDK包:aliyun_tablestore

2、_java_sdk_2.2.4.zipGitHub:/aliyun/aliyun-tablestore-java-sdk版本迭代詳情參考這里版本當(dāng)前最新版本:2.2.4兼容性對(duì)于2.x.x 系列SDK:兼容變更內(nèi)容新增condition update新增filter安裝環(huán)境準(zhǔn)備適用于JDK 6及以上版本安裝方式Maven方式com.aliyun.openservicesots-public2.2.4在Maven工程中使用TableStore Java SDK只需在pom.xml中加入相應(yīng)依賴即可。以2.2.4版本為例,在dependencies標(biāo)簽內(nèi)加入如下內(nèi)容:Eclipse中導(dǎo)入JAR包以

3、2.2.4版本為例,步驟如下:下載Java SDK開發(fā)包版本號(hào) aliyun_tablestore_java_sdk_2.2.4.zip解壓該開發(fā)包;將解壓后文件夾中的文件: ots-public-.jar 以及l(fā)ib文件夾下的所有文件拷貝到您的項(xiàng)目中;在Eclipse中選擇您的工程,右擊 - Properties - Java Build Path - Add JARs;選中您在第三步拷貝的所有JAR文件;經(jīng)過以上幾步,您就可以在Eclipse項(xiàng)目中使用TableStore Java SDK。示例程序TableStore Java SDK提供豐富的示例程序,方便用戶參考或直接使用。您可以通過

4、以下兩種方式獲取示例程序:下載TableStore Java SDK開發(fā)包后,解壓后examples為示例程序;訪問TableStore Java SDK的GitHub項(xiàng)目:aliyun-tablestore-java-sdk初始化OTSClient是表格存儲(chǔ)(TableStore)服務(wù)的客戶端,它為調(diào)用者提供了一系列的方法,可以用來操作表、讀寫單 行數(shù)據(jù)、讀寫多行數(shù)據(jù)等。使用Java SDK發(fā)起TableStore請(qǐng)求,您需要初始化一個(gè)OTSClient實(shí)例,并根據(jù)需 要修改ClientConfiguration的默認(rèn)配置項(xiàng)。確定EndpointEndpoint是阿里云TableStore服

5、務(wù)各個(gè)實(shí)例的域名地址,目前支持下列形式示例解釋 HYPERLINK / HTTP協(xié)議,公網(wǎng)網(wǎng)絡(luò)訪問杭州區(qū)域的sun實(shí)例HTTPS協(xié)議,公網(wǎng)網(wǎng)絡(luò)訪問杭州區(qū)域的sun實(shí)例提示:除了公網(wǎng)可以訪問外,也支持私網(wǎng)地址。您可以登陸阿里云TableStore控制臺(tái),進(jìn)入Instance概覽頁,實(shí)例訪問地址即是該實(shí)例的Endpoint。配置密鑰要接入阿里云TableStore,您需要擁有一個(gè)有效的訪問密鑰進(jìn)行簽名認(rèn)證。目前支持下面三種方式:主帳號(hào)的AccessKeyId和AccessKeySecret通過以下方式獲得注冊(cè)阿里云帳號(hào)申請(qǐng)AccessKey被授予訪問TableStore權(quán)限的子帳號(hào)的AccessK

6、eyId和AccesskeySecret主帳號(hào)前往訪問控制RAM創(chuàng)建一個(gè)新的子帳號(hào)或者使用已經(jīng)存在的子帳號(hào)主帳號(hào)授予子帳號(hào)訪問TableStore的權(quán)限子帳號(hào)被授權(quán)后,就可以使用自己的AccessKeyId和AccessKeySecret訪問了。臨時(shí)訪問的STS token應(yīng)用的服務(wù)器通過訪問RAM/STS服務(wù),獲取一個(gè)臨時(shí)的AccesskeyId,AccesskeySecret和tokne,發(fā)送給使用方使用方使用上述臨時(shí)密鑰訪問TableStore服務(wù)在獲取到 AccessKeyId和 AccessKeySecretd等密鑰之后,您可以按照下面步驟進(jìn)行初始化對(duì)接新建Client使用Table

7、Store Endpoint新建Client/*使用指定的OTS Endpoint和默認(rèn)配置構(gòu)造一個(gè)新的實(shí)例。*注意:1. 大多數(shù)情況下,全局創(chuàng)建一個(gè)OTSClient對(duì)象即可(線程安全),不需要每次創(chuàng)建一個(gè)。當(dāng)并發(fā)極高時(shí)(數(shù)萬QPS),可以嘗試使用多個(gè)OTSClient對(duì)象進(jìn)行性能測試與調(diào)優(yōu),但數(shù)目也不宜過多。2. 每個(gè)OTSClient會(huì)占用一定的線程、連接資源,可以通過ClientConfiguration配置線程數(shù)、連接數(shù)等。3. 在使用完畢后,請(qǐng)調(diào)用shutdown方法釋放OTSClient占有的線程和連接資源。*param endpoint TableStore服務(wù)的endpoin

8、t。param accessKeyId 訪問TableStore服務(wù)的Access Key ID。param accessKeySecret 訪問TableStore服務(wù)的Access Key Secret。*/public OTSClient(String endpoint, String accessKeyId, String accessKeySecret, String instanceName);接口示例final String endPoint = ; final String accessId = ;final String accessKey = ;final String i

9、nstanceName = ;/ 創(chuàng)建一個(gè)OTSClient對(duì)象OTSClient client = new OTSClient(endPoint, accessId, accessKey, instanceName);/ 使用client讀寫TableStore/ 關(guān) 閉 client client.shutdown();提示:除了支持AccessKeyId,AccessKeySecret外,還支持STS token訪問。在創(chuàng)建OTSClient時(shí),也支持通過ClientConfiguration參數(shù)設(shè)置一些參數(shù),比如超時(shí)時(shí)間,最大 連接數(shù),最多重試次數(shù)等配置OTSClient如果您需要修改

10、OTSClient的一些默認(rèn)配置,請(qǐng)?jiān)跇?gòu)造OTSClient的時(shí)候傳入ClientConfiguration實(shí)例。ClientConfiguration是OTSClient的配置類,可配置代理、連接超時(shí)、最大連接數(shù)等參數(shù)。 通過ClientConfiguration可以設(shè)置的參數(shù)見下表:參數(shù)描述默認(rèn)值方法MaxConnections允許打開的最大HTTP連接數(shù)300ClientConfiguration. setMaxConnectionsSocketTimeoutInMill isecondSocket層傳輸數(shù)據(jù)的超時(shí)時(shí)間,0表示無限等待(單位:毫秒)15 1000ClientConfigu

11、ration. setSocketTimeoutIn MillisecondConnectionTimeoutI nMillisecond建立連接的超時(shí)時(shí)間,0表示無限等待(單位:毫秒)15 1000ClientConfiguration. setConnectionTimeo utInMillisecondRetryThreadCount用于執(zhí)行錯(cuò)誤重試的線程池的線程的個(gè)數(shù)1ClientConfiguration. setRetryThreadCoun tIoThreadCounHttpAsyncClient的IOReactor的線程數(shù)CPU數(shù)ClientConfiguration. set

12、IoThreadCountProxyPort代理服務(wù)器端口nullpublic void setProxyPort(int proxyPort)ProxyHost代理服務(wù)器主機(jī)地址nullClientConfiguration. setProxyHostProxyUsername代理服務(wù)器驗(yàn)證的用戶名nullClientConfiguration. setProxyUsernameProxyPassword代理服務(wù)器驗(yàn)證的密碼nullClientConfiguration. setProxyPasswordProxyDomain設(shè)置訪問NTLM驗(yàn)證的代理服務(wù)器的Windows域名nullCli

13、entConfiguration. setProxyDomainProxyWorkstationNTLM代理服務(wù)器的Windows工作站名稱nullClientConfiguration. setProxyWorkstationUserAgent用戶代理,指HTTP的User-Agent頭。aliyun-tablestore- sdk-javaClientConfiguration. setUserAgentHTTPS升級(jí)到j(luò)ava 7后即可多線程支持多線程使用多線程時(shí),建議共用一個(gè)OTSClient對(duì)象。表操作SDK提供了CreateTable,ListTable,DeleteTable,U

14、pdateTable和DescribeTable等表級(jí)別的操作接口創(chuàng)建表(CreateTable)根據(jù)給定的表結(jié)構(gòu)信息創(chuàng)建相應(yīng)的表。創(chuàng)建Table Store表時(shí)必須指定表的主鍵。主鍵包含14個(gè)主鍵列。每一個(gè)主鍵列都有名字和類型。/*創(chuàng)建表(Table)。*param createTableRequest 執(zhí)行CreateTable操作所需參數(shù)的封裝。return CreateTable操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public Crea

15、teTableResult createTable(CreateTableRequest createTableRequest) throws OTSException, ClientException;接口提示:- Table Store表在被創(chuàng)建之后需要幾秒鐘進(jìn)行加載,在此期間對(duì)該表的讀寫數(shù)據(jù)操作均會(huì)失敗。示例/創(chuàng)建主鍵列的schema,包括PK的個(gè)數(shù),名稱和類型/第一個(gè)PK列為整數(shù),名稱是pk0,這個(gè)同時(shí)也是分片列/第二個(gè)PK列為整數(shù),名稱是pk1TableMeta tableMeta = new TableMeta(SampleTable); tableMeta.addPrimaryK

16、eyColumn(pk0, PrimaryKeyType.INTEGER); tableMeta.addPrimaryKeyColumn(pk1, PrimaryKeyType.INTEGER);/ 將該表的讀寫CU都設(shè)置為0CapacityUnit capacityUnit = new CapacityUnit(0, 0);try創(chuàng)建一個(gè)有2個(gè)主鍵列,預(yù)留讀寫吞吐量為(0,0)的表。/ 構(gòu)造CreateTableRequest對(duì)象CreateTableRequest request = new CreateTableRequest();/ 調(diào)用client的createTable接口,如果沒

17、有拋出異常,則說明成功,否則失敗client.createTable(request);System.out.println(Create table succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Create table failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Create table failed.);提示:- 詳細(xì)代碼:createTableGitHub列出表名稱(List

18、Table)獲取當(dāng)前實(shí)例下已創(chuàng)建的所有表的表名。/*返回表(Table)名的列表。*return ListTable 操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效,或由于網(wǎng)絡(luò)原因請(qǐng)求失敗,或訪問超時(shí)。*/public ListTableResult listTable() throws OTSException, ClientException;接口示例try/ 調(diào)用listTable接口ListTableResult result = client.listTable();/ 打印table名稱

19、for (String tableName : result.getTableNames() System.out.println(tableName);獲取實(shí)例下所有表名System.out.println(List table succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(List table failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(List table failed.);提

20、示:- 詳細(xì)代碼:listTableGitHub更新表(UpdateTable)更新指定表的預(yù)留讀吞吐量或預(yù)留寫吞吐量設(shè)置/*更新表的讀或?qū)慍apacityUnit。*param updateTableRequest 執(zhí)行UpdateTable操作所需參數(shù)的封裝。return UpdateTable 操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public UpdateTableResult updateTable(UpdateTableRequest

21、 updateTableRequest) throws OTSException, ClientException;接口示例/ 設(shè)定新的預(yù)留讀吞吐量為1,寫吞吐量為2ReservedThroughputChange cuChange = new ReservedThroughputChange();/ 若想單獨(dú)調(diào)整寫CU,則無須設(shè)置讀CU cuChange.setReadCapacityUnit(1);/ 若想單獨(dú)調(diào)整讀CU,則無須設(shè)置寫CU cuChange.setWriteCapacityUnit(2);/ 構(gòu)造UpdateTableRequest對(duì)象UpdateTableRequest

22、request = new UpdateTableRequest(); request.setTableName(SampleTable); equest.setReservedThroughputChange(reservedThroughput);更新表的CU值為讀1,寫2try/ 調(diào)用接口更新表的預(yù)留讀寫吞吐量client.updateTable(request);/ 沒有拋出異常,則說明執(zhí)行成功System.out.println(Update table succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤Syste

23、m.out.println(Update table failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Update table failed.);提示:- 詳細(xì)代碼:updateTableGitHub查詢表描述信息(DescribeTable)查詢指定表的結(jié)構(gòu)信息和預(yù)留讀寫吞吐量設(shè)置信息。/*返回表(Table)的結(jié)構(gòu)信息。*param describeTableRequest 執(zhí)行DescribeTable操作所需參數(shù)的封裝。return DescribeTable 操作的響應(yīng)內(nèi)容。throws

24、 OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public DescribeTableResult describeTable(DescribeTableRequest describeTableRequest) throws OTSException, ClientException;接口示例try/ 構(gòu)造一個(gè)請(qǐng)求對(duì)象DescribeTableRequest request = new DescribeTableRequest(); request.setTableName(Sa

25、mpleTable);DescribeTableResult result = client.describeTable(request);獲取表的描述信息/ 打印SampleTable的描述信息TableMeta tableMeta = result.getTableMeta(); System.out.println(Table name: + tableMeta.getTableName(); System.out.println(Table Pk:);for (String keyName : tableMeta.getPrimaryKey().keySet() System.out.

26、println(keyName + : + tableMeta.getPrimaryKey().get(keyName);ReservedThroughputDetails reservedThroughputDetails = result.getReservedThroughputDetails(); System.out.println(Table read CU:+ reservedThroughputDetails.getCapacityUnit().getReadCapacityUnit(); System.out.println(Table write CU:+ reserved

27、ThroughputDetails.getCapacityUnit().getWriteCapacityUnit(); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Descibe table failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Describe row failed.);提示:- 詳細(xì)代碼:describeTableGitHub刪除表(DeleteTable)刪除本實(shí)例下指定的表。/*刪除表(Tab

28、le)。*param deleteTableRequest 執(zhí)行DeleteTable操作所需參數(shù)的封裝。return DeleteTable 操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public DeleteTableResult deleteTable(DeleteTableRequest deleteTableRequest) throws OTSException, ClientException;接口示例DeleteTableReques

29、t request = new DeleteTableRequest(); request.setTableName(SampleTable);刪除表try/ 刪除表client.deleteTable(request);/ 如果沒有拋出異常,則說明刪除成功System.out.println(Delete table succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Delete table failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,

30、則說明請(qǐng)求失敗System.out.println(Delete table failed.);提示:- 詳細(xì)代碼:deleteTableGitHub單行數(shù)據(jù)操作TableStore提供了PutRow,GetRow,UpdateRow和DeleteRow等單行操作的接口插入一行數(shù)據(jù)(PutRow)插入數(shù)據(jù)到指定的行。/*向表(Table)中插入或覆蓋一行數(shù)據(jù)。*param putRowRequest 執(zhí)行PutRow操作所需參數(shù)的封裝。return PutRow 操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返

31、回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public PutRowResult putRow(PutRowRequest putRowRequest) throws OTSException, ClientException;接口示例插入一行數(shù)據(jù)/ 定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致RowPrimaryKey primaryKey = new RowPrimaryKey(); primaryKey.addPrimaryKeyColumn(pk0, PrimaryKeyValue.fromLong(1); primaryKey.addPrimaryKey

32、Column(pk1, PrimaryKeyValue.fromLong(101);RowPutChange rowChange = new RowPutChange(SampleTable); rowChange.setPrimaryKey(primaryKey);/ 定義要寫入改行的屬性列rowChange.addAttributeColumn(col0, ColumnValue.fromLong(10); rowChange.addAttributeColumn(col1, ColumnValue.fromLong(111111); rowChange.addAttributeColum

33、n(col2, ColumnValue.fromString(北上杭深);/ RowExistenceExpectation.EXPECT_NOT_EXIST表示只有此行不存在時(shí)才會(huì)執(zhí)行插入rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);try/ 構(gòu)造插入數(shù)據(jù)的請(qǐng)求對(duì)象,PutRowRequest request = new PutRowRequest(); request.setRowChange(rowChange);/ 調(diào)用PutRow接口插入數(shù)據(jù)client.PutRow(re

34、quest);/ 如果沒有拋出異常,則說明執(zhí)行成功System.out.println(Put row succeeded.); catch (ClientException ex) System.out.println(Put row failed.); catch (OTSException ex) System.out.println(Put row failed.);提示:RowExistenceExpectation.IGNORE表示不管此行是否已經(jīng)存在,都會(huì)插入新數(shù)據(jù),如果之前有 會(huì)被覆蓋RowExistenceExpectation.EXPECT_EXIST表示只有此行存在時(shí),才

35、會(huì)插入新數(shù)據(jù),此時(shí),原有 數(shù)據(jù)也會(huì)被覆蓋RowExistenceExpectation.EXPECT_NOT_EXIST表示只有此行不存在時(shí),才會(huì)插入數(shù)據(jù),否則 不執(zhí)行詳細(xì)代碼:PutRowGitHub條件插入一行數(shù)據(jù)/ 定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致RowPrimaryKey primaryKey = new RowPrimaryKey(); primaryKey.addPrimaryKeyColumn(pk0, PrimaryKeyValue.fromLong(1); primaryKey.addPrimaryKeyColumn(pk1, PrimaryKey

36、Value.fromLong(101);下列示例演示:當(dāng)行存在,且col0小于5,且col2不等于beijing的時(shí)候才執(zhí)行插入操作RowPutChange rowChange = new RowPutChange(SampleTable); rowChange.setPrimaryKey(primaryKey);/ 定義要寫入改行的屬性列rowChange.addAttributeColumn(col0, ColumnValue.fromLong(10); rowChange.addAttributeColumn(col1, ColumnValue.fromLong(111111); row

37、Change.addAttributeColumn(col2, ColumnValue.fromString(北上杭深);try/條件1:col0的值小于5ColumnCondition filter1 = new RelationalCondition( col0, RelationalCondition.CompareOperator.LESS_THAN, ColumnValue.fromLong(5);/條件2:col2不等于beijing的行ColumnCondition filter2 = new RelationalCondition( col2, RelationalCondit

38、ion.CompareOperator.NOT_EQUAL, ColumnValue.fromString(beijing);/ 組合條件1和條件2,關(guān)系是ANDColumnCondition cond = new CompositeCondition(CompositeCondition.LogicOperator.AND).addCondition(filter1).addCondition(filter2);/ RowExistenceExpectation.EXPECT_NOT_EXIST表示只有此行不存在時(shí)才會(huì)執(zhí)行后面邏輯,否則直接返回Condition condition = ne

39、w Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);/ 設(shè)置列條件,只有col0小于5且col2不等于beijing的時(shí)候才會(huì)插入condition.setColumnCondition(cond);/ 設(shè)置條件rowChange.setCondition(cond);/ 構(gòu)造插入數(shù)據(jù)的請(qǐng)求對(duì)象PutRowRequest request = new PutRowRequest(); request.setRowChange(rowChange);/ 調(diào)用PutRow接口插入數(shù)據(jù) client.PutRow(request);/ 如果沒有拋

40、出異常,則說明執(zhí)行成功System.out.println(Put row succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Put row failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Put row failed.);讀取一行數(shù)據(jù)(GetRow)根據(jù)給定的主鍵讀取單行數(shù)據(jù)。/*返回表(Table)中的一行數(shù)據(jù)。*param getRowRequest 執(zhí)行GetRow操作所需參數(shù)的封裝

41、。return GetRow操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public GetRowResult getRow(GetRowRequest getRowRequest) throws OTSException, ClientException;接口示例/ 定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致RowPrimaryKey primaryKeys = new RowPrimaryKey(); primaryKeys.ad

42、dPrimaryKeyColumn(pk0, PrimaryKeyValue.fromLong(1);primaryKeys.addPrimaryKeyColumn(pk1, PrimaryKeyValue.fromLong(101);SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(SampleTable); criteria.setPrimaryKey(primaryKeys);try/ 構(gòu)造查詢請(qǐng)求對(duì)象,這里未指定讀哪列,默認(rèn)讀整行GetRowRequest request = new GetRowRequest()

43、; request.setRowQueryCriteria(criteria);/ 調(diào)用GetRow接口查詢數(shù)據(jù)GetRowResult result = client.getRow(request);/ 輸出此行的數(shù)據(jù)Row row = result.getRow();int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit(); System.out.println(Consumed capacity unti: + consumedReadCU); System.out.

44、println(col0: + row.getColumns().get(col0);System.out.println(col1: + row.getColumns().get(col1); System.out.println(col2: + row.getColumns().get(col2);/ 如果沒有拋出異常,則說明成功System.out.println(Get row succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Get row failed.); catch (O

45、TSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Get row failed.);讀取一行數(shù)據(jù)提示:查詢一行數(shù)據(jù)時(shí),默認(rèn)返回這一行所有列的數(shù)據(jù),如果想只返回特定行,可以通過criteria.addColumnsToGet接口限制,如果將col0和col1加入到columnsToGet中,則只返回col0和col1的值。查詢時(shí)也支持按條件過濾,比如當(dāng)col0的值大于24時(shí)才返回結(jié)果當(dāng)columnsToGet和condition同時(shí)使用時(shí),順序是columnsToGet先生效,然后再去返回的列 中去過濾當(dāng)某列不存在時(shí)的行為,可以通過Pa

46、ssIfMissing控制詳細(xì)代碼:GetRowGitHub過濾讀取一行數(shù)據(jù)/ 定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致RowPrimaryKey primaryKeys = new RowPrimaryKey(); primaryKeys.addPrimaryKeyColumn(pk0, PrimaryKeyValue.fromLong(1);primaryKeys.addPrimaryKeyColumn(pk1, PrimaryKeyValue.fromLong(101);SingleRowQueryCriteria criteria = new SingleRowQu

47、eryCriteria(SampleTable); criteria.setPrimaryKey(primaryKeys);try/條件1:col0的值等于24ColumnCondition filter1 = new RelationalCondition( col0, RelationalCondition.CompareOperator.EQUAL, ColumnValue.fromLong(24);/條件2:col2不等于chengdu的行ColumnCondition filter2 = new RelationalCondition( col1, RelationalConditi

48、on.CompareOperator.NOT_EQUAL, ColumnValue.fromString(chengdu);/ 組合條件1和條件2,關(guān)系是ORcriteria.setFilter(new CompositeCondition(CompositeCondition.LogicOperator.OR).addCondition(filter1).addCondition(filter2);/ 構(gòu)造查詢請(qǐng)求對(duì)象,這里未指定讀哪列,默認(rèn)讀整行GetRowRequest request = new GetRowRequest(); request.setRowQueryCriteria(

49、criteria);/ 調(diào)用GetRow接口查詢數(shù)據(jù)GetRowResult result = client.getRow(request);/ 輸出此行的數(shù)據(jù)Row row = result.getRow();下面演示查詢數(shù)據(jù),在col0和col2上面過濾,只有滿足條件是col0等于24,或者col2不等于chengdu的列才 會(huì)返回。int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit(); System.out.println(Consumed capacity un

50、ti: + consumedReadCU); System.out.println(col0: + row.getColumns().get(col0);System.out.println(col2: + row.getColumns().get(col2);/ 如果沒有拋出異常,則說明成功System.out.println(Get row succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Get row failed.); catch (OTSException ex) / 如果拋

51、出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Get row failed.);更新一行數(shù)據(jù)(UpdateRow)更新指定行的數(shù)據(jù),如果該行不存在,則新增一行;若該行存在,則根據(jù)請(qǐng)求的內(nèi)容在這一行中新增、修改或 者刪除指定列的值/*更新表的讀或?qū)慍apacityUnit。*param updateTableRequest 執(zhí)行UpdateTable操作所需參數(shù)的封裝。return UpdateTable操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗,

52、或訪問超時(shí)。*/public UpdateTableResult updateTable(UpdateTableRequest updateTableRequest) throws OTSException, ClientException;接口示例/ 定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致RowUpdateChange rowChange = new RowUpdateChange(SampleTable); RowPrimaryKey primaryKeys = new RowPrimaryKey(); primaryKeys.addPrimaryKeyColumn(

53、pk0, PrimaryKeyValue.fromLong(1); primaryKeys.addPrimaryKeyColumn(pk1, PrimaryKeyValue.fromLong(101); rowChange.setPrimaryKey(primaryKeys);/ 定義要寫入改行的屬性列rowChange.addAttributeColumn(col0, ColumnValue.fromLong(99); rowChange.addAttributeColumn(col2, ColumnValue.fromString(京滬杭深);/ 刪除col1更新一行數(shù)據(jù)rowChange

54、.deleteAttributeColumn(col1);rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_EXIST); try/ 構(gòu)造更新行的請(qǐng)求對(duì)象UpdateRowRequest request = new UpdateRowRequest(); request.setRowChange(rowChange);/ 調(diào)用UpdateRow接口執(zhí)行client.updateRow(request);/ 如果沒有拋出異常,則說明執(zhí)行成功System.out.println(Update row succee

55、ded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Update row failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Update row failed.);提示:更新一行數(shù)據(jù)也支持條件語句詳細(xì)代碼:UpdateRowGitHub刪除一行數(shù)據(jù)(DeleteRow)/*刪除表(Table)中的一行數(shù)據(jù)。*param deleteRowRequest 執(zhí)行DeleteRow操作所需參數(shù)的封裝。return

56、 DeleteRow操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問返回錯(cuò)誤消息throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public DeleteRowResult deleteRow(DeleteRowRequest deleteRowRequest) throws OTSException, ClientException;接口示例/ 要?jiǎng)h除的行的PK列分別為0和100刪除一行數(shù)據(jù)RowDeleteChange rowChange = new RowDeleteChange(SampleTable);

57、RowPrimaryKey primaryKeys = new RowPrimaryKey(); primaryKeys.addPrimaryKeyColumn(pk0, PrimaryKeyValue.fromLong(0); primaryKeys.addPrimaryKeyColumn(pk1, PrimaryKeyValue.fromLong(100); rowChange.setPrimaryKey(primaryKeys);try/ 構(gòu)造請(qǐng)求DeleteRowRequest request = new DeleteRowRequest(); request.setRowChange

58、(rowChange);/ 調(diào)用DeleteRow接口執(zhí)行刪除client.deleteRow(request);/ 如果沒有拋出異常,則表示成功System.out.println(Delete table succeeded.); catch (ClientException ex) / 如果拋出客戶端異常,則說明參數(shù)等有誤System.out.println(Delete row failed.); catch (OTSException ex) / 如果拋出服務(wù)器端異常,則說明請(qǐng)求失敗System.out.println(Delete row failed.);提示:刪除一行數(shù)據(jù)也支持

59、條件語句詳細(xì)代碼:DeleteRowGitHub多行數(shù)據(jù)操作批量讀(BatchGetRow)批量讀取一個(gè)或多個(gè)表中的若干行數(shù)據(jù)。BatchGetRow操作可視為多個(gè)GetRow操作的集合,各個(gè)操作獨(dú)立執(zhí) 行,獨(dú)立返回結(jié)果,獨(dú)立計(jì)算服務(wù)能力單元。與執(zhí)行大量的GetRow操作相比,使用BatchGetRow操作可以有 效減少請(qǐng)求的響應(yīng)時(shí)間,提高數(shù)據(jù)的讀取速率。/*從多張表(Table)中獲取多行數(shù)據(jù)。*param batchGetRowRequest 執(zhí)行BatchGetRow操作所需參數(shù)的封裝。return BatchGetRow操作的響應(yīng)內(nèi)容。throws OTSException OTS訪問

60、返回錯(cuò)誤消息接口* throws ClientException 請(qǐng)求的返回結(jié)果無效, 或由于網(wǎng)絡(luò)原因請(qǐng)求失敗, 或訪問超時(shí)。*/public BatchGetRowResult batchGetRow(BatchGetRowRequest batchGetRowRequest) throws OTSException, ClientException;示例try/ 構(gòu)造BatchGetRow對(duì)象和參數(shù)BatchGetRowRequest request = new BatchGetRowRequest(); MultiRowQueryCriteria tableRows = new Mult

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論