阅读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 参考_表格存储-阿里云