/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.coheigea.camel.hbase;
import java.io.IOException;
import java.net.ServerSocket;
import org.apache.camel.spring.Main;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
/**
* This demo shows how to use Apache Camel to invoke on Apache HBase.
*/
public class HBaseTest extends org.junit.Assert {
private static HBaseTestingUtility utility;
@org.junit.BeforeClass
public static void setup() throws Exception {
utility = new HBaseTestingUtility();
int port = Integer.parseInt(System.getProperty("hbase.port"));
utility.getConfiguration().set("test.hbase.zookeeper.property.clientPort", "" + port);
utility.getConfiguration().set("hbase.master.port", "" + getFreePort());
utility.getConfiguration().set("hbase.master.info.port", "" + getFreePort());
utility.getConfiguration().set("hbase.regionserver.port", "" + getFreePort());
utility.getConfiguration().set("hbase.regionserver.info.port", "" + getFreePort());
utility.getConfiguration().set("zookeeper.znode.parent", "/hbase-unsecure");
utility.startMiniCluster();
// Create a table
final Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "" + port);
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
// Create a table
if (!admin.tableExists(TableName.valueOf("temp"))) {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("temp"));
// Adding column families to table descriptor
tableDescriptor.addFamily(new HColumnDescriptor("colfam1"));
tableDescriptor.addFamily(new HColumnDescriptor("colfam2"));
admin.createTable(tableDescriptor);
}
// Add a new row
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col1"), Bytes.toBytes("val1"));
Table table = conn.getTable(TableName.valueOf("temp"));
table.put(put);
put = new Put(Bytes.toBytes("row2"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col1"), Bytes.toBytes("val2"));
table.put(put);
conn.close();
}
@org.junit.AfterClass
public static void cleanup() throws Exception {
utility.shutdownMiniCluster();
}
@org.junit.Test
public void testHBase() throws Exception {
// Start up the Camel route
Main main = new Main();
main.setApplicationContextUri("camel-hbase.xml");
main.start();
// Sleep to allow time to copy the files etc.
Thread.sleep(10 * 1000);
main.stop();
}
private static int getFreePort() throws IOException {
ServerSocket serverSocket = new ServerSocket(0);
int port = serverSocket.getLocalPort();
serverSocket.close();
return port;
}
}