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.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     * @throws java.lang.Exception
36     */
37    @BeforeClass
38    public static void setUpBeforeClass() throws Exception {
39      TEST_UTIL.startMiniCluster();
40    }
41  
42    /**
43     * @throws java.lang.Exception
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        // put one row
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        put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d"));
64        table.put(put);
65        // get row back and assert the values
66        Get get = new Get(rowKey);
67        Result result = table.get(get);
68        assertTrue("Column A value should be a",
69            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
70        assertTrue("Column B value should be b",
71            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
72        assertTrue("Column C value should be c",
73            Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
74        assertTrue("Column D value should be d",
75            Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d"));
76        // put the same row again with C column deleted
77        put = new Put(rowKey);
78        put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a1"));
79        put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b1"));
80        KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
81            HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
82        put.add(family, Bytes.toBytes("D"), Bytes.toBytes("d1"));
83        put.add(marker);
84        table.put(put);
85        // get row back and assert the values
86        get = new Get(rowKey);
87        result = table.get(get);
88        assertTrue("Column A value should be a1",
89            Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1"));
90        assertTrue("Column B value should be b1",
91            Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1"));
92        assertTrue("Column C should not exist",
93            result.getValue(family, Bytes.toBytes("C")) == null);
94        assertTrue("Column D value should be d1",
95            Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1"));
96      } finally {
97        table.close();
98      }
99    }
100 }
101 
102