over 3 years ago

如果你也安裝完這兩篇完全分佈式安裝Hadoop完全分佈式安裝Zookeeper與HBase

那接下來可以開發HBase的應用程式了

0 準備軟體&環境

Eclipse Java EE IDE for Web Developers. Version: Kepler Release 下載去
hbase-0.96.1.1-hadoop2-bin.tar.gz 下載去

1 設定Eclipse

通常我的第一個習慣是修改預設所有檔案編碼
打開eclipse.ini並在檔案內新增一行

eclipse.ini
-Dfile.encoding=UTF-8

因為Eclipse預設是採用系統編碼來寫入檔案(以前好像是CP950現在新版已變成Big5)
這樣不管你是要寫java、jsp、xml等...所有檔案均是用UTF-8來編寫
一來是匯入別人原始檔比較不容易有亂碼
二來是開發web時比較不會被編碼搞死

再來我會為每一個專案設立專屬的工作資料夾
將eclipse.exe建立一個捷徑,並開啟捷徑的內容
在目標的地方增加參數(記得先加空格再加參數)

-data D:\workspace\HBase_Client

像是這樣

2 解開hbase-0.96.1.1-hadoop2-bin.tar.gz

就用RAR之類的軟體解開就可以了

3 專案設定

開啟eclipse捷徑後建立一個java專案


專案資料夾下新增一格libs資料夾放jar檔用


需匯入的Jar檔,這邊是我執行程式時缺什麼才抓什麼進來,理論上是最小安裝版本了,jar檔全部都可以在hbase-0.96.1.1-hadoop2-bin.tar.gz裡面lib資料夾找的到

commons-codec-1.7.jar
commons-configuration-1.6.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
guava-12.0.1.jar
hadoop-auth-2.2.0.jar
hadoop-common-2.2.0.jar
hbase-client-0.96.1.1-hadoop2.jar
hbase-common-0.96.1.1-hadoop2.jar
hbase-protocol-0.96.1.1-hadoop2.jar
htrace-core-2.01.jar
jackson-core-asl-1.8.8.jar
jackson-mapper-asl-1.8.8.jar
log4j-1.2.17.jar
netty-3.6.6.Final.jar
protobuf-java-2.5.0.jar
slf4j-api-1.6.4.jar
slf4j-log4j12-1.6.4.jar
zookeeper-3.4.5.jar

開啟專案的Properties選Java Build Path選項再選Libraries頁籤的Add JARs按鈕


選擇所有Jar檔

新增程式

