閱讀552 返回首頁    go 微信


單行數據操作__Java-SDK_SDK 參考_表格存儲-阿裏雲

表格存儲的 SDK 提供了 PutRow、GetRow、UpdateRow 和 DeleteRow 等單行操作的接口。

插入一行數據(PutRow)

插入數據到指定的行。

接口

  1. /**
  2. * 向表(Table)中插入或覆蓋一行數據。
  3. *
  4. * @param putRowRequest 執行PutRow操作所需參數的封裝。
  5. * @return PutRow 操作的響應內容。
  6. * @throws OTSException OTS訪問返回錯誤消息
  7. * @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
  8. */
  9. public PutRowResult putRow(PutRowRequest putRowRequest)
  10. throws OTSException, ClientException;

示例 1

插入一行數據

  1. // 定義行的主鍵,必須與創建表時的TableMeta中定義的一致
  2. RowPrimaryKey primaryKey = new RowPrimaryKey();
  3. primaryKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
  4. primaryKey.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
  5. RowPutChange rowChange = new RowPutChange("SampleTable");
  6. rowChange.setPrimaryKey(primaryKey);
  7. // 定義要寫入改行的屬性列
  8. rowChange.addAttributeColumn("col0", ColumnValue.fromLong(10));
  9. rowChange.addAttributeColumn("col1", ColumnValue.fromLong(111111));
  10. rowChange.addAttributeColumn("col2", ColumnValue.fromString("北上杭深"));
  11. // RowExistenceExpectation.EXPECT_NOT_EXIST表示隻有此行不存在時才會執行插入
  12. rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST));
  13. try
  14. {
  15. // 構造插入數據的請求對象,
  16. PutRowRequest request = new PutRowRequest();
  17. request.setRowChange(rowChange);
  18. // 調用PutRow接口插入數據
  19. client.PutRow(request);
  20. // 如果沒有拋出異常,則說明執行成功
  21. System.out.println("Put row succeeded.");
  22. } catch (ClientException ex) {
  23. System.out.println("Put row failed.");
  24. } catch (OTSException ex) {
  25. System.out.println("Put row failed.");
  26. }

提示:

  • RowExistenceExpectation.IGNORE 表示不管此行是否已經存在,都會插入新數據,如果之前有會被覆蓋。

  • RowExistenceExpectation.EXPECT_EXIST 表示隻有此行存在時,才會插入新數據,此時,原有數據也會被覆蓋。

  • RowExistenceExpectation.EXPECT_NOT_EXIST 表示隻有此行不存在時,才會插入數據,否則不執行。

  • 詳細代碼:PutRow@GitHub

示例 2

根據條件插入一行數據:當行存在,且 col0 小於 5,且 col2 不等於“beijing”的時候才執行插入操作。

  1. // 定義行的主鍵,必須與創建表時的TableMeta中定義的一致
  2. RowPrimaryKey primaryKey = new RowPrimaryKey();
  3. primaryKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
  4. primaryKey.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
  5. RowPutChange rowChange = new RowPutChange("SampleTable");
  6. rowChange.setPrimaryKey(primaryKey);
  7. // 定義要寫入改行的屬性列
  8. rowChange.addAttributeColumn("col0", ColumnValue.fromLong(10));
  9. rowChange.addAttributeColumn("col1", ColumnValue.fromLong(111111));
  10. rowChange.addAttributeColumn("col2", ColumnValue.fromString("北上杭深"));
  11. try
  12. {
  13. //條件1:col0的值小於5
  14. ColumnCondition filter1 = new RelationalCondition(
  15. "col0",
  16. RelationalCondition.CompareOperator.LESS_THAN,
  17. ColumnValue.fromLong(5));
  18. //條件2:col2不等於beijing的行
  19. ColumnCondition filter2 = new RelationalCondition(
  20. "col2",
  21. RelationalCondition.CompareOperator.NOT_EQUAL,
  22. ColumnValue.fromString("beijing"));
  23. // 組合條件1和條件2,關係是AND
  24. ColumnCondition cond = new CompositeCondition(CompositeCondition.LogicOperator.AND)
  25. .addCondition(filter1).addCondition(filter2);
  26. // RowExistenceExpectation.EXPECT_NOT_EXIST表示隻有此行不存在時才會執行後麵邏輯,否則直接返回
  27. Condition condition = new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);
  28. // 設置列條件,隻有col0小於5且col2不等於beijing的時候才會插入
  29. condition.setColumnCondition(cond);
  30. // 設置條件
  31. rowChange.setCondition(cond);
  32. // 構造插入數據的請求對象
  33. PutRowRequest request = new PutRowRequest();
  34. request.setRowChange(rowChange);
  35. // 調用PutRow接口插入數據
  36. client.PutRow(request);
  37. // 如果沒有拋出異常,則說明執行成功
  38. System.out.println("Put row succeeded.");
  39. } catch (ClientException ex) {
  40. // 如果拋出客戶端異常,則說明參數等有誤
  41. System.out.println("Put row failed.");
  42. } catch (OTSException ex) {
  43. // 如果拋出服務器端異常,則說明請求失敗
  44. System.out.println("Put row failed.");
  45. }

