R語言與Hadoop和Hbase的聯合使用

HBase和rhbase的安裝與使用,分爲3個章節。php

1. 環境準備及HBase安裝
2. rhbase安裝
3. rhbase程序用例

每一章節,都會分爲」文字說明部分」和」代碼部分」,保持文字說明與代碼的連貫性。html

注:Hadoop環境及RHadoop的環境,請查看同系列前二篇文章,此文將再也不介紹。java

1. 環境準備及HBase安裝

文字說明部分:

首先環境準備,這裏我選擇了Linux Ubuntu操做系統12.04的64位版本,你們能夠根據本身的使用習慣選擇順手的Linux。python

但JDK必定要用Oracle SUN官方的版本,請從官網下載,操做系統的自帶的OpenJDK會有各類不兼容。JDK請選擇1.6.x的版本,JDK1.7版本也會有各類的不兼容狀況。
http://www.oracle.com/technetwork/java/javase/downloads/index.html算法

Hadoop的環境安裝,請參考RHadoop實踐系統」Hadoop環境搭建」的一文。shell

Hadoop和HBase版本:hadoop-1.0.3,hbase-0.94.2apache

配置HBase的啓動命令的環境變量,使用HBase自帶的ZooKeeper
export HBASE_MANAGES_ZK=true架構

配置hbase-site.xml,設置訪問目錄,數據副本數,ZooKeeper的訪問端口。oracle

複製Hadoop環境的類庫,覆蓋HBase中的類庫。函數

配置完成,啓動HBase服務。

代碼部分:

hbase安裝

1) 下載安裝hbase

~ http://www.fayea.com/apache-mirror/hbase/hbase-0.94.2/hbase-0.94.2.tar.gz
~ tar xvf hbase-0.94.2.tar.gz

2) 修改配置文件

~ cd hbase-0.94.2/
~ vi conf/hbase-env.sh 

    export JAVA_HOME=/root/toolkit/jdk1.6.0_29
    export HBASE_HOME=/root/hbase-0.94.2
    export HADOOP_INSTALL=/root/hadoop-1.0.3
    export HBASE_CLASSPATH=/root/hadoop-1.0.3/conf
    export HBASE_MANAGES_ZK=true

~ vi conf/hbase-site.xml

    <configuration>
      <property>
        <name>hbase.rootdir</name>
        <value>hdfs://master:9000/hbase</value>
      </property>

      <property>
        <name>hbase.cluster.distributed</name>
        <value>true</value>
      </property>

      <property>
         <name>dfs.replication</name>
         <value>1</value>
      </property>

      <property>
        <name>hbase.zookeeper.quorum</name>
        <value>master</value>
      </property>

      <property>
          <name>hbase.zookeeper.property.clientPort</name>
          <value>2181</value>
      </property>

      <property>
        <name>hbase.zookeeper.property.dataDir</name>
        <value>/root/hadoop/hdata</value>
      </property>
    </configuration>

3) 複製hadoop環境的配置文件和類庫

  ~ cp ~/hadoop-1.0.3/conf/hdfs-site.xml ~/hbase-0.94.2/conf
  ~ cp ~/hadoop-1.0.3/hadoop-core-1.0.3.jar ~/hbase-0.94.2/lib
  ~ cp ~/hadoop-1.0.3/lib/commons-configuration-1.6.jar ~/hbase-0.94.2/lib
  ~ cp ~/hadoop-1.0.3/lib/commons-collections-3.2.1.jar ~/hbase-0.94.2/lib

4) 啓動hadoop和hbase

  ~/hadoop-1.0.3/bin/start-all.sh
  ~/hbase-0.94.2/bin/start-hbase.sh

5) 查看hbase進行

~ jps

    12041 HMaster
    12209 HRegionServer
    31734 TaskTracker
    31343 DataNode
    31499 SecondaryNameNode
    13328 Jps
    31596 JobTracker
    11916 HQuorumPeer
    31216 NameNode

6) 打開hbase命令行客戶端

~/hbase-0.94.2/bin/hbase shell

HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.2, r1395367, Sun Oct  7 19:11:01 UTC 2012

hbase(main):001:0> list

    TABLE
    0 row(s) in 0.0150 seconds

HBase安裝完成。

2. rhbase安裝

文字說明部分:

安裝完成HBase後,咱們還須要安裝Thrift,由於rhbase是經過Thrift調用HBase的。

Thrift是須要本地編譯的,官方沒有提供二進制安裝包,首先下載thrift-0.8.0。

在thrift解壓目錄輸入./configure,會列Thrift在當前機器所支持的語言環境,若是隻是爲了rhbase默認配置就能夠了。
在個人配置中除了但願支持rhbase訪問,還支持PHP,Python,C++。所以須要在系統中,裝一些額外的類庫。你們能夠根據本身的要求,設置Thrift的編譯參數。

編譯並安裝Thrift,而後啓動HBase的ThriftServer服務。

最後,安裝rhbase。

