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 static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.io.IOException;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.filter.CompareFilter;
29 import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36
37 @Category(MediumTests.class)
38 public class TestCheckAndMutate {
39 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
40
41
42
43
44 @BeforeClass
45 public static void setUpBeforeClass() throws Exception {
46 TEST_UTIL.startMiniCluster();
47 }
48
49
50
51
52 @AfterClass
53 public static void tearDownAfterClass() throws Exception {
54 TEST_UTIL.shutdownMiniCluster();
55 }
56
57 @Test
58 public void testCheckAndMutate() throws Throwable {
59 final TableName tableName = TableName.valueOf("TestPutWithDelete");
60 final byte[] rowKey = Bytes.toBytes("12345");
61 final byte[] family = Bytes.toBytes("cf");
62 HTable table = TEST_UTIL.createTable(tableName, family);
63 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
64 try {
65
66 Put put = new Put(rowKey);
67 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
68 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
69 put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
70 table.put(put);
71
72 Get get = new Get(rowKey);
73 Result result = table.get(get);
74 assertTrue("Column A value should be a",
75 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
76 assertTrue("Column B value should be b",
77 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
78 assertTrue("Column C value should be c",
79 Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
80
81
82 RowMutations rm = new RowMutations(rowKey);
83 put = new Put(rowKey);
84 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
85 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
86 rm.add(put);
87 Delete del = new Delete(rowKey);
88 del.deleteColumn(family, Bytes.toBytes("C"));
89 rm.add(del);
90 boolean res = table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
91 Bytes.toBytes("a"), rm);
92 assertTrue(res);
93
94
95 get = new Get(rowKey);
96 result = table.get(get);
97 assertTrue("Column A value should be a",
98 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
99 assertTrue("Column B value should be b",
100 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
101 assertTrue("Column C should not exist",
102 result.getValue(family, Bytes.toBytes("C")) == null);
103
104
105 try {
106 Put p = new Put(rowKey);
107 p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, new byte[0]);
108 rm = new RowMutations(rowKey);
109 rm.add(p);
110 table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL,
111 Bytes.toBytes("a"), rm);
112 fail("Expected NoSuchColumnFamilyException");
113 } catch (RetriesExhaustedWithDetailsException e) {
114 try {
115 throw e.getCause(0);
116 } catch (NoSuchColumnFamilyException e1) {
117
118 }
119 }
120 } finally {
121 table.close();
122 }
123 }
124
125 @Test
126 public void testCheckRowAndMutateDifferentRow() throws IOException {
127 TableName tableName = TableName.valueOf("TestCheckRowAndMutateDifferentRow");
128 byte[] family = Bytes.toBytes("f");
129 TEST_UTIL.createTable(tableName, family);
130 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
131 byte[] row1 = Bytes.toBytes("row1");
132 byte[] qualifier = Bytes.toBytes("q1");
133 byte[] value1 = Bytes.toBytes("value1");
134
135 Put put = new Put(row1);
136 put.addColumn(family, qualifier, value1);
137 table.put(put);
138
139 Result result = table.get(new Get(row1));
140 assertArrayEquals("the value of column q in row1 should be value1",
141 value1, result.getValue(family, qualifier));
142
143 byte[] row2 = Bytes.toBytes("row2");
144 byte[] value2 = Bytes.toBytes("value2");
145 RowMutations mutations = new RowMutations(row2);
146 put = new Put(row2);
147 put.addColumn(family, qualifier, value2);
148 mutations.add(put);
149
150
151 assertTrue(table.checkAndMutate(row1, family, qualifier,
152 CompareFilter.CompareOp.GREATER, value2, mutations));
153
154 result = table.get(new Get(row2));
155 assertArrayEquals("the value of column q in row2 should be value2",
156 value2, result.getValue(family, qualifier));
157 }
158 }
159 }