讀取一行數據(GetRow)

根據給定的主鍵讀取單行數據。

接口

  1. /**
  2. * 返回表(Table)中的一行數據。
  3. *
  4. * @param getRowRequest 執行GetRow操作所需參數的封裝。
  5. * @return GetRow操作的響應內容。
  6. * @throws OTSException OTS訪問返回錯誤消息
  7. * @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
  8. */
  9. public GetRowResult getRow(GetRowRequest getRowRequest)
  10. throws OTSException, ClientException;

示例 1

讀取一行數據。

  1. // 定義行的主鍵,必須與創建表時的TableMeta中定義的一致
  2. RowPrimaryKey primaryKeys = new RowPrimaryKey();
  3. primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
  4. primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
  5. SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("SampleTable");
  6. criteria.setPrimaryKey(primaryKeys);
  7. try
  8. {
  9. // 構造查詢請求對象,這裏未指定讀哪列,默認讀整行
  10. GetRowRequest request = new GetRowRequest();
  11. request.setRowQueryCriteria(criteria);
  12. // 調用GetRow接口查詢數據
  13. GetRowResult result = client.getRow(request);
  14. // 輸出此行的數據
  15. Row row = result.getRow();
  16. int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();
  17. System.out.println("Consumed capacity unti:" + consumedReadCU);
  18. System.out.println("col0:" + row.getColumns().get("col0"));
  19. System.out.println("col1:" + row.getColumns().get("col1"));
  20. System.out.println("col2:" + row.getColumns().get("col2"));
  21. // 如果沒有拋出異常,則說明成功
  22. System.out.println("Get row succeeded.");
  23. } catch (ClientException ex) {
  24. // 如果拋出客戶端異常,則說明參數等有誤
  25. System.out.println("Get row failed.");
  26. } catch (OTSException ex) {
  27. // 如果拋出服務器端異常,則說明請求失敗
  28. System.out.println("Get row failed.");
  29. }

提示:

  • 查詢一行數據時,默認返回這一行所有列的數據,如果想隻返回特定行,可以通過 criteria.addColumnsToGet 接口限製,如果將 col0 和 col1 加入到 columnsToGet 中,則隻返回 col0 和 col1 的值。

  • 查詢時也支持按條件過濾,比如當 col0 的值大於 24 時才返回結果。

  • 當同時使用 columnsToGet 和 condition 時,順序是 columnsToGet 先生效,然後再去返回的列中進行過濾。

  • 某列不存在時的行為,可以通過 PassIfMissing 控製。

  • 詳細代碼:GetRow@GitHub

示例 2

使用過濾功能讀取一行數據。

下麵演示查詢數據,在 col0 和 col2 上麵過濾,隻有滿足條件是 col0 等於24,或者 col2 不等於“chengdu”的列才會返回。

  1. // 定義行的主鍵,必須與創建表時的TableMeta中定義的一致
  2. RowPrimaryKey primaryKeys = new RowPrimaryKey();
  3. primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
  4. primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
  5. SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("SampleTable");
  6. criteria.setPrimaryKey(primaryKeys);
  7. try
  8. {
  9. //條件1:col0的值等於24
  10. ColumnCondition filter1 = new RelationalCondition(
  11. "col0",
  12. RelationalCondition.CompareOperator.EQUAL,
  13. ColumnValue.fromLong(24));
  14. //條件2:col2不等於chengdu的行
  15. ColumnCondition filter2 = new RelationalCondition(
  16. "col1",
  17. RelationalCondition.CompareOperator.NOT_EQUAL,
  18. ColumnValue.fromString("chengdu"));
  19. // 組合條件1和條件2,關係是OR
  20. criteria.setFilter(new CompositeCondition(CompositeCondition.LogicOperator.OR)
  21. .addCondition(filter1).addCondition(filter2));
  22. // 構造查詢請求對象,這裏未指定讀哪列,默認讀整行
  23. GetRowRequest request = new GetRowRequest();
  24. request.setRowQueryCriteria(criteria);
  25. // 調用GetRow接口查詢數據
  26. GetRowResult result = client.getRow(request);
  27. // 輸出此行的數據
  28. Row row = result.getRow();
  29. int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();
  30. System.out.println("Consumed capacity unti:" + consumedReadCU);
  31. System.out.println("col0:" + row.getColumns().get("col0"));
  32. System.out.println("col2:" + row.getColumns().get("col2"));
  33. // 如果沒有拋出異常,則說明成功
  34. System.out.println("Get row succeeded.");
  35. } catch (ClientException ex) {
  36. // 如果拋出客戶端異常,則說明參數等有誤
  37. System.out.println("Get row failed.");
  38. } catch (OTSException ex) {
  39. // 如果拋出服務器端異常,則說明請求失敗
  40. System.out.println("Get row failed.");
  41. }

