开始接触HBase,捣鼓了半天总算使用Java在HBase上成功创建了一个表。记录这个过程,供新手参考。
1. 环境
HBase使用版本0.94.5.
下载地址: hbase-0.94.5.tar.gz
配置方法:
这里使用standalone(单机模拟方式)模式
java 版本: 1.6
操作系统:centos 5.
2. 编写步骤
创建Example_1.java文件
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class Example_1
{
private Configuration conf=null;
public Example_1()
{
conf=HBaseConfiguration.create();//需要读取HBase-site.xml配置文件
}
/**
* 创建表操作
* @throws IOException
*/
public void createTable(String tablename, String[] cfs) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)) {
System.out.println("表已经存在");
}
else {
HTableDescriptor tableDesc = new HTableDescriptor(tablename);
for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
}
admin.createTable(tableDesc);
System.out.println("表创建成功!");
}
admin.close();
}
public static void main(String args[]) throws IOException
{
Example_1 e=new Example_1();
String tablename="test";
String columnFamilys[]={"cf1","cf2","cf3"};
e.createTable(tablename, columnFamilys);
}
}
编译该文件使用的命令为:
HBASE_HOME=path-to-hbase-directory
javac -cp $HBASE_HOME/*:$HBASE_HOME/lib/* Example_1.java
4. 运行
运行时,需要指定HBase配置。简单的方法是将HBase-site.xml文件拷贝到程序相同的目录下。也可以通过conf.set(name,value)方式写入代码中,但这种方式不太灵活.
HBase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/hbase/hbase-0.94.5/hbase_data</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>file:///home/hbase/hbase-0.94.5/zookeeper_data</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop.master</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>false</value>
</property>
</configuration>
运行需要指定各个jar包。
HBASE_HOME=path-to-hbase-directory
java -cp .:$HBASE_HOME/*:$HBASE_HOME/lib/* Example_1
5.
结果
代码运行成功后,会创建表test. 包括三个columnFamily: 'cf1', 'cf2', 'cf3'.
使用hbase shell检查:
[hbase@hadoop hbase-example-1]$ hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.5, r1443843, Fri Feb 8 05:51:25 UTC 2013
hbase(main):003:0> describe 'test'
DESCRIPTION ENABLED
{NAME => 'test', FAMILIES => [{NAME => 'cf1', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VER true
SIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536
', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}, {NAME => 'cf2', DATA_BLOCK_ENCODING => 'NONE', BLOOMFIL
TER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_D
ELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}, {NAME => 'c
f3', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_
VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK =
> 'true', BLOCKCACHE => 'true'}]}
1 row(s) in 0.0130 seconds
更多Java控制HBase的总结将不断更新.