代碼部分:

  1. 下載thrift

    ~ wget http://archive.apache.org/dist/thrift/0.8.0/thrift-0.8.0.tar.gz
    ~ tar xvf thrift-0.8.0.tar.gz
    ~ cd thrift-0.8.0/
  2. 下載PHP支持類庫(可選)

    ~ sudo apt-get install php-cli
  3. 下載C++支持類庫(可選)

    ~ sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
  4. 生成編譯的配置參數

    ~ ./configure
    
      thrift 0.8.0
    
      Building code generators ..... :
    
      Building C++ Library ......... : yes
      Building C (GLib) Library .... : no
      Building Java Library ........ : no
      Building C# Library .......... : no
      Building Python Library ...... : yes
      Building Ruby Library ........ : no
      Building Haskell Library ..... : no
      Building Perl Library ........ : no
      Building PHP Library ......... : yes
      Building Erlang Library ...... : no
      Building Go Library .......... : no
    
      Building TZlibTransport ...... : yes
      Building TNonblockingServer .. : yes
    
      Using Python ................. : /usr/bin/python
    
      Using php-config ............. : /usr/bin/php-config
  5. 編譯和安裝

    ~ make
    ~ make install
  6. 查看thrift版本

    ~ thrift -version
    
      Thrift version 0.8.0
  7. 啓動HBase的Thrift Server

      ~ /hbase-0.94.2/bin/hbase-daemon.sh start thrift
    
      ~ jps 
    
          12041 HMaster
          12209 HRegionServer
          13222 ThriftServer
          31734 TaskTracker
          31343 DataNode
          31499 SecondaryNameNode
          13328 Jps
          31596 JobTracker
          11916 HQuorumPeer
          31216 NameNode
  8. 安裝rhbase

      ~ R CMD INSTALL rhbase_1.1.1.tar.gz

很順利的安裝完成。

3. rhbase的相關函數

hb.compact.table      hb.describe.table     hb.insert             hb.regions.table
hb.defaults           hb.get                hb.insert.data.frame  hb.scan
hb.delete             hb.get.data.frame     hb.list.tables        hb.scan.ex
hb.delete.table       hb.init               hb.new.table          hb.set.table.mode

4. hbase和rhbase的基本操做對比

建表
HBASE:     create 'student_shell','info'
RHBASE:    hb.new.table("student_rhbase","info")

列出全部表
HBASE:     list
RHBASE:    hb.list.tables()

顯示錶結構
HBASE:     describe 'student_shell'
RHBASE:    hb.describe.table("student_rhbase")

插入一條數據
HBASE:     put 'student_shell','mary','info:age','19'
RHBASE:    hb.insert("student_rhbase",list(list("mary","info:age", "24")))

讀取數據
HBASE:     get 'student_shell','mary'
RHBASE:    hb.get('student_rhbase','mary')

刪除表(HBASE須要兩條命令,rhbase僅是一個操做)
HBASE:     disable 'student_shell'
HBASE:     drop 'student_shell'
RHBASE:    hb.delete.table('student_rhbase')

代碼部分:

Hbase Shell

> create 'student_shell','info'
> list

    TABLE
    student_shell

> describe 'student_shell'

   DESCRIPTION                                                          ENABLED
   {NAME => 'student_shell', FAMILIES => [{NAME => 'info', DATA_BLOCK_ true
   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 => 't
   rue'}]}

>  put 'student_shell','mary','info:age','19'
>  get 'student_shell','mary'

  COLUMN                      CELL
  info:age                   timestamp=1365414964962, value=19

> disable 'student_shell'
> drop 'student_shell'

rhbase script

~ R
> library(rhbase)
> hb.init()

    <pointer: 0x16494a0>
    attr(,"class")
    [1] "hb.client.connection"

 >hb.new.table("student_rhbase","info",opts=list(maxversions=5,x=list(maxversions=1L,compression='GZ',inmemory=TRUE)))

   [1] TRUE

> hb.list.tables()

    $student_rhbase
      maxversions compression inmemory bloomfiltertype bloomfiltervecsize
    info:           5        NONE    FALSE            NONE                  0
          bloomfilternbhashes blockcache timetolive
    info:                   0      FALSE         -1

 > hb.describe.table("student_rhbase")

          maxversions compression inmemory bloomfiltertype bloomfiltervecsize
    info:           5        NONE    FALSE            NONE                  0
          bloomfilternbhashes blockcache timetolive
    info:                   0      FALSE         -1

> hb.insert("student_rhbase",list(list("mary","info:age", "24")))

    [1] TRUE

> hb.get('student_rhbase','mary')

    [[1]]
    [[1]][[1]]
    [1] "mary"

    [[1]][[2]]
    [1] "info:age"

    [[1]][[3]]
    [[1]][[3]][[1]]
    [1] "24"

> hb.delete.table('student_rhbase')

    [1] TRUE

RHadoop實踐系列文章的第四篇完成!但願這個四篇文章對你們有所幫助。稍後我可能還會寫一些,關於rmr算法實踐,rhadoop架構方面和hive的使用的相關文章。歡迎你們多提問題,多交流。

相關文章
相關標籤/搜索