You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by Vinod V <vi...@gmail.com> on 2013/05/26 01:53:48 UTC

Using RowMutations to replace all columns of a row

I have a HBase 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 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?

Thanks,
Vinod

Re: Using RowMutations to replace all columns of a row

Posted by Ted Yu <yu...@gmail.com>.
Over in HBASE-8626, Jean-Marc and Andrew voiced the opinion that the
current behavior may not be a bug.

Vinod:

What do you think ?

On Sat, May 25, 2013 at 4:53 PM, Vinod V <vi...@gmail.com> wrote:

> I have a HBase 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 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?
>
> Thanks,
> Vinod
>

Re: Using RowMutations to replace all columns of a row

Posted by Vinod V <vi...@gmail.com>.
Ted,

Here is a patch of the unit tests for trunk:

https://issues.apache.org/jira/secure/attachment/12584875/tests_for_row_mutations1.patch

Thanks!

On Sun, May 26, 2013 at 9:24 PM, Ted Yu <yu...@gmail.com> wrote:

> Vinod:
> Are you able to generate trunk patch for your test ?
>
> Please refer to http://wiki.apache.org/hadoop/Hbase/HowToContribute
>
> Please add license header and remove the following:
>
>  * Created with IntelliJ IDEA.
>  * User: vinodvr
>  * Date: 26/5/13
>  * Time: 7:07 PM
>
> On Sun, May 26, 2013 at 8:02 AM, Vinod V <vi...@gmail.com> wrote:
>
> > Thanks Ted,
> >
> > Created a JIRA with attached test case.
> >
> > https://issues.apache.org/jira/browse/HBASE-8626
> >
> >
> > On Sun, May 26, 2013 at 6:05 PM, Ted Yu <yu...@gmail.com> wrote:
> >
> > > There is TestAtomicOperation.java in the code base but obviously it
> > doesn't
> > > cover your use case.
> > >
> > > I assume you are using 0.94.x
> > > I put your test case
> > > in
> > >
> >
> src/test/java/org/apache/hadoop/hbase/regionserver/TestHBaseRowMutations.java
> > > but found that the test didn't start a cluster.
> > > Therefore it got stuck here:
> > > at
> org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:126)
> > >
> > > Do you mind opening a JIRA and attach your test there ?
> > >
> > > Thanks
> > >
> > > On Sun, May 26, 2013 at 3:31 AM, Vinod V <vi...@gmail.com> wrote:
> > >
> > > > Below is a test case describing the issues I am facing when doing the
> > > > following RowMutations:
> > > >
> > > > 1. Delete a column family of a row
> > > > 2. Put new columns and values to the same column family of same row
> > > >
> > > > Seems like the column family gets deleted fine but the Puts do not
> > happen
> > > > even though they are part of the same row mutation.
> > > >
> > > > 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();
> > > >         }
> > > >     }
> > > >
> > > > }
> > > >
> > > >
> > > >
> > > > --
> > > > View this message in context:
> > > >
> > >
> >
> http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-tp4045247p4045263.html
> > > > Sent from the HBase User mailing list archive at Nabble.com.
> > > >
> > >
> >
>

Re: Using RowMutations to replace all columns of a row

Posted by Ted Yu <yu...@gmail.com>.
Vinod:
Are you able to generate trunk patch for your test ?

Please refer to http://wiki.apache.org/hadoop/Hbase/HowToContribute

Please add license header and remove the following:

 * Created with IntelliJ IDEA.
 * User: vinodvr
 * Date: 26/5/13
 * Time: 7:07 PM

On Sun, May 26, 2013 at 8:02 AM, Vinod V <vi...@gmail.com> wrote:

