表格存储的 SDK 提供了 PutRow、GetRow、UpdateRow 和 DeleteRow 等单行操作的接口。
插入一行数据(PutRow)
插入数据到指定的行。
接口/**
* 向表(Table)中插入或覆盖一行数据。
*
* @param putRowRequest 执行PutRow操作所需参数的封装。
* @return PutRow 操作的响应内容。
* @throws OTSException OTS访问返回错误消息
* @throws ClientException 请求的返回结果无效, 或由于网络原因请求失败, 或访问超时。
*/
public PutRowResult putRow(PutRowRequest putRowRequest)
throws OTSException, ClientException;
示例 1
插入一行数据
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
RowPrimaryKey primaryKey = new RowPrimaryKey();
primaryKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
primaryKey.addPrimaryKeyColumn("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.addAttributeColumn("col2", ColumnValue.fromString("北上杭深"));
// RowExistenceExpectation.EXPECT_NOT_EXIST表示只有此行不存在时才会执行插入
rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST));
try
{
// 构造插入数据的请求对象,
PutRowRequest request = new PutRowRequest();
request.setRowChange(rowChange);
// 调用PutRow接口插入数据
client.PutRow(request);
// 如果没有抛出异常,则说明执行成功
System.out.println("Put row succeeded.");
} catch (ClientException ex) {
System.out.println("Put row failed.");
} catch (OTSException ex) {
System.out.println("Put row failed.");
}
提示:
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
RowPrimaryKey primaryKey = new RowPrimaryKey();
primaryKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
primaryKey.addPrimaryKeyColumn("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.addAttributeColumn("col2", ColumnValue.fromString("北上杭深"));
try
{
//条件1:col0的值小于5
ColumnCondition filter1 = new RelationalCondition(
"col0",
RelationalCondition.CompareOperator.LESS_THAN,
ColumnValue.fromLong(5));
//条件2:col2不等于beijing的行
ColumnCondition filter2 = new RelationalCondition(
"col2",
RelationalCondition.CompareOperator.NOT_EQUAL,
ColumnValue.fromString("beijing"));
// 组合条件1和条件2,关系是AND
ColumnCondition cond = new CompositeCondition(CompositeCondition.LogicOperator.AND)
.addCondition(filter1).addCondition(filter2);
// RowExistenceExpectation.EXPECT_NOT_EXIST表示只有此行不存在时才会执行后面逻辑,否则直接返回
Condition condition = new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);
// 设置列条件,只有col0小于5且col2不等于beijing的时候才会插入
condition.setColumnCondition(cond);
// 设置条件
rowChange.setCondition(cond);
// 构造插入数据的请求对象
PutRowRequest request = new PutRowRequest();
request.setRowChange(rowChange);
// 调用PutRow接口插入数据
client.PutRow(request);
// 如果没有抛出异常,则说明执行成功
System.out.println("Put row succeeded.");
} catch (ClientException ex) {
// 如果抛出客户端异常,则说明参数等有误
System.out.println("Put row failed.");
} catch (OTSException ex) {
// 如果抛出服务器端异常,则说明请求失败
System.out.println("Put row failed.");
}
/**
* 返回表(Table)中的一行数据。
*
* @param getRowRequest 执行GetRow操作所需参数的封装。
* @return GetRow操作的响应内容。
* @throws OTSException OTS访问返回错误消息
* @throws ClientException 请求的返回结果无效, 或由于网络原因请求失败, 或访问超时。
*/
public GetRowResult getRow(GetRowRequest getRowRequest)
throws OTSException, ClientException;
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
RowPrimaryKey primaryKeys = new RowPrimaryKey();
primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("SampleTable");
criteria.setPrimaryKey(primaryKeys);
try
{
// 构造查询请求对象,这里未指定读哪列,默认读整行
GetRowRequest request = new GetRowRequest();
request.setRowQueryCriteria(criteria);
// 调用GetRow接口查询数据
GetRowResult result = client.getRow(request);
// 输出此行的数据
Row row = result.getRow();
int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();
System.out.println("Consumed capacity unti:" + consumedReadCU);
System.out.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) {
// 如果抛出客户端异常,则说明参数等有误
System.out.println("Get row failed.");
} catch (OTSException ex) {
// 如果抛出服务器端异常,则说明请求失败
System.out.println("Get row failed.");
}
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
RowPrimaryKey primaryKeys = new RowPrimaryKey();
primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(101));
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("SampleTable");
criteria.setPrimaryKey(primaryKeys);
try
{
//条件1:col0的值等于24
ColumnCondition filter1 = new RelationalCondition(
"col0",
RelationalCondition.CompareOperator.EQUAL,
ColumnValue.fromLong(24));
//条件2:col2不等于chengdu的行
ColumnCondition filter2 = new RelationalCondition(
"col1",
RelationalCondition.CompareOperator.NOT_EQUAL,
ColumnValue.fromString("chengdu"));
// 组合条件1和条件2,关系是OR
criteria.setFilter(new CompositeCondition(CompositeCondition.LogicOperator.OR)
.addCondition(filter1).addCondition(filter2));
// 构造查询请求对象,这里未指定读哪列,默认读整行
GetRowRequest request = new GetRowRequest();
request.setRowQueryCriteria(criteria);
// 调用GetRow接口查询数据
GetRowResult result = client.getRow(request);
// 输出此行的数据
Row row = result.getRow();
int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();
System.out.println("Consumed capacity unti:" + 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) {
// 如果抛出客户端异常,则说明参数等有误
System.out.println("Get row failed.");
} catch (OTSException ex) {
// 如果抛出服务器端异常,则说明请求失败
System.out.println("Get row failed.");
}
/**
* 更新表的读或写CapacityUnit。
*
* @param updateTableRequest 执行UpdateTable操作所需参数的封装。
* @return UpdateTable操作的响应内容。
* @throws OTSException OTS访问返回错误消息
* @throws ClientException 请求的返回结果无效, 或由于网络原因请求失败, 或访问超时。
*/
public UpdateTableResult updateTable(UpdateTableRequest updateTableRequest)
throws OTSException, ClientException;
// 定义行的主键,必须与创建表时的TableMeta中定义的一致
RowUpdateChange rowChange = new RowUpdateChange("SampleTable");
RowPrimaryKey primaryKeys = new RowPrimaryKey();
primaryKeys.addPrimaryKeyColumn("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
rowChange.deleteAttributeColumn("col1");
rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_EXIST));
try
{
// 构造更新行的请求对象
UpdateRowRequest request = new UpdateRowRequest();
request.setRowChange(rowChange);
// 调用UpdateRow接口执行
client.updateRow(request);
// 如果没有抛出异常,则说明执行成功
System.out.println("Update row succeeded.");
} catch (ClientException ex) {
// 如果抛出客户端异常,则说明参数等有误
System.out.println("Update row failed.");
} catch (OTSException ex) {
// 如果抛出服务器端异常,则说明请求失败
System.out.println("Update row failed.");
}
/**
* 删除表(Table)中的一行数据。
*
* @param deleteRowRequest 执行DeleteRow操作所需参数的封装。
* @return DeleteRow操作的响应内容。
* @throws OTSException OTS访问返回错误消息
* @throws ClientException 请求的返回结果无效, 或由于网络原因请求失败, 或访问超时。
*/
public DeleteRowResult deleteRow(DeleteRowRequest deleteRowRequest)
throws OTSException, ClientException;
// 要删除的行的PK列分别为0和100
RowDeleteChange rowChange = new RowDeleteChange("SampleTable");
RowPrimaryKey primaryKeys = new RowPrimaryKey();
primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(0));
primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(100));
rowChange.setPrimaryKey(primaryKeys);
try
{
// 构造请求
DeleteRowRequest request = new DeleteRowRequest();
request.setRowChange(rowChange);
// 调用DeleteRow接口执行删除
client.deleteRow(request);
// 如果没有抛出异常,则表示成功
System.out.println("Delete table succeeded.");
} catch (ClientException ex) {
// 如果抛出客户端异常,则说明参数等有误
System.out.println("Delete row failed.");
} catch (OTSException ex) {
// 如果抛出服务器端异常,则说明请求失败
System.out.println("Delete row failed.");
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。