windows 10環境下搭建基於HBase的Eclipse開發環境

Apache HBase是一個分佈式的、面向列的開源數據庫,其主要特色是:html

  •  HBase – Hadoop Database,是一個高可靠性、高性能、面向列、可伸縮、 實時讀寫的分佈式數據庫 
  • 利用Hadoop HDFS做爲其文件存儲系統,利用Hadoop MapReduce來處理 HBase中的海量數據,利用Zookeeper做爲其分佈式協同服務
  •  主要用來存儲非結構化和半結構化的鬆散數據(列存NoSQL數據庫)

本文用於指導hbase在windows10環境下單機版的使用,採用僞分佈式。java

1.1  準備工做

下載對應的軟件版本:shell

  • Windows10家庭版
  • JDK 1.8.0_171-b11
  • hbase-1.2.6.1

lHBase的僞分佈模式是依賴於HDFS的,所以在安裝HBase以前須要安裝匹配版本的Hadoop。新建環境變量HADOOP_HOME,在path後添加:%HADOOP_HOME%\bin數據庫

Hadoop安裝參考文章:http://www.javashuo.com/article/p-elrxakzq-mx.htmlapache

 

1.2  HBase的配置與啓動

【JDK安裝再也不說明】windows

第一步:HBase解壓軟件瀏覽器

我使用的路徑是:D:\Study\codeproject\hbase-1.2.6.1網絡

 

 

第二步:修改HBase下的conf/hbase-env.cmdsession

set JAVA_HOME=C:\Java\jdk1.8.0_171eclipse

set HBASE_MANAGES_ZK=false

其餘都使用默認值,無需修改。

 

第三步:修改HBase下的hbase-site.xml

D:\Study\codeproject\hbase-1.2.6.1\conf

<configuration> 

    <property> 

        <name>hbase.rootdir</name> 

        <value>file:///D:/Study/codeproject/hbase-1.2.6.1/data</value> 

    </property> 

    <property> 

        <name>hbase.tmp.dir</name> 

        <value>D:/Study/codeproject/hbase-1.2.6.1/data/tmp</value>  

    </property> 

    <property> 

        <name>hbase.zookeeper.quorum</name> 

        <value>127.0.0.1</value> 

    </property> 

    <property> 

        <name>hbase.zookeeper.property.dataDir</name> 

        <value>D:/Study/codeproject/hbase-1.2.6.1/data/zoo</value> 

    </property> 

    <property> 

        <name>hbase.cluster.distributed</name> 

        <!--HBase以分佈式模式進行,這個功能在win下不支持,寫成false-->

        <value>false</value> 

    </property> 

</configuration>

 

第四步:啓動HBase

D:\Study\codeproject\hbase-1.2.6.1\bin下打開命令行,輸入start-hbase.cmd,啓動HBase。

 

第五步:測試Shell

 HBase啓動後,打開一個新窗口,進入D:\Study\codeproject\hbase-1.2.6.1\bin,在命令行輸入hbase shell,出現以下結果表示鏈接成功。

D:\Study\codeproject\hbase-1.2.6.1\bin>hbase shell

HBase Shell; enter 'help<RETURN>' for list of supported commands.

Type "exit<RETURN>" to leave the HBase Shell

Version 1.2.6.1, rUnknown, Sun Jun  3 23:19:26 CDT 2018

hbase(main):001:0>

 

第六步:打開HBase主頁GUI,在瀏覽器輸入網址:http://127.0.0.1:16010/master-status

 

 

 

1.3  HBase的基本操做

第一步:建立表hbase_test有兩個列族cf1cf2

此表有兩個列族,cf1和cf2,其中CF1和CF2下分別有兩個列name和gender,Chinese和Math

hbase(main):010:0>  create 'hbase_test',  {NAME=>'cf1'}, {NAME=>'cf2'}

0 row(s) in 1.2440 seconds

=> Hbase::Table - hbase_test

切換到管理頁面查看錶:

 

第二步:向表中添加數據,在想HBase的表中添加數據的時候,只能一列一列的添加,不能同時添加多列。

hbase(main):011:0> put'hbase_test', '001','cf1:name','Tao'

