安裝Hadoop和Hbasephp
hadoop和hbase版本的選擇以及安裝,參考以前的文章:java
Hadoop的版本選擇和單機模式:http://codingo.xyz/index.php/2016/08/16/hadoop-stand-alone/shell
Hadoop的僞分佈式模式:http://codingo.xyz/index.php/2016/08/16/hadoop_false_distribute/apache
Hbase版本選擇和單機模式入門:http://codingo.xyz/index.php/2016/08/17/hbase_standalone/緩存
Hbase的僞分佈式模式:http://codingo.xyz/index.php/2016/08/16/hadoop_false_distribute/服務器
Hadoop版本:hadoop-2.5.1socket
Hbase版本:Hbase1.2.2分佈式
鏈接Hbaseoop
1.導入jar包測試
將HBASE_HOME/lib下的jar包加入classpath目錄下
2.添加日誌文件
爲了能詳細的瞭解輸出日誌,添加log4j.properties文件
3.添加hbase-site.xml
將HBASE_HOME/conf目錄下hbase-site.xml文件拷貝到classpath目錄下
<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://192.168.111.129:9000/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>192.168.111.129</value> </property> </configuration>
192.168.111.129 是本地虛擬機的ip,根據實際狀況修改
體系結構
來自Hbase權威指南的一張圖片
Hbase Client鏈接Server流程:
首先HBase Client端會鏈接Zookeeper Qurom,經過Zookeeper組件Client能獲知哪一個Server管理-ROOT- Region。那麼Client就去訪問管理-ROOT-的Server,在META中記錄了HBase中全部表信息,(你可使用 scan 'hbase:meta' 命令列出你建立的全部表的詳細信息),從而獲取Region分佈的信息。一旦Client獲取了這一行的位置信息,好比這一行屬於哪一個Region,Client將會緩存這個信息並直接訪問HRegionServer。長此以往Client緩存的信息漸漸增多,即便不訪問hbase:meta表也能知道去訪問哪一個HRegionServer。
因此須要作以下配置:
1.配置hbase.zookeeper.quorum
<property> <name>hbase.zookeeper.quorum</name> <value>192.168.111.129</value> </property>
由於首先鏈接的就是Zookeeper Qurom,因此須要配置ip地址,同時須要對外打開2181端口,不然出現以下錯誤:
2016-09-14 16:27:21 - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 2016-09-14 16:27:22 - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
2.配置hostname
配置完Zookeeper Qurom,執行client程序會有以下問題:
Call exception, tries=11, retries=35, started=60140 ms ago, cancelled=true, msg=row 'table1,row1,99999999999999' on table 'hbase:meta' at region=hbase:meta,,1.1588230740, hostname=bogon,16201,1473841196735, seqNum=0
C:\Windows\System32\drivers\etc的hosts文件加上一句:
192.168.111.129 bogon
bogon是服務器hostname
3.開啓RegionServer端口
由於最後Hbase Client須要和指定的RegionServer進行鏈接,因此須要開啓RegionServer端口,默認端口是:16201,不然有以下錯誤:
2016-09-14 11:26:33 [main] 405::: ERROR org.apache.hadoop.hbase.client.AsyncProcess - Failed to get region location org.apache.hadoop.hbase.TableNotFoundException: testtable at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegionInMeta(ConnectionManager.java:1283) at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:1181) at org.apache.hadoop.hbase.client.AsyncProcess.submit(AsyncProcess.java:395) at org.apache.hadoop.hbase.client.AsyncProcess.submit(AsyncProcess.java:344) at org.apache.hadoop.hbase.client.BufferedMutatorImpl.backgroundFlushCommits(BufferedMutatorImpl.java:238) at org.apache.hadoop.hbase.client.BufferedMutatorImpl.flush(BufferedMutatorImpl.java:190) at org.apache.hadoop.hbase.client.HTable.flushCommits(HTable.java:1434) at org.apache.hadoop.hbase.client.HTable.put(HTable.java:1018) at ch03.PutExample1.main(PutExample1.java:37)
測試實例:
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import common.HBaseHelper; public class PutExample { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), Bytes.toBytes("val1")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("val2")); table.put(put); table.close(); connection.close(); helper.close(); } }
注:以上的‘testtable’是在hbase shell中建立好的
測試結果:
hbase(main):006:0> scan 'testtable' ROW COLUMN+CELL row1 column=colfam1:qual2, timestamp=1473847426654, value=val2 row1 column=colfam1:qual3, timestamp=1473847426654, value=val1 1 row(s) in 0.1690 seconds