zoukankan      html  css  js  c++  java
  • 【HBase】+Java+批量增查数据



    https://help.aliyun.com/document_detail/43017.html?spm=a2c4g.11186623.6.762.22f87e748D8rpv

    1、批量查询数据

    import com.alicloud.openservices.tablestore.SyncClient;
    import com.alicloud.openservices.tablestore.model.*;
    import com.alicloud.openservices.tablestore.model.filter.SingleColumnValueFilter;
    
    import java.util.List;
    
    public class batchSearchData {
        /**
         * @param client
         * @param tableName      表名
         * @param primaryKeyName 主键名称
         * @param rowCount       要读取的行数
         * @param colName        要读取的列名称
         */
        private void batchGetRow(SyncClient client, String tableName, String primaryKeyName, int rowCount, List<String> colName) {
            MultiRowQueryCriteria multiRowQueryCriteria = new MultiRowQueryCriteria(tableName);
            // 加入要读取的行
            for (int i = 0; i < rowCount; i++) {
                PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
                primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString("pk" + i));
                PrimaryKey primaryKey = primaryKeyBuilder.build();
                multiRowQueryCriteria.addRow(primaryKey);
            }
            // 添加条件
            multiRowQueryCriteria.setMaxVersions(1);
            for (int i = 0; i < colName.size(); i++) {
                multiRowQueryCriteria.addColumnsToGet(colName.get(i));
            }
            SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(colName.get(0),
                    SingleColumnValueFilter.CompareOperator.EQUAL, ColumnValue.fromLong(0));
            singleColumnValueFilter.setPassIfMissing(false);
            multiRowQueryCriteria.setFilter(singleColumnValueFilter);
    
            BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
            // batchGetRow支持读取多个表的数据, 一个multiRowQueryCriteria对应一个表的查询条件, 可以添加多个multiRowQueryCriteria.
            batchGetRowRequest.addMultiRowQueryCriteria(multiRowQueryCriteria);
    
            BatchGetRowResponse batchGetRowResponse = client.batchGetRow(batchGetRowRequest);
    
            System.out.println("是否全部成功:" + batchGetRowResponse.isAllSucceed());
            if (!batchGetRowResponse.isAllSucceed()) {
                for (BatchGetRowResponse.RowResult rowResult : batchGetRowResponse.getFailedRows()) {
                    System.out.println("失败的行:" + batchGetRowRequest.getPrimaryKey(rowResult.getTableName(), rowResult.getIndex()));
                    System.out.println("失败原因:" + rowResult.getError());
                }
    
                /**
                 * 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试.这里只给出构造重试请求的部分.
                 * 推荐的重试方法是使用SDK的自定义重试策略功能, 支持对batch操作的部分行错误进行重试. 设定重试策略后, 调用接口处即不需要增加重试代码.
                 */
                BatchGetRowRequest retryRequest = batchGetRowRequest.createRequestForRetry(batchGetRowResponse.getFailedRows());
            }
        }
    }

    2、批量插入数据

    待完善(要添加哪些列?造哪些列的数据)
    import com.alicloud.openservices.tablestore.SyncClient;
    import com.alicloud.openservices.tablestore.model.*;
    
    public class batchWriteData {
        /**
         * @param client
         * @param tableName      表名
         * @param primaryKeyName 主键名称
         *                       备注:待完善(要添加哪些列?造哪些列的数据)
         */
        private void batchWriteRow(SyncClient client, String tableName, String primaryKeyName) {
            BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
    
            // 构造rowPutChange1
            PrimaryKeyBuilder pk1Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            pk1Builder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString("pk1"));
            RowPutChange rowPutChange1 = new RowPutChange(tableName, pk1Builder.build());
            // 添加一些列
            for (int i = 0; i < 10; i++) {
                rowPutChange1.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
            }
            // 添加到batch操作中
            batchWriteRowRequest.addRowChange(rowPutChange1);
    
            // 构造rowPutChange2
            PrimaryKeyBuilder pk2Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            pk2Builder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString("pk2"));
            RowPutChange rowPutChange2 = new RowPutChange(tableName, pk2Builder.build());
            // 添加一些列
            for (int i = 0; i < 10; i++) {
                rowPutChange2.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
            }
            // 添加到batch操作中
            batchWriteRowRequest.addRowChange(rowPutChange2);
    
            // 构造rowUpdateChange
            PrimaryKeyBuilder pk3Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            pk3Builder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString("pk3"));
            RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, pk3Builder.build());
            // 添加一些列
            for (int i = 0; i < 10; i++) {
                rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i)));
            }
            // 删除一列
            rowUpdateChange.deleteColumns("Col10");
            // 添加到batch操作中
            batchWriteRowRequest.addRowChange(rowUpdateChange);
    
            // 构造rowDeleteChange
            PrimaryKeyBuilder pk4Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            pk4Builder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString("pk4"));
            RowDeleteChange rowDeleteChange = new RowDeleteChange(tableName, pk4Builder.build());
            // 添加到batch操作中
            batchWriteRowRequest.addRowChange(rowDeleteChange);
    
            BatchWriteRowResponse response = client.batchWriteRow(batchWriteRowRequest);
    
            System.out.println("是否全部成功:" + response.isAllSucceed());
            if (!response.isAllSucceed()) {
                for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
                    System.out.println("失败的行:" + batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
                    System.out.println("失败原因:" + rowResult.getError());
                }
                /**
                 * 可以通过createRequestForRetry方法再构造一个请求对失败的行进行重试.这里只给出构造重试请求的部分.
                 * 推荐的重试方法是使用SDK的自定义重试策略功能, 支持对batch操作的部分行错误进行重试. 设定重试策略后, 调用接口处即不需要增加重试代码.
                 */
                BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
            }
        }
    }
  • 相关阅读:
    个人项目:Java实现WC
    第六篇Scrum冲刺博客
    java实现小学生四则运算
    实现wc部分功能 java
    WordCount作业修改
    我的过去、现在和未来
    WordCount
    Window 通过cmd查看端口占用、相应进程、杀死进程等的命令
    《《《在同一台服务器上配置多个Tomcat
    关于Android 5.x的低功耗蓝牙BLE开发简介
  • 原文地址:https://www.cnblogs.com/danhuai/p/11537814.html
Copyright © 2011-2022 走看看