開始接觸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-directoryjavac -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-directoryjava -cp .:$HBASE_HOME/*:$HBASE_HOME/lib/* Example_1
5. 結果
代碼運行成功後,會建立表test. 包括三個columnFamily: 'cf1', 'cf2', 'cf3'.
使用hbase shell檢查:
[hbase@hadoop hbase-example-1]$ hbase shellHBase Shell; enter 'help<RETURN>' for list of supported commands.Type "exit<RETURN>" to leave the HBase ShellVersion 0.94.5, r1443843, Fri Feb 8 05:51:25 UTC 2013hbase(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的總結將不斷更新.