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.HBaseClassTestRule; 023import org.apache.hadoop.hbase.HBaseTestingUtil; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.KeyValue; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.testclassification.ClientTests; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.junit.AfterClass; 031import org.junit.BeforeClass; 032import org.junit.ClassRule; 033import org.junit.Rule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036import org.junit.rules.TestName; 037 038@Category({ MediumTests.class, ClientTests.class }) 039public class TestPutWithDelete { 040 041 @ClassRule 042 public static final HBaseClassTestRule CLASS_RULE = 043 HBaseClassTestRule.forClass(TestPutWithDelete.class); 044 045 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 046 047 @Rule 048 public TestName name = new TestName(); 049 050 /** 051 * @throws java.lang.Exception 052 */ 053 @BeforeClass 054 public static void setUpBeforeClass() throws Exception { 055 TEST_UTIL.startMiniCluster(); 056 } 057 058 /** 059 * @throws java.lang.Exception 060 */ 061 @AfterClass 062 public static void tearDownAfterClass() throws Exception { 063 TEST_UTIL.shutdownMiniCluster(); 064 } 065 066 @Test 067 public void testHbasePutDeleteCell() throws Exception { 068 final TableName tableName = TableName.valueOf(name.getMethodName()); 069 final byte[] rowKey = Bytes.toBytes("12345"); 070 final byte[] family = Bytes.toBytes("cf"); 071 Table table = TEST_UTIL.createTable(tableName, family); 072 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000); 073 try { 074 // put one row 075 Put put = new Put(rowKey); 076 put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a")); 077 put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b")); 078 put.addColumn(family, Bytes.toBytes("C"), Bytes.toBytes("c")); 079 put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d")); 080 table.put(put); 081 // get row back and assert the values 082 Get get = new Get(rowKey); 083 Result result = table.get(get); 084 assertTrue("Column A value should be a", 085 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a")); 086 assertTrue("Column B value should be b", 087 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b")); 088 assertTrue("Column C value should be c", 089 Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c")); 090 assertTrue("Column D value should be d", 091 Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d")); 092 // put the same row again with C column deleted 093 put = new Put(rowKey); 094 put.addColumn(family, Bytes.toBytes("A"), Bytes.toBytes("a1")); 095 put.addColumn(family, Bytes.toBytes("B"), Bytes.toBytes("b1")); 096 KeyValue marker = new KeyValue(rowKey, family, Bytes.toBytes("C"), 097 HConstants.LATEST_TIMESTAMP, KeyValue.Type.DeleteColumn); 098 put.addColumn(family, Bytes.toBytes("D"), Bytes.toBytes("d1")); 099 put.add(marker); 100 table.put(put); 101 // get row back and assert the values 102 get = new Get(rowKey); 103 result = table.get(get); 104 assertTrue("Column A value should be a1", 105 Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a1")); 106 assertTrue("Column B value should be b1", 107 Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b1")); 108 assertTrue("Column C should not exist", result.getValue(family, Bytes.toBytes("C")) == null); 109 assertTrue("Column D value should be d1", 110 Bytes.toString(result.getValue(family, Bytes.toBytes("D"))).equals("d1")); 111 } finally { 112 table.close(); 113 } 114 } 115}