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.*;
22 import org.apache.hadoop.hbase.util.Bytes;
23 import org.junit.AfterClass;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27
28 import static org.junit.Assert.assertTrue;
29
30 @Category(MediumTests.class)
31 public class TestPutWithDelete {
32 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
33
34
35
36
37 @BeforeClass
38 public static void setUpBeforeClass() throws Exception {
39 TEST_UTIL.startMiniCluster();
40 }
41
42
43
44
45 @AfterClass
46 public static void tearDownAfterClass() throws Exception {
47 TEST_UTIL.shutdownMiniCluster();
48 }
49
50 @Test
51 public void testHbasePutDeleteCell() throws Exception {
52 final TableName tableName = TableName.valueOf("TestPutWithDelete");
53 final byte[] rowKey = Bytes.toBytes("12345");
54 final byte[] family = Bytes.toBytes("cf");
55 HTable table = TEST_UTIL.createTable(tableName, family);
56 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
57 try {
58
59 Put put = new Put(rowKey);
60 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
61 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
62 put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
63 table.put(put);
64
65 Get get = new Get(rowKey);
66 Result result = table.get(get);
67 assertTrue("Column A value should be a",
68 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
69 assertTrue("Column B value should be b",
70 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
71 assertTrue("Column C value should be c",
72 Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
73
74 put = new Put(rowKey);
75 put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
76 put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
77 KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
78 HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
79 put.add(marker);
80 table.put(put);
81
82 get = new Get(rowKey);
83 result = table.get(get);
84 assertTrue("Column A value should be a",
85 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
86 assertTrue("Column B value should be b",
87 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
88 assertTrue("Column C should not exist",
89 result.getValue(family, Bytes.toBytes("C")) == null);
90 } finally {
91 table.close();
92 }
93 }
94 }
95
96