HBaseTest.java
package com.hbase.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTest {
    private static Configuration conf = null;

    /**
     * 初始化配置
     */
    static {
        conf = HBaseConfiguration.create();
        //如果沒有配置文件,一定要記得手動宣告

        conf.set("hbase.zookeeper.quorum", "master");
    }

    /**
     * 建立表格
     * @param tablename
     * @param cfs
     */
    public static void createTable(String tablename, String[] cfs){
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            if (admin.tableExists(tablename)) {
                System.out.println("table already exists!");
            } else {
                HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
                for (int i = 0; i < cfs.length; i++) {
                    tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
                }
                admin.createTable(tableDesc);
                admin.close();
                System.out.println("create table " + tablename + " ok.");
            }
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 刪除表格
     * @param tablename
     */
    public static void deleteTable(String tablename){   
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            admin.disableTable(tablename);
            admin.deleteTable(tablename);
            System.out.println("delete table " + tablename + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增一筆資料
     * @param tableName
     * @param rowKey
     * @param family
     * @param qualifier
     * @param value
     */
    public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value){
        try {
            HTable table = new HTable(conf, tableName);
            Put put = new Put(Bytes.toBytes(rowKey));
            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
            table.put(put);
            System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 刪除一筆資料
     * @param tableName
     * @param rowKey
     */
    public static void delRecord (String tableName, String rowKey){
        try {
            HTable table = new HTable(conf, tableName);
            List list = new ArrayList();
            Delete del = new Delete(rowKey.getBytes());
            list.add(del);
            table.delete(list);
            System.out.println("del recored " + rowKey + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得一筆資料
     * @param tableName
     * @param rowKey
     */
    public static void getOneRecord (String tableName, String rowKey){         
        try {
            HTable table = new HTable(conf, tableName);
            Get get = new Get(rowKey.getBytes());
            Result rs = table.get(get);
            for(KeyValue kv : rs.raw()){
                System.out.print(new String(kv.getRow()) + " " );
                System.out.print(new String(kv.getFamily()) + ":" );
                System.out.print(new String(kv.getQualifier()) + " " );
                System.out.print(kv.getTimestamp() + " " );
                System.out.println(new String(kv.getValue()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得所有資料
     * @param tableName
     */
    public static void getAllRecord (String tableName) {         
        try{
            HTable table = new HTable(conf, tableName);
            Scan s = new Scan();
            ResultScanner ss = table.getScanner(s);         
            for(Result r:ss){
                for(KeyValue kv : r.raw()){
                    System.out.print(new String(kv.getRow()) + " ");
                    System.out.print(new String(kv.getFamily()) + ":");
                    System.out.print(new String(kv.getQualifier()) + " ");
                    System.out.print(kv.getTimestamp() + " ");
                    System.out.println(new String(kv.getValue()));
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {         
            String tablename = "scores";
            String[] familys = {"grade", "course"};         
            HBaseTest.createTable(tablename, familys);

            //add record zkb         

            HBaseTest.addRecord(tablename,"zkb","grade","","5");
            HBaseTest.addRecord(tablename,"zkb","course","","90");
            HBaseTest.addRecord(tablename,"zkb","course","math","97");
            HBaseTest.addRecord(tablename,"zkb","course","art","87");
            //add record  baoniu         

            HBaseTest.addRecord(tablename,"baoniu","grade","","4");
            HBaseTest.addRecord(tablename,"baoniu","course","math","89");

            System.out.println("===========get one record========");
            HBaseTest.getOneRecord(tablename, "zkb");

            System.out.println("===========show all record========");
            HBaseTest.getAllRecord(tablename);

            System.out.println("===========del one record========");
            HBaseTest.delRecord(tablename, "baoniu");
            HBaseTest.getAllRecord(tablename);

            System.out.println("===========show all record========");
            HBaseTest.getAllRecord(tablename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

比較重要的是要設定hbase.zookeeper.quorum跟程式說Zookeeper在哪

執行結果


ps.log4的警告目前沒有關係,因為我沒有設定啦XD.....等正式開發的時候就會設好了

驗證HBase確實寫入資料

補充
如果不想用程式宣告Zookeeper的位置也可用xml檔,配置方式如下圖

因為KeyValue元件很多方法都被Deprecated,所以我找了一下似乎改以Cell操作所以就改了一下

HBaseTest.java
package com.hbase.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTest {
    private static Configuration conf = null;

    /**
     * 初始化配置
     */
    static {
        conf = HBaseConfiguration.create();
        //如果沒有配置文件,一定要記得手動宣告

        //conf.set("hbase.zookeeper.quorum", "master");

    }

    /**
     * 建立表格
     * @param tablename
     * @param cfs
     */
    public static void createTable(String tablename, String[] cfs){
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            if (admin.tableExists(tablename)) {
                System.out.println("table already exists!");
            } else {
                HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
                for (int i = 0; i < cfs.length; i++) {
                    tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
                }
                admin.createTable(tableDesc);
                admin.close();
                System.out.println("create table " + tablename + " ok.");
            }
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 刪除表格
     * @param tablename
     */
    public static void deleteTable(String tablename){   
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            admin.disableTable(tablename);
            admin.deleteTable(tablename);
            System.out.println("delete table " + tablename + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增一筆資料
     * @param tableName
     * @param rowKey
     * @param family
     * @param qualifier
     * @param value
     */
    public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value){
        try {
            HTable table = new HTable(conf, tableName);
            Put put = new Put(Bytes.toBytes(rowKey));
            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
            table.put(put);
            System.out.println("insert recored " + rowKey + " to table " + tableName +" ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 刪除一筆資料
     * @param tableName
     * @param rowKey
     */
    public static void delRecord (String tableName, String rowKey){
        try {
            HTable table = new HTable(conf, tableName);
            List list = new ArrayList();
            Delete del = new Delete(rowKey.getBytes());
            list.add(del);
            table.delete(list);
            System.out.println("del recored " + rowKey + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得一筆資料
     * @param tableName
     * @param rowKey
     */
    public static void getOneRecord (String tableName, String rowKey){         
        try {
            HTable table = new HTable(conf, tableName);
            Get get = new Get(rowKey.getBytes());
            Result rs = table.get(get);
            List<Cell> list = rs.listCells();
            for(Cell cell:list){
                System.out.print(new String(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()) + " " );
                System.out.print(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()) + ":" );
                System.out.print(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()) + " " );
                System.out.print(cell.getTimestamp() + " " );
                System.out.print(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()) + " " );
                System.out.println("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取得所有資料
     * @param tableName
     */
    public static void getAllRecord (String tableName) {
        try{
            HTable table = new HTable(conf, tableName);
            Scan scan = new Scan();
            ResultScanner resultscanner = table.getScanner(scan);
            for(Result rs:resultscanner){
                List<Cell> list = rs.listCells();
                for(Cell cell:list){
                    System.out.print(new String(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()) + " " );
                    System.out.print(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()) + ":" );
                    System.out.print(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()) + " " );
                    System.out.print(cell.getTimestamp() + " " );
                    System.out.print(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()) + " " );
                    System.out.println("");
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 取得Family清單
     * @param tableName
     * @return
     */
    public static ArrayList getAllFamilyName(String tableName) {
        ArrayList<String> familyname_list = new ArrayList();
        try{
            HTable table = new HTable(conf, tableName);
            HTableDescriptor htabledescriptor = table.getTableDescriptor();
            HColumnDescriptor[] hdlist = htabledescriptor.getColumnFamilies();
            for(int i=0;i<hdlist.length;i++){
                HColumnDescriptor hd = hdlist[i];
                familyname_list.add(hd.getNameAsString());
            }
        } catch (IOException e){
            e.printStackTrace();
        }
        return familyname_list;
    }

    public static void main(String[] args) {
        try {         
            String tablename = "scores";
            String[] familys = {"grade", "course"};         
            HBaseTest.createTable(tablename, familys);

            //add record zkb         

            HBaseTest.addRecord(tablename,"zkb","grade","","5");
            HBaseTest.addRecord(tablename,"zkb","course","","90");
            HBaseTest.addRecord(tablename,"zkb","course","math","97");
            HBaseTest.addRecord(tablename,"zkb","course","art","87");
            //add record  baoniu         

            HBaseTest.addRecord(tablename,"baoniu","grade","","4");
            HBaseTest.addRecord(tablename,"baoniu","course","math","89");

            System.out.println("===========get one record========");
            HBaseTest.getOneRecord(tablename, "baoniu");

            System.out.println("===========show all record========");
            HBaseTest.getAllRecord(tablename);

            System.out.println("===========del one record========");
            HBaseTest.delRecord(tablename, "baoniu");
            HBaseTest.getAllRecord(tablename);

            System.out.println("===========show all record========");
            HBaseTest.getAllRecord(tablename);

            System.out.println("===========show all familyname========");
            System.out.println(HBaseTest.getAllFamilyName(tablename));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

參考來源
http://blog.csdn.net/chen_zhipeng/article/details/7887109

← Hadoop 2 完全分佈式安裝 HBase Zookeeper Use Jasypt Encryptor Configuration In Spring →
 
comments powered by Disqus