001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertTrue;
021
022import org.apache.hadoop.hbase.*;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.ClientTests;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.AfterClass;
028import org.junit.BeforeClass;
029import org.junit.ClassRule;
030import org.junit.Rule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.junit.rules.TestName;
034
035@Category({MediumTests.class, ClientTests.class})
036public class TestPutWithDelete {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestPutWithDelete.class);
041
042  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
043
044  @Rule
045  public TestName name = new TestName();
046
047  /**
048   * @throws java.lang.Exception
049   */
050  @BeforeClass
051  public static void setUpBeforeClass() throws Exception {
052    TEST_UTIL.startMiniCluster();
053  }
054
055  /**
056   * @throws java.lang.Exception
057   */
058  @AfterClass
059  public static void tearDownAfterClass() throws Exception {
060    TEST_UTIL.shutdownMiniCluster();
061  }
062
063  @Test
064  public void testHbasePutDeleteCell() throws Exception {
065    final TableName tableName = TableName.valueOf(name.getMethodName());
066    final byte[] rowKey = Bytes.toBytes("12345");
067    final byte[] family = Bytes.toBytes("cf");
068    Table table = TEST_UTIL.createTable(tableName, family);
069    TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
070    try {
071      // put one row
072      Put put = new Put(rowKey);
073      put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
074      put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
075      put.addColumn(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
076      put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d"));
077      table.put(put);
078      // get row back and assert the values
079      Get get = new Get(rowKey);
080      Result result = table.get(get);
081      assertTrue("Column A value should be a",
082          Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"));
083      assertTrue("Column B value should be b",
084          Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"));
085      assertTrue("Column C value should be c",
086          Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"));
087      assertTrue("Column D value should be d",
088          Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d"));
089      // put the same row again with C column deleted
090      put = new Put(rowKey);
091      put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a1"));
092      put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b1"));
093      KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
094          HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
095      put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d1"));
096      put.add(marker);
097      table.put(put);
098      // get row back and assert the values
099      get = new Get(rowKey);
100      result = table.get(get);
101      assertTrue("Column A value should be a1",
102          Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1"));
103      assertTrue("Column B value should be b1",
104          Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1"));
105      assertTrue("Column C should not exist",
106          result.getValue(family, Bytes.toBytes("C")) == null);
107      assertTrue("Column D value should be d1",
108          Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1"));
109    } finally {
110      table.close();
111    }
112  }
113}
114
115