Hbase 基礎 - shell 與 客戶端

版權說明:  本文章版權歸本人及博客園共同全部,轉載請標明原文出處(http://www.cnblogs.com/mikevictor07/),如下內容爲我的理解,僅供參考。java

 

1、簡介shell

    Hbase是在HDFS上開發的面向列的分佈式數據庫,適用於隨機讀/寫超大規模的數據集(一般這種數據壓力傳統RDBMS很難承受),能夠在廉價的硬件上構成的集羣上管理超大規模的稀疏表,而且能夠水平擴展。數據庫

 

2、基礎概念分佈式

    一、Hbase把數據存放在表中,表由行列組成,表中的行是排序的(根據ASCII順序),行鍵做爲表的主鍵,對錶的數據訪問須要經過主鍵或者主鍵Range,故行鍵的設計很重要spa

    二、列由「列族」組成(即對列分類),不一樣列族的數據一般放在不一樣的文件夾裏,列族不宜過多,Hbase啓動時就打開數據文件,而且一直保持打開狀態(Linux 默認一個進程打開最大文件數爲1024),不合理的設計將致使異常。定義表時必須定義一個可用的列族,用戶可根據須要增長或刪除列族,可是必須先disable。設計

    三、Hbase爲master/slave結構,依賴於zookeeper,master 管理着多個regionServer。code

 

3、安裝(standalone)xml

    一、必須安裝Java 1.6 或者更高版本。blog

    二、可用修改~/.base_profile,export JAVA_HOME指向JAVA安裝路徑,也可修改conf/hbase-env.sh 中 export JAVA_HOME=/usr/java/jdk1.6.0/排序

    三、默認狀況下,hbase會使用/tmp/hbase-$USERID做爲數據存儲目錄,有些系統重啓會清空/tmp目錄,可用經過更改hbase-site.xml來配置數據存儲目錄,如:

<configuration>
        <property>
            <name>hbase.rootdir</name>
            <value>file:///opt/hbase_data</value>
        </property>
</configuration>

    四、修改bin下sh文件執行權限,經過 ./bin/start-hbase.sh便可啓動hbase,經過tail -f 監聽./log/hbase-root-master-hbase-xx.log來查看啓動信息。

 

4、Hbase shell

使用 ./bin/hbase shell便可進入管理hbase,使用secureCRT會致使沒法刪除鍵入的錯誤命令(backspace delete鍵沒法使用),筆者使用putty(v0.62)可正常使用,下面是一些示例:

下面以建立一個stations的表,這表的行鍵是stationid,  列包含info.name(名稱)、info.countryCode(站點所屬國家代號)

一、建立一個表與顯示全部表

hbase> create 'stations','info'   --建立一個帶有info列族的stations表
hbase> list --顯示當前全部表

二、錄入數據(若是錄入同一行同一列則表明更新)

hbase> put 'stations', '1001', 'info:name', 'HAILAR'    --錄入1001爲行鍵、HAILAR爲站點名稱的記錄
hbase> put 'stations', '1001', 'info:countryCode', 'CH'   --CH表明china

  hbase> put 'stations', '1002', 'info:name', 'NENJIANG'
  hbase> put 'stations', '1002', 'info:countryCode', 'CH'

三、讀取、刪除數據

hbase> scan 'stations'   --讀取表中全部數據
hbase> get 'stations','1001'  --獲得行鍵爲1001的全部列
hbase> get 'stations','1002','info:name' --獲得行鍵爲1002的info:name列
hbase> delete 'stations','1001','info:countryCode' --刪除1001行的info:countryCode列

 四、增長/刪除列族

hbase> disable 'stations'
hbase> alter 'stations', {NAME=>'data'}   --增長data列族,能夠錄入以data:做爲prefix的列
hbase> enable 'stations'
hbase> describe 'stations' --列出表結構

---刪除列族
hbase> disable 'stations'
hbase> alter 'stations',{NAME=>'data', METHOD=>'delete'} --刪除stations裏面的data列族,列族下面的列將被所有刪除

五、刪除表

hbase> disable 'stations'   --須要把表disable
hbase> drop 'stations'

 經過http://hbase-master:60010/ 可查看hbase狀態信息

 

5、Java 客戶端

基本表的管理與訪問,下面方法依賴一個靜態變量:

private static String host = "192.168.70.41"; --這裏是Master 的地址

下面各段代碼中有重複部分,關鍵的在try{}中,可觸類旁通。

一、建立表

/**
     * create 'tableName','colFamily'
     */
    public static void createTable(String tableName, String colFamily) throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        try {
            hadmin = new HBaseAdmin(config);
            HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName)));
            HColumnDescriptor hcd = new HColumnDescriptor(colFamily);
            htd.addFamily(hcd);
            
            hadmin.createTable(htd);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (hadmin != null)
                hadmin.close();
        }
    }

 

二、列出全部表名

/**
     * list
     */
    public static void list() throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        
        try {
            hadmin = new HBaseAdmin(config);
            HTableDescriptor[] tables = hadmin.listTables();
            
            for (HTableDescriptor table : tables) {
                System.out.println(new String(table.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (hadmin != null)
                hadmin.close();
        }
    }

 

三、錄入數據

/**
     * put 'tableName','row','colFamily:qualifier','value'
     */
    public static void put(String tableName,String row, String colFamily, String qualifier, String value) throws Exception {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        HTable table = null;
        try {
            table = new HTable(config, tableName);
            Put put = new Put(Bytes.toBytes(row));
            put.add(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value));
            
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) 
                table.close();
            if (hadmin != null)
                hadmin.close();
        }
    }

 

四、獲取數據

/**
     * get 'tableName', 'row', 'colFamily:qualifier' 
     */
    public static void get(String tableName,String row, String colFamily, String qualifier) throws Exception {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        HTable table = null;
        try {
            table = new HTable(config, tableName);
            Get get = new Get(Bytes.toBytes(row));
            get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier));
            
            Result result = table.get(get);
            String value = Bytes.toString(result.getValue(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier)));
            System.out.println(value);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) 
                table.close();
            if (hadmin != null)
                hadmin.close();
        }
    }

 

五、刪除數據

/**
     * delete 'tableName', 'row', 'colFamily:qualifier' 
     */
    public static void delete(String tableName,String row, String colFamily, String qualifier) throws Exception {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        HTable table = null;
        try {
            table = new HTable(config, tableName);
            Delete delete = new Delete(Bytes.toBytes(row));
            
            delete.deleteColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier));
            
            table.delete(delete);
            System.out.println("delete successful");
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) 
                table.close();
            if (hadmin != null)
                hadmin.close();
        }
    }

六、掃描全表

/**
     * scan 'tableName'
     */
    public static void scan(String tableName) throws Exception {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", host);
        HBaseAdmin hadmin = null;
        HTable table = null;
        try {
            table = new HTable(config, tableName);
            Scan scan = new Scan();
            ResultScanner rc = table.getScanner(scan);
            
            for (Result result : rc) {
                System.out.println(result);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null) 
                table.close();
            if (hadmin != null)
                hadmin.close();
        }
    }
相關文章
相關標籤/搜索