更新一行數據(UpdateRow)

更新指定行的數據,如果該行不存在,則新增一行;若該行存在,則根據請求的內容在這一行中新增、修改或者刪除指定列的值。

接口

  1. /**
  2. * 更新表的讀或寫CapacityUnit。
  3. *
  4. * @param updateTableRequest 執行UpdateTable操作所需參數的封裝。
  5. * @return UpdateTable操作的響應內容。
  6. * @throws OTSException OTS訪問返回錯誤消息
  7. * @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
  8. */
  9. public UpdateTableResult updateTable(UpdateTableRequest updateTableRequest)
  10. throws OTSException, ClientException;

示例

更新一行數據。

  1. // 定義行的主鍵,必須與創建表時的TableMeta中定義的一致
  2. RowUpdateChange rowChange = new RowUpdateChange("SampleTable");
  3. RowPrimaryKey primaryKeys = new RowPrimaryKey();
  4. primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
  5. primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
  6. rowChange.setPrimaryKey(primaryKeys);
  7. // 定義要寫入改行的屬性列
  8. rowChange.addAttributeColumn("col0", ColumnValue.fromLong(99));
  9. rowChange.addAttributeColumn("col2", ColumnValue.fromString("京滬杭深"));
  10. // 刪除col1
  11. rowChange.deleteAttributeColumn("col1");
  12. rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_EXIST));
  13. try
  14. {
  15. // 構造更新行的請求對象
  16. UpdateRowRequest request = new UpdateRowRequest();
  17. request.setRowChange(rowChange);
  18. // 調用UpdateRow接口執行
  19. client.updateRow(request);
  20. // 如果沒有拋出異常,則說明執行成功
  21. System.out.println("Update row succeeded.");
  22. } catch (ClientException ex) {
  23. // 如果拋出客戶端異常,則說明參數等有誤
  24. System.out.println("Update row failed.");
  25. } catch (OTSException ex) {
  26. // 如果拋出服務器端異常,則說明請求失敗
  27. System.out.println("Update row failed.");
  28. }

提示:

刪除一行數據(DeleteRow)

接口

  1. /**
  2. * 刪除表(Table)中的一行數據。
  3. *
  4. * @param deleteRowRequest 執行DeleteRow操作所需參數的封裝。
  5. * @return DeleteRow操作的響應內容。
  6. * @throws OTSException OTS訪問返回錯誤消息
  7. * @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
  8. */
  9. public DeleteRowResult deleteRow(DeleteRowRequest deleteRowRequest)
  10. throws OTSException, ClientException;

示例

刪除一行數據。

  1. // 要刪除的行的PK列分別為0和100
  2. RowDeleteChange rowChange = new RowDeleteChange("SampleTable");
  3. RowPrimaryKey primaryKeys = new RowPrimaryKey();
  4. primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(0));
  5. primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(100));
  6. rowChange.setPrimaryKey(primaryKeys);
  7. try
  8. {
  9. // 構造請求
  10. DeleteRowRequest request = new DeleteRowRequest();
  11. request.setRowChange(rowChange);
  12. // 調用DeleteRow接口執行刪除
  13. client.deleteRow(request);
  14. // 如果沒有拋出異常,則表示成功
  15. System.out.println("Delete table succeeded.");
  16. } catch (ClientException ex) {
  17. // 如果拋出客戶端異常,則說明參數等有誤
  18. System.out.println("Delete row failed.");
  19. } catch (OTSException ex) {
  20. // 如果拋出服務器端異常,則說明請求失敗
  21. System.out.println("Delete row failed.");
  22. }

提示:

最後更新:2016-11-23 16:04:09

  上一篇:go UpdateRowInBatchWriteRowRequest__DataType_API 參考_表格存儲-阿裏雲
  下一篇:go 多行數據操作__Java-SDK_SDK 參考_表格存儲-阿裏雲