【问题标题】:Update Value of Entire Column in HBase更新 HBase 中整列的值
【发布时间】:2018-06-15 16:36:35
【问题描述】:

我有一个 Hbase 表,其中所有行的特定列值为

901877853087813636      column=metadata:collection-id, timestamp=1514594631532, value=1007

现在如何将表中所有行的值从 1007 更改为 1008。

所有帮助点都指向修改特定行。

请帮帮我

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 我在想是否有任何命令可以针对我们给出的特定行执行 `Put p = new Put(Bytes.toBytes("row1")); // 更新单元格值 p.add(Bytes.toBytes("personal"), Bytes.toBytes("city"),Bytes.toBytes("Delih")); // 将 put Instance 保存到 HTable。 hTable.put(p);`
  • @Subash Kunjupillai 你能帮帮我吗

标签: java scala hadoop hbase bigdata


【解决方案1】:

使用 SingleColumnValueFilter 扫描表以获取 value 所在的所有行 1007,然后您可以使用批量放置为所有这些行放置新值(1008)。例如,扫描放置过滤器,如下所示:

 SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("metadata".getBytes(),
               "collection-id".getBytes(),CompareOp.EQUAL,
               new BinaryComparator(Bytes.toBytes(1007)));

【讨论】:

    【解决方案2】:

    HBase 支持仅基于 rowKey 更新记录。因此,我们必须获取所有需要更新的记录并创建我们自己的批处理来更新这些记录,如下所示。

    UpdateAllRows [tableName] [columnQualifier] [columnFamily] [oldValue] [newValue]

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Admin;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.hbase.client.Table;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class UpdateAllRows {
    
        private static Connection conn;
        public static Admin getConnection() throws IOException {
            if (conn == null) {
                conn = ConnectionFactory.createConnection(HBaseConfiguration.create());
            }
            return conn.getAdmin();
        }
    
        public static void main(String args[]) throws Exception {
            getConnection();
            updateTable(args[0], args[1], args[2], args[3], args[4]);
        }
    
    
        public static void updateTable(String tableName, String columnFamily, String columnQualifier, String oldValue, String newValue) throws Exception{
            Table table = conn.getTable(TableName.valueOf(tableName));
            ResultScanner rs = scan(tableName, columnFamily, columnQualifier);
    
            byte[] cfBytes = Bytes.toBytes(columnFamily);
            byte[] cqBytes = Bytes.toBytes(columnQualifier);
            byte[] oldvalBytes = Bytes.toBytes(oldValue);
            byte[] newvalBytes = Bytes.toBytes(newValue);
    
            Result res = null;
            List<Put> putList = new ArrayList<Put>();
            try {
                while ((res = rs.next()) != null) {
                    if (Arrays.equals(res.getValue(cfBytes, cqBytes), oldvalBytes)){
                        Put p = new Put(res.getRow());
                        p.addColumn(cfBytes, cqBytes, newvalBytes);
                        putList.add(p);
                    }
                }
            } finally {
                rs.close();
            }
            table.put(putList);
            table.close();
        }
    
        public static ResultScanner scan(String tableName, String columnFamily, String columnQualifier) throws IOException {
            Table table = conn.getTable(TableName.valueOf(tableName));
            return  table.getScanner(new Scan().addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多