简体   繁体   English

HBase中整个列的更新值

[英]Update Value of Entire Column in HBase

I have a Hbase table with a specific column value for all rows as 我有一个Hbase表,其中所有行的列值都特定

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

Now how do I change the value from 1007 to 1008 for all rows in the table. 现在,如何将表中所有行的值从1007更改为1008。

All the help points to modifying a specific row. 所有帮助都指向修改特定行。

Please help me in this 请帮我

scan the table with SingleColumnValueFilter to get all the rows where value is 1007 and than u can put new value(1008) for all those rows using bulk put.for example to scan put the filter like this: 使用SingleColumnValueFilter扫描表以获取value为1007的所有行,然后您可以使用批量put为所有这些行添加新的value(1008)。例如,扫描put这样的过滤器:

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

HBase supports update of records based on rowKey alone. HBase仅支持基于rowKey的记录更新。 So, we have to fetch all the records which have to be updated and create our own batch put to update those records something like below. 因此,我们必须获取所有需要更新的记录,并创建我们自己的批处理存储以更新这些记录,如下所示。

UpdateAllRows [tableName] [columnQualifier] [columnFamily] [oldValue] [newValue] 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)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM