View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import org.apache.hadoop.hbase.*;
22  import org.apache.hadoop.hbase.testclassification.MediumTests;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  import org.junit.experimental.categories.Category;
28  
29  import static org.junit.Assert.assertTrue;
30  
31  @Category(MediumTests.class)
32  public class TestPutWithDelete {
33    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
34  
35    /**
36     * @throws java.lang.Exception
37     */
38    @BeforeClass
39    public static void setUpBeforeClass() throws Exception {
40      TEST_UTIL.startMiniCluster();
41    }
42  
43    /**
44     * @throws java.lang.Exception
45     */
46    @AfterClass
47    public static void tearDownAfterClass() throws Exception {
48      TEST_UTIL.shutdownMiniCluster();
49    }
50  
51    @Test
52    public void testHbasePutDeleteCell() throws Exception {
53      final TableName tableName = TableName.valueOf("TestPutWithDelete");
54      final byte[] rowKey = Bytes.toBytes("12345");
55      final byte[] family = Bytes.toBytes("cf");
56      HTable table = TEST_UTIL.createTable(tableName, family);
57      TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
58      try {
59        // put one row
60        Put put = new Put(rowKey);
61        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
62        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
63        put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
64        put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d"));
65        table.put(put);
66        // get row back and assert the values
67        Get get = new Get(rowKey);
68        Result result = table.get(get);
69        assertTrue("Column A value should be a",
70            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
71        assertTrue("Column B value should be b",
72            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
73        assertTrue("Column C value should be c",
74            Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
75        assertTrue("Column D value should be d",
76            Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d"));
77        // put the same row again with C column deleted
78        put = new Put(rowKey);
79        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a1"));
80        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b1"));
81        KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
82            HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
83        put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d1"));
84        put.add(marker);
85        table.put(put);
86        // get row back and assert the values
87        get = new Get(rowKey);
88        result = table.get(get);
89        assertTrue("Column A value should be a1",
90            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1"));
91        assertTrue("Column B value should be b1",
92            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1"));
93        assertTrue("Column C should not exist",
94            result.getValue(family, Bytes.toBytes("C")) == null);
95        assertTrue("Column D value should be d1",
96            Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1"));
97      } finally {
98        table.close();
99      }
100   }
101 }
102 
103