Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HBase RowMutations to replace all columns of a row

Tags:

atomic

hbase

I have a HBase (v0.94.7) table with a single column family and columns are added to it over time. These columns are named as the timestamp they were created, so unless I query the row I do not know what all columns it has.

Now given a row, I want to atomically remove all the existing columns of this column family and add a new set of columns and values.

So I thought of using HBase's RowMutations like:

RowMutations mutations = new RowMutations(row);

//delete the column family
Delete delete = new Delete(row);
delete.deleteFamily(cf);

//add new columns
Put put = new Put(row);
put.add(cf, col1, v1);
put.add(cf, col2, v2);

//delete column family and add new columns to same family
mutations.add(delete);
mutations.add(put);

table.mutateRow(mutations);

But what this code ends up doing is just deleting the column family, it does not add the new columns. Is this behaviour expected?

If so, then how can I achieve my goal of atomically replacing all columns of a column family with a new set of columns?

Here is a test case for the same:

import junit.framework.Assert;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.NavigableMap;

public class TestHBaseRowMutations {
    static String tableName = "nnn";
    static byte[] cf1 = Bytes.toBytes("cf1");
    static byte[] row = Bytes.toBytes("r1");
    static HTablePool hTablePool;

    @BeforeClass
    public static void beforeClass() throws Exception {
        Configuration config = HBaseConfiguration.create();
        hTablePool = new HTablePool(config, Integer.MAX_VALUE);
        HBaseAdmin admin = new HBaseAdmin(config);
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        tableDescriptor.addFamily(new HColumnDescriptor(cf1));
        try {
            admin.createTable(tableDescriptor);
        } catch (TableExistsException ignored){}
    }

    @Before
    public void before() throws Exception {
        HTableInterface table = hTablePool.getTable(tableName);
        try {
            Delete delete = new Delete(row);
            table.delete(delete);
            System.out.println("deleted old row");

            Put put = new Put(row);
            put.add(cf1, Bytes.toBytes("c1"), Bytes.toBytes("v1"));
            put.add(cf1, Bytes.toBytes("c11"), Bytes.toBytes("v11"));
            table.put(put);
            System.out.println("Created row with seed data");
        } finally {
            table.close();
        }
    }


    @Test
    public void testColumnFamilyDeleteRM() throws Exception {
        HTableInterface table = hTablePool.getTable(tableName);
        try {
            RowMutations rm =new RowMutations(row);

            //delete column family cf1
            Delete delete = new Delete(row);
            delete.deleteFamily(cf1);
            rm.add(delete);
            System.out.println("Added delete of cf1 column family to row mutation");

            //add new columns to same column family cf1
            Put put = new Put(row);
            put.add(cf1, Bytes.toBytes("c1"), Bytes.toBytes("new_v1"));
            put.add(cf1, Bytes.toBytes("c11"), Bytes.toBytes("new_v11"));
            rm.add(put);
            System.out.println("Added puts of cf1 column family to row mutation");

            //atomic mutate the row
            table.mutateRow(rm);
            System.out.println("Mutated row");

            //now read the column family cf1 back
            Result result = table.get(new Get(row));
            NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(cf1);

            //column family cf1 should have 2 columns because of the Put above
            //------Following assert fails as cf1 does not exist anymore, why does cf1 not exist anymore?-------
            Assert.assertNotNull(familyMap);
            Assert.assertEquals(2, familyMap.size());
        } finally {
            table.close();
        }
    }
}
like image 495
vinodv26 Avatar asked May 26 '13 01:05

vinodv26


2 Answers

Posted the same question on HBase user forum and turns out this is a bug in HBase.

The expected behaviour is that if a RowMutation has a Delete to some column-family/column/row followed by a Put to same column-family/column/row, the Put should also be honoured (but this is not the case currently).

HBase user group discussion on this: http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-td4045247.html

HBase JIRA for the same: https://issues.apache.org/jira/browse/HBASE-8626 which also provides patch.

like image 160
vinodv26 Avatar answered Sep 28 '22 05:09

vinodv26


The closest one can do is set the timestamp on the Put to be higher than on the Delete:

long now = System.currentTimeMillis();

Delete delete = new Delete(row);
delete.deleteFamily(cf1, now);

Put put = new Put(row);
put.add(cf1, col1, now + 1);

RowMutations mutations = new RowMutations(row);
mutations.add(delete);
mutations.add(put);

table.mutateRow(mutations);

Sadly, it does mean that a get to the timestamp 'now' will have nothing in that column family. Source

like image 26
EmDash Avatar answered Sep 28 '22 03:09

EmDash