> Thanks Ted,
>
> Created a JIRA with attached test case.
>
> https://issues.apache.org/jira/browse/HBASE-8626
>
>
> On Sun, May 26, 2013 at 6:05 PM, Ted Yu <yu...@gmail.com> wrote:
>
> > There is TestAtomicOperation.java in the code base but obviously it
> doesn't
> > cover your use case.
> >
> > I assume you are using 0.94.x
> > I put your test case
> > in
> >
> src/test/java/org/apache/hadoop/hbase/regionserver/TestHBaseRowMutations.java
> > but found that the test didn't start a cluster.
> > Therefore it got stuck here:
> > at org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:126)
> >
> > Do you mind opening a JIRA and attach your test there ?
> >
> > Thanks
> >
> > On Sun, May 26, 2013 at 3:31 AM, Vinod V <vi...@gmail.com> wrote:
> >
> > > Below is a test case describing the issues I am facing when doing the
> > > following RowMutations:
> > >
> > > 1. Delete a column family of a row
> > > 2. Put new columns and values to the same column family of same row
> > >
> > > Seems like the column family gets deleted fine but the Puts do not
> happen
> > > even though they are part of the same row mutation.
> > >
> > > 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();
> > >         }
> > >     }
> > >
> > > }
> > >
> > >
> > >
> > > --
> > > View this message in context:
> > >
> >
> http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-tp4045247p4045263.html
> > > Sent from the HBase User mailing list archive at Nabble.com.
> > >
> >
>

Re: Using RowMutations to replace all columns of a row

Posted by Vinod V <vi...@gmail.com>.
Thanks Ted,

Created a JIRA with attached test case.

https://issues.apache.org/jira/browse/HBASE-8626


On Sun, May 26, 2013 at 6:05 PM, Ted Yu <yu...@gmail.com> wrote:

> There is TestAtomicOperation.java in the code base but obviously it doesn't
> cover your use case.
>
> I assume you are using 0.94.x
> I put your test case
> in
> src/test/java/org/apache/hadoop/hbase/regionserver/TestHBaseRowMutations.java
> but found that the test didn't start a cluster.
> Therefore it got stuck here:
> at org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:126)
>
> Do you mind opening a JIRA and attach your test there ?
>
> Thanks
>
> On Sun, May 26, 2013 at 3:31 AM, Vinod V <vi...@gmail.com> wrote:
>
> > Below is a test case describing the issues I am facing when doing the
> > following RowMutations:
> >
> > 1. Delete a column family of a row
> > 2. Put new columns and values to the same column family of same row
> >
> > Seems like the column family gets deleted fine but the Puts do not happen
> > even though they are part of the same row mutation.
> >
> > 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();
> >         }
> >     }
> >
> > }
> >
> >
> >
> > --
> > View this message in context:
> >
> http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-tp4045247p4045263.html
> > Sent from the HBase User mailing list archive at Nabble.com.
> >
>

Re: Using RowMutations to replace all columns of a row

Posted by Ted Yu <yu...@gmail.com>.
There is TestAtomicOperation.java in the code base but obviously it doesn't
cover your use case.

I assume you are using 0.94.x
I put your test case
in src/test/java/org/apache/hadoop/hbase/regionserver/TestHBaseRowMutations.java
but found that the test didn't start a cluster.
Therefore it got stuck here:
at org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:126)

Do you mind opening a JIRA and attach your test there ?

Thanks

On Sun, May 26, 2013 at 3:31 AM, Vinod V <vi...@gmail.com> wrote:

> Below is a test case describing the issues I am facing when doing the
> following RowMutations:
>
> 1. Delete a column family of a row
> 2. Put new columns and values to the same column family of same row
>
> Seems like the column family gets deleted fine but the Puts do not happen
> even though they are part of the same row mutation.
>
> 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();
>         }
>     }
>
> }
>
>
>
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-tp4045247p4045263.html
> Sent from the HBase User mailing list archive at Nabble.com.
>

Re: Using RowMutations to replace all columns of a row

Posted by Vinod V <vi...@gmail.com>.
Below is a test case describing the issues I am facing when doing the
following RowMutations:

1. Delete a column family of a row
2. Put new columns and values to the same column family of same row

Seems like the column family gets deleted fine but the Puts do not happen
even though they are part of the same row mutation. 

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

}



--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Using-RowMutations-to-replace-all-columns-of-a-row-tp4045247p4045263.html
Sent from the HBase User mailing list archive at Nabble.com.