开发者社区 问答 正文

Java-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.");
    }


提示:

删除一行数据(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.");
    }


提示:

展开
收起
云栖大讲堂 2017-10-26 09:59:25 2237 分享 版权
0 条回答
写回答
取消 提交回答