安裝環境
jdk1.8
zookeeper3.4.14
hbase 2.2.1
hadoop 2.8.5
版本兼容一覽
首推清華鏡像下載站下載所需包
清華鏡像
單機版zookeeper部署
解壓下載好的zookeeper包,到自己的目錄
cd zookeeper/
進入zookeeper目錄
創建文件夾data 用來存儲同步信息
mkdir data
進入到conf目錄
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
修改添加zoo.cfg文件內容如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/zookeeper/data #這是剛剛創建的data目錄用來存儲同步信息
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=localhost:2888:3888 #單機版添加一個server即可
然後再回到創建的data目錄:
touch myid
echo 1 >> myid
進入到bin目錄
./zkServer.sh start
./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper/bin/../conf/zoo.cfg
Mode: standalone
發現是standalone即可
安裝Hbase
解壓下載好的hbase包到自定義的目錄
wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.2.1/hbase-2.2.1-bin.tar.gz
進入到hbase目錄下,將lib中zookeeper的jar包換成自己相應版本的
[root@hadoop lib]# cp /usr/zookeeper/zookeeper-3.4.14.jar ./
將hadoop配置文件中的hdfs-site.xml 和 core-site.xml 複製一份到hbase的conf目錄下
修改hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop:9000/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name> #這裏是來自ZooKeeper的配置zoo.cfg的屬性。存儲快照的目錄
<value>/usr/hbase/zookeeper</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
修改hbase-env.sh
# The java implementation to use. Java 1.8+ required.
export JAVA_HOME=/usr/java
# Tell HBase whether it should manage it's own instance of ZooKeeper or not.這裏選擇使用自己的ZK,不用他自帶的
export HBASE_MANAGES_ZK=false
配置環境變量/etc/profile
# HBase Environment Variable
export HBASE_HOME=/usr/hbase
export PATH=$HBASE_HOME/bin:$PATH
並使得環境配置生效source /etc/profile
啓動hbase(注意要提前啓動hdfs和zookeeper,保證9000端口可訪問)
start-hbase.sh
輸入命令jps可發現如下信息
16867 Jps
11783 NodeManager
11145 NameNode
14954 HMaster //這個
11851 JobHistoryServer
9804 QuorumPeerMain
14482 HRegionServer //這個
9238 Bootstrap
11478 SecondaryNameNode
11671 ResourceManager
11259 DataNode
如有以上兩項,即可
訪問頁面 localhost:16010
簡單使用
輸入指令 hbase shell
進入hbase控制檯
新建命名空間:
create_namespace 'ns1'
列出所有表
list
列出某一命名空間下的表
list_namespace_tables 'ns1'
新建表
create 'ns1:t1', 'cf1'
含義爲新建一個以命名空間ns1的表t1,列族爲cf1。
插入數據
put 'ns1:t1','1','cf1:name','孫悟空'
put 'ns1:t1','1','cf1:sex','男'
put 'ns1:t1','1','cf1:age','18'
put 'ns1:t1','2','cf1:name','安琪拉'
put 'ns1:t1','2','cf1:sex','女'
put 'ns1:t1','2','cf1:age','19'
put 'ns1:t1','3','cf1:name','狄仁傑'
put 'ns1:t1','3','cf1:sex','男'
put 'ns1:t1','3','cf1:age','20'
查看錶數據:
hbase> scan 'ns1:t1'
hbase> scan 'ns1:t1', {LIMIT=>5} # 查看前5行數據
由於hbase是十六進制存儲所以將會看到十六進制中文
hbase> scan 'ns1:t1'
ROW COLUMN+CELL
1 column=cf1:age, timestamp=1538030949261, value=18
1 column=cf1:name, timestamp=1538030949174, value=\xE5\xAD\x99\xE6\x82\x9F\xE7\xA9\xBA
1 column=cf1:sex, timestamp=1538030949219, value=\xE7\x94\xB7
2 column=cf1:age, timestamp=1538030949393, value=19
2 column=cf1:name, timestamp=1538030949314, value=\xE5\xAE\x89\xE7\x90\xAA\xE6\x8B\x89
2 column=cf1:sex, timestamp=1538030949350, value=\xE5\xA5\xB3
3 column=cf1:age, timestamp=1538030950752, value=20
3 column=cf1:name, timestamp=1538030949448, value=\xE7\x8B\x84\xE4\xBB\x81\xE6\x9D\xB0
3 column=cf1:sex, timestamp=1538030949487, value=\xE7\x94\xB7
3 row(s) in 0.0230 seconds
可以通過第三方平臺來顯示漢字如hue
注意:在綁定hue時要在hue.ini中配置,且啓動ThriftServer守護進程
hbase-daemon.sh start thrift
先啓動hdfs–>hbase–>ThriftServer–>hue
快照
新建快照
snapshot 'sourceTable', 'snapshotName'
例如:
snapshot 'ns1:t1', 'ns1_t1'
查看所有快照
list_snapshots
刪除表
disable 'ns1:t1'
drop 'ns1:t1'
從快照複製生成一個新表:
hbase> clone_snapshot 'ns1_t1', 'ns1:t1'
分區:
在新建表時創建10個預分區
create 'ns1:stu', 'cf1', {SPLITS => ['1', '2', '3', '4', '5', '6', '7', '8', '9']}
則每個分區的Start Key和End Key分別爲:
(~, 1),[1, 2),[2, 3),[3, 4),[4, 5),[5, 6),[6, 7),[7, 8),[8, 9),[9, ~)
查看HDFS是可以看到這個表下有10個文件夾(如果沒有預分區,則只有一個文件夾)
手動分區
一開始沒有分區的表
使用切分鍵’1’一分爲二:
hbase> split 'tableName', 'splitKey'
hbase> split 'gld:stu', '1'
繼續將第二個Region進行切分,以’2’作爲切分鍵:
hbase> split 'regionName', 'splitKey'
hbase> split 'gld:stu,1,1538223641947', '2'
合併分區
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME'
hbase> merge_region 'eea56b5303ed9aca9aa2b4adc14798b8','e8324c9b047282a608588c8826484f5d'
刪除命名空間
hbase> drop_namespace 'ns1'
該命名空間必須爲空否則會報錯!
庫都刪了趕快跑路吧-_-