1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import org.apache.hadoop.hbase.HBaseTestingUtility;
22 import org.apache.hadoop.hbase.testclassification.MediumTests;
23 import org.apache.hadoop.hbase.TableName;
24 import org.apache.hadoop.hbase.filter.CompareFilter;
25 import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.fail;
34
35 @Category(MediumTests.class)
36 public class TestCheckAndMutate {
37 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
38
39
40
41
42 @BeforeClass
43 public static void setUpBeforeClass() throws Exception {
44 TEST_UTIL.startMiniCluster();
45 }
46
47
48
49
50 @AfterClass
51 public static void tearDownAfterClass() throws Exception {
52 TEST_UTIL.shutdownMiniCluster();
53 }
54
55 @Test
56 public void testCheckAndMutate() throws Exception {
57 final TableName tableName = TableName.valueOf("TestPutWithDelete");
58 final byte[] rowKey = Bytes.toBytes("12345");
59 final byte[] family = Bytes.toBytes("cf");
60 HTable table = TEST_UTIL.createTable(tableName, family);
61 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
62 try {
63
64 Put put = new Put(rowKey);
65 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
66 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
67 put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
68 table.put(put);
69
70 Get get = new Get(rowKey);
71 Result result = table.get(get);
72 assertTrue("Column A value should be a",
73 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
74 assertTrue("Column B value should be b",
75 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
76 assertTrue("Column C value should be c",
77 Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
78
79
80 RowMutations rm = new RowMutations(rowKey);
81 put = new Put(rowKey);
82 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
83 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
84 rm.add(put);
85 Delete del = new Delete(rowKey);
86 del.deleteColumn(family, Bytes.toBytes("C"));
87 rm.add(del);
88 boolean res = table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
89 Bytes.toBytes("a"), rm);
90 assertTrue(res);
91
92
93 get = new Get(rowKey);
94 result = table.get(get);
95 assertTrue("Column A value should be a",
96 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
97 assertTrue("Column B value should be b",
98 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
99 assertTrue("Column C should not exist",
100 result.getValue(family, Bytes.toBytes("C")) == null);
101
102
103 try {
104 Put p = new Put(rowKey);
105 p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, new byte[0]);
106 rm = new RowMutations(rowKey);
107 rm.add(p);
108 table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
109 Bytes.toBytes("a"), rm);
110 fail("Expected NoSuchColumnFamilyException");
111 } catch(NoSuchColumnFamilyException e) {
112 }
113 } finally {
114 table.close();
115 }
116 }
117 }