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.assertArrayEquals;
021
022import java.io.IOException;
023import java.util.stream.Collectors;
024import java.util.stream.IntStream;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
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.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ MediumTests.class, ClientTests.class })
038public class TestBufferedMutator {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestBufferedMutator.class);
043
044  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045
046  private static TableName TABLE_NAME = TableName.valueOf("test");
047
048  private static byte[] CF = Bytes.toBytes("cf");
049
050  private static byte[] CQ = Bytes.toBytes("cq");
051
052  private static int COUNT = 1024;
053
054  private static byte[] VALUE = new byte[1024];
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    TEST_UTIL.startMiniCluster(1);
059    TEST_UTIL.createTable(TABLE_NAME, CF);
060  }
061
062  @AfterClass
063  public static void tearDown() throws Exception {
064    TEST_UTIL.shutdownMiniCluster();
065  }
066
067  @Test
068  public void test() throws Exception {
069    try (BufferedMutator mutator = TEST_UTIL.getConnection()
070      .getBufferedMutator(new BufferedMutatorParams(TABLE_NAME).writeBufferSize(64 * 1024))) {
071      mutator.mutate(IntStream.range(0, COUNT / 2)
072        .mapToObj(i -> new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))
073        .collect(Collectors.toList()));
074      mutator.flush();
075      mutator.mutate(IntStream.range(COUNT / 2, COUNT)
076        .mapToObj(i -> new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))
077        .collect(Collectors.toList()));
078      mutator.close();
079      verifyData();
080    }
081  }
082
083  private void verifyData() throws IOException {
084    try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
085      for (int i = 0; i < COUNT; i++) {
086        Result r = table.get(new Get(Bytes.toBytes(i)));
087        assertArrayEquals(VALUE, ((Result) r).getValue(CF, CQ));
088      }
089    }
090  }
091}