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 java.util.Arrays; 023import org.apache.hadoop.hbase.Cell; 024import org.apache.hadoop.hbase.CellBuilder; 025import org.apache.hadoop.hbase.CellUtil; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.testclassification.ClientTests; 029import org.apache.hadoop.hbase.testclassification.MediumTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 032import org.junit.jupiter.api.AfterAll; 033import org.junit.jupiter.api.BeforeAll; 034import org.junit.jupiter.api.Tag; 035import org.junit.jupiter.api.Test; 036import org.junit.jupiter.api.TestInfo; 037 038@Tag(MediumTests.TAG) 039@Tag(ClientTests.TAG) 040public class TestMutationGetCellBuilder { 041 042 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 043 044 @BeforeAll 045 public static void setUpBeforeClass() throws Exception { 046 TEST_UTIL.startMiniCluster(); 047 } 048 049 @AfterAll 050 public static void tearDownAfterClass() throws Exception { 051 TEST_UTIL.shutdownMiniCluster(); 052 } 053 054 @Test 055 public void testMutationGetCellBuilder(TestInfo testInfo) throws Exception { 056 final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 057 final byte[] rowKey = Bytes.toBytes("12345678"); 058 final byte[] uselessRowKey = Bytes.toBytes("123"); 059 final byte[] family = Bytes.toBytes("cf"); 060 final byte[] qualifier = Bytes.toBytes("foo"); 061 final long now = EnvironmentEdgeManager.currentTime(); 062 try (Table table = TEST_UTIL.createTable(tableName, family)) { 063 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000); 064 // put one row 065 Put put = new Put(rowKey); 066 CellBuilder cellBuilder = put.getCellBuilder().setQualifier(qualifier).setFamily(family) 067 .setValue(Bytes.toBytes("bar")).setTimestamp(now); 068 // setRow is useless 069 cellBuilder.setRow(uselessRowKey); 070 put.add(cellBuilder.build()); 071 byte[] cloneRow = CellUtil.cloneRow(cellBuilder.build()); 072 assertTrue(!Arrays.equals(cloneRow, uselessRowKey), "setRow must be useless"); 073 table.put(put); 074 075 // get the row back and assert the values 076 Get get = new Get(rowKey); 077 get.setTimestamp(now); 078 Result result = table.get(get); 079 assertTrue(Arrays.equals(result.getRow(), rowKey), "row key must be same"); 080 assertTrue(Bytes.toString(result.getValue(family, qualifier)).equals("bar"), 081 "Column foo value should be bar"); 082 083 // Delete that row 084 Delete delete = new Delete(rowKey); 085 cellBuilder = delete.getCellBuilder().setQualifier(qualifier).setFamily(family); 086 // if this row has been deleted,then can check setType is useless. 087 cellBuilder.setType(Cell.Type.Put); 088 delete.add(cellBuilder.build()); 089 table.delete(delete); 090 091 // check this row whether exist 092 get = new Get(rowKey); 093 get.setTimestamp(now); 094 result = table.get(get); 095 assertTrue(result.getValue(family, qualifier) == null, "Column foo should not exist"); 096 } 097 } 098}