HBase Java 控制-1__Java

來源:互聯網
上載者:User



開始接觸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的總結將不斷更新.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.