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.jupiter.api.Assertions.assertTrue;
021
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.KeyValue;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.junit.jupiter.api.TestInfo;
034
035@Tag(MediumTests.TAG)
036@Tag(ClientTests.TAG)
037public class TestPutWithDelete {
038
039  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
040
041  @BeforeAll
042  public static void setUpBeforeClass() throws Exception {
043    TEST_UTIL.startMiniCluster();
044  }
045
046  @AfterAll
047  public static void tearDownAfterClass() throws Exception {
048    TEST_UTIL.shutdownMiniCluster();
049  }
050
051  @Test
052  public void testHbasePutDeleteCell(TestInfo testInfo) throws Exception {
053    final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
054    final byte[] rowKey = Bytes.toBytes("12345");
055    final byte[] family = Bytes.toBytes("cf");
056    try (Table table = TEST_UTIL.createTable(tableName, family)) {
057      TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
058      // put one row
059      Put put = new Put(rowKey);
060      put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a"));
061      put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b"));
062      put.addColumn(family, Bytes.toBytes("C"), Bytes.toBytes("c"));
063      put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d"));
064      table.put(put);
065      // get row back and assert the values
066      Get get = new Get(rowKey);
067      Result result = table.get(get);
068      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a"),
069        "Column A value should be a");
070      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b"),
071        "Column B value should be b");
072      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c"),
073        "Column C value should be c");
074      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d"),
075        "Column D value should be d");
076      // put the same row again with C column deleted
077      put = new Put(rowKey);
078      put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a1"));
079      put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b1"));
080      KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"),
081        HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn);
082      put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d1"));
083      put.add(marker);
084      table.put(put);
085      // get row back and assert the values
086      get = new Get(rowKey);
087      result = table.get(get);
088      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1"),
089        "Column A value should be a1");
090      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1"),
091        "Column B value should be b1");
092      assertTrue(result.getValue(family, Bytes.toBytes("C")) == null, "Column C should not exist");
093      assertTrue(Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1"),
094        "Column D value should be d1");
095    }
096  }
097}