0 row(s) in 0.0720 seconds

hbase(main):019:0> put'hbase_test', '002','cf1:gender','man'

0 row(s) in 0.0060 seconds

hbase(main):020:0>

hbase(main):021:0*  put'hbase_test', '001','cf2:chinese','99'

0 row(s) in 0.0030 seconds

hbase(main):022:0>  put'hbase_test', '001','cf2:math','100'

0 row(s) in 0.0040 seconds

這樣表結構就起來了,其實比較自由,列族裏邊能夠自由添加子列很方便。若是列族下沒有子列,加不加冒號都是能夠的。

若是在添加數據的時候,須要手動的設置時間戳,則在put命令的最後加上相應的時間戳,時間戳是long類型的,因此不須要加引號

hbase(main):045:0> put'hbase_test', '001','cf2:math','91',1478053832459

第三步:查看錶中的全部數據

hbase(main):027:0* scan 'hbase_test'

ROW                 COLUMN+CELL                                                                            

 001                column=cf1:name, timestamp=1552210114388, value=Tao                                    

 001                column=cf2:chinese, timestamp=1552210284719, value=99                                  

 001                column=cf2:math, timestamp=1552210290155, value=100                                    

 002               column=cf1:gender, timestamp=1552210272044, value=man                                  

2 row(s) in 0.0250 seconds

 

第四步:查看其中某一個Key的數據

hbase(main):028:0>  get'hbase_test','001'

COLUMN                      CELL                                                                                   

 cf1:name                      timestamp=1552210114388, value=Tao                                                     

 cf2:chinese                    timestamp=1552210284719, value=99                                                      

 cf2:math                       timestamp=1552210290155, value=100                                                     

3 row(s) in 0.0280 seconds

1.4  基於eclipse的HBase的java操做

第一步:在eclipse中建立以下demo代碼(參考網絡代碼編寫),注意:相關包的引入,不然編譯不經過,同時HBase已經啓動:

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.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.HTablePool;
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;

