Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package fr.jetoile.hadoopunit.component; import fr.jetoile.hadoopunit.Component; import fr.jetoile.hadoopunit.HadoopUnitConfig; import fr.jetoile.hadoopunit.HadoopBootstrap; import fr.jetoile.hadoopunit.exception.BootstrapException; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.junit.Assert.assertEquals; public class HBaseBootstrapTest { static private Logger LOGGER = LoggerFactory.getLogger(HBaseBootstrapTest.class); static private Configuration configuration; @BeforeClass public static void setup() throws Exception { try { configuration = new PropertiesConfiguration(HadoopUnitConfig.DEFAULT_PROPS_FILE); } catch (ConfigurationException e) { throw new BootstrapException("bad config", e); } HadoopBootstrap.INSTANCE.startAll(); } @AfterClass public static void tearDown() throws Exception { HadoopBootstrap.INSTANCE.stopAll(); } @Test public void hBaseShouldStart() throws Exception { String tableName = configuration.getString(HadoopUnitConfig.HBASE_TEST_TABLE_NAME_KEY); String colFamName = configuration.getString(HadoopUnitConfig.HBASE_TEST_COL_FAMILY_NAME_KEY); String colQualiferName = configuration.getString(HadoopUnitConfig.HBASE_TEST_COL_QUALIFIER_NAME_KEY); Integer numRowsToPut = configuration.getInt(HadoopUnitConfig.HBASE_TEST_NUM_ROWS_TO_PUT_KEY); org.apache.hadoop.conf.Configuration hbaseConfiguration = HadoopBootstrap.INSTANCE .getService(Component.HBASE).getConfiguration(); LOGGER.info("HBASE: Creating table {} with column family {}", tableName, colFamName); createHbaseTable(tableName, colFamName, hbaseConfiguration); LOGGER.info("HBASE: Populate the table with {} rows.", numRowsToPut); for (int i = 0; i < numRowsToPut; i++) { putRow(tableName, colFamName, String.valueOf(i), colQualiferName, "row_" + i, hbaseConfiguration); } LOGGER.info("HBASE: Fetching and comparing the results"); for (int i = 0; i < numRowsToPut; i++) { Result result = getRow(tableName, colFamName, String.valueOf(i), colQualiferName, hbaseConfiguration); assertEquals("row_" + i, new String(result.value())); } } private static void createHbaseTable(String tableName, String colFamily, org.apache.hadoop.conf.Configuration configuration) throws Exception { final HBaseAdmin admin = new HBaseAdmin(configuration); HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(colFamily); hTableDescriptor.addFamily(hColumnDescriptor); admin.createTable(hTableDescriptor); } private static void putRow(String tableName, String colFamName, String rowKey, String colQualifier, String value, org.apache.hadoop.conf.Configuration configuration) throws Exception { HTable table = new HTable(configuration, tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(colFamName), Bytes.toBytes(colQualifier), Bytes.toBytes(value)); table.put(put); table.flushCommits(); table.close(); } private static Result getRow(String tableName, String colFamName, String rowKey, String colQualifier, org.apache.hadoop.conf.Configuration configuration) throws Exception { HTable table = new HTable(configuration, tableName); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(colFamName), Bytes.toBytes(colQualifier)); get.setMaxVersions(1); return table.get(get); } }