552
阿里云
单行数据操作__Java-SDK_SDK 参考_表格存储-阿里云
表格存储的 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.");
}
提示:
RowExistenceExpectation.IGNORE 表示不管此行是否已经存在,都会插入新数据,如果之前有会被覆盖。
RowExistenceExpectation.EXPECT_EXIST 表示只有此行存在时,才会插入新数据,此时,原有数据也会被覆盖。
RowExistenceExpectation.EXPECT_NOT_EXIST 表示只有此行不存在时,才会插入数据,否则不执行。
详细代码:PutRow@GitHub。
示例 2
根据条件插入一行数据:当行存在,且 col0 小于 5,且 col2 不等于“beijing”的时候才执行插入操作。
// 定义行的主键,必须与创建表时的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.");
}
读取一行数据(GetRow)
根据给定的主键读取单行数据。
接口
/**
* 返回表(Table)中的一行数据。
*
* @param getRowRequest 执行GetRow操作所需参数的封装。
* @return GetRow操作的响应内容。
* @throws OTSException OTS访问返回错误消息
* @throws ClientException 请求的返回结果无效, 或由于网络原因请求失败, 或访问超时。
*/
public GetRowResult getRow(GetRowRequest getRowRequest)
throws OTSException, ClientException;
示例 1
读取一行数据。
// 定义行的主键,必须与创建表时的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.");
}
提示:
查询一行数据时,默认返回这一行所有列的数据,如果想只返回特定行,可以通过 criteria.addColumnsToGet 接口限制,如果将 col0 和 col1 加入到 columnsToGet 中,则只返回 col0 和 col1 的值。
查询时也支持按条件过滤,比如当 col0 的值大于 24 时才返回结果。
当同时使用 columnsToGet 和 condition 时,顺序是 columnsToGet 先生效,然后再去返回的列中进行过滤。
某列不存在时的行为,可以通过 PassIfMissing 控制。
详细代码:GetRow@GitHub。
示例 2
使用过滤功能读取一行数据。
下面演示查询数据,在 col0 和 col2 上面过滤,只有满足条件是 col0 等于24,或者 col2 不等于“chengdu”的列才会返回。
// 定义行的主键,必须与创建表时的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.");
}
更新一行数据(UpdateRow)
更新指定行的数据,如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。
接口
/**
* 更新表的读或写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.");
}
提示:
更新一行数据也支持条件语句。
详细代码:UpdateRow@GitHub。
删除一行数据(DeleteRow)
接口
/**
* 删除表(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.");
}
提示:
删除一行数据也支持条件语句。
详细代码:DeleteRow@GitHub。
最后更新:2016-11-23 16:04:09
上一篇:
UpdateRowInBatchWriteRowRequest__DataType_API 参考_表格存储-阿里云
下一篇:
多行数据操作__Java-SDK_SDK 参考_表格存储-阿里云
批量删除消息__队列接口规范_API使用手册_消息服务-阿里云
配置数据导入任务__基础示例—完整数据开发_场景教程_大数据开发套件-阿里云
获取应用列表__应用管理类 API_Open API 参考_企业级分布式应用服务 EDAS-阿里云
修改实例描述__实例管理_开放API_分布式关系型数据库 DRDS-阿里云
开发准备___开发人员指南_E-MapReduce-阿里云
Array数组类型说明__功能篇_最佳实践_开放搜索-阿里云
EDAS 产品系列__产品系列及发行说明_企业级分布式应用服务 EDAS-阿里云
视频转码回调消息讲解___视频专区_媒体转码-阿里云
获取访问者真实IP___常见接入问题_Web 应用防火墙-阿里云
计费问题__计量计费_弹性伸缩-阿里云
相关内容
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云