public class HbaseTest
{
    private HBaseAdmin admin = null;
    // 定義配置對象HBaseConfiguration
    private HBaseConfiguration cfg = null;
    public HbaseTest() throws Exception
    {
        Configuration HBASE_CONFIG = new Configuration();
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "127.0.0.1");
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
        //HBASE_CONFIG.set("hbase.master", "192.168.1.109:9001"); 
        cfg = new HBaseConfiguration(HBASE_CONFIG);
        admin = new HBaseAdmin(cfg);
    }
    // 建立一張表,指定表名,列族
    public void createTable(String tableName, String columnFarily)throws Exception
    {
        if (admin.tableExists(tableName))
        {
            System.out.println(tableName + "存在!");
            System.exit(0);

        } else
        {
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);
            tableDesc.addFamily(new HColumnDescriptor(columnFarily));
            admin.createTable(tableDesc);
            System.out.println("建立表成功!");
        }
    }
    // Hbase獲取全部的表信息
    public List getAllTables()
    {
        List<String> tables = null;
        if (admin != null)
        {
            try
            {
                HTableDescriptor[] allTable = admin.listTables();
                if (allTable.length > 0)
                    tables = new ArrayList<String>();
                for (HTableDescriptor hTableDescriptor : allTable)
                {
                    tables.add(hTableDescriptor.getNameAsString());
                    System.out.println(hTableDescriptor.getNameAsString());
                }
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return tables;
    }
    // Hbase中往某個表中添加一條記錄
    public boolean addOneRecord(String table, String key, String family,String col, byte[] dataIn)
    {
        HTablePool tp = new HTablePool(cfg, 1000);
        // HTable tb = (HTable) tp.getTable(table);
        Put put = new Put(key.getBytes());
        put.add(family.getBytes(), col.getBytes(), dataIn);
        try
        {
            HTablePool pool = new HTablePool(cfg, 1000);
            pool.getTable(table).put(put);
            // tb.put(put);
            System.out.println("插入數據條" + key + "成功!!!");
            return true;
        } catch (IOException e)
        {
            e.printStackTrace();
            System.out.println("插入數據條" + key + "失敗!!!");
            return false;
        }
    }
    // Hbase表中記錄信息的查詢
    public void getValueFromKey(String table, String key)
    {
        HTablePool tp = new HTablePool(cfg, 1000);
        // HTable tb = (HTable) tp.getTable(table);
        Get get = new Get(key.getBytes());
        try
        {
            Result rs = tp.getTable(table).get(get);
            if (rs.raw().length == 0)
            {
                System.out.println("不存在關鍵字爲" + key + "的行!!");
            } else
            {
                for (KeyValue kv : rs.raw())
                {
                    System.out.println(new String(kv.getKey()) + " " + new String(kv.getValue()));
                }
            }

        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    // 顯示全部數據,經過HTable Scan類獲取已有表的信息
    public void getAllData(String tableName) throws Exception
    {
        HTable table = new HTable(cfg, tableName);
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for (Result r : rs)
        {
            for (KeyValue kv : r.raw())
            {
                System.out.println(new String(kv.getKey())+ new String(kv.getValue()));
            }
        }
    }

    // Hbase表中記錄信息的刪除
    public boolean deleteRecord(String table, String key)
    {
        HTablePool tp = new HTablePool(cfg, 1000);
        // HTable tb = (HTable) tp.getTable(table);
        Delete de = new Delete(key.getBytes());
        try
        {
            tp.getTable(table).delete(de);
            return true;
        } catch (IOException e)
        {
            System.out.println("刪除記錄" + key + "異常!!!");
            return false;
        }
    }
    // Hbase中表的刪除
    public boolean deleteTable(String table)
    {
        try
        {
            if (admin.tableExists(table))
            {
                admin.disableTable(table);
                admin.deleteTable(table);
                System.out.println("刪除表" + table + "!!!");
            }
            return true;
        } catch (IOException e)
        {
            System.out.println("刪除表" + table + "異常!!!");
            return false;
        }

    }
    // 測試函數
    public static void main(String[] args)
    {
        try
        {
            HbaseTest hbase = new HbaseTest();
            hbase.createTable("hbase_test2", "cf1");
            // hbase.getAllTables();
            hbase.addOneRecord("hbase_test2", "id1", "cf1", "name", "Kite".getBytes());
            hbase.addOneRecord("hbase_test2", "id1", "cf1", "address", "HW".getBytes());
            // hbase.getValueFromKey("hbase_test2","id1");
            // hbase.getAllData("hbase_test2");
            // hbase.deleteRecord("hbase_test2", "id1");
            // hbase.deleteTable("hbase_test2");
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

 第二步:執行上述代碼,成功以後eclipse打印以下信息(上述測試函數中註釋掉的代碼,能夠分別放開測試並查看數據,這裏不一一列舉):

上述代碼執行後結果打印以下:

DEBUG [main] - Connecting to LAPTOP-TAOTAO/192.168.1.104:59608

 INFO [main] - Created hbase_test2

建立表成功!

DEBUG [main-SendThread(127.0.0.1:2181)] - Reading reply sessionid:0x16977330f120007, packet:: clientPath:null serverPath:null finished:false header:: 7,4  replyHeader:: 7,1108,0  request:: '/hbase/meta-region-server,F  response:: #ffffffff0001a726567696f6e7365727665723a35393633392410ffffffd342ffffffce75323350425546a1aad4c4150544f502d54414f54414f10fffffff7ffffffd1318ffffff9dffffffa9ffffffccffffffb9ffffff972d100183,s{1080,1080,1552483046311,1552483046311,0,0,0,0,67,0,1080}

DEBUG [main-SendThread(127.0.0.1:2181)] - Reading reply sessionid:0x16977330f120007, packet:: clientPath:null serverPath:null finished:false header:: 8,8  replyHeader:: 8,1108,0  request:: '/hbase,F  response:: v{'replication,'meta-region-server,'rs,'splitWAL,'backup-masters,'table-lock,'flush-table-proc,'region-in-transition,'online-snapshot,'master,'running,'recovering-regions,'draining,'namespace,'hbaseid,'table}

插入數據條id1成功!!!

插入數據條id1成功!!!

第三步:從HBase的命令行查看新建的表數據:

 

第四部:從圖形界面查看:

相關文章
相關標籤/搜索