简体   繁体   中英

Update Value of Entire Column in HBase

I have a Hbase table with a specific column value for all rows as

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.

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 singleColumnValueFilter = new SingleColumnValueFilter("metadata".getBytes(),
               "collection-id".getBytes(),CompareOp.EQUAL,
               new BinaryComparator(Bytes.toBytes(1007)));

HBase supports update of records based on rowKey alone. 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]

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)));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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