我們已經了解了使用flume監聽端口以及文件內容,本節將展示使用flume導入數據到hbase。
1 系統、軟件以及前提約束
- CentOS 7 64 工作站 作者的機子ip是192.168.100.200,請讀者根據自己實際情況設置
- 已完成flume安裝並監聽端口數據,flume的服務名稱為a1
https://www.jianshu.com/p/3e4f7db8080f - 已完成hbase安裝
https://www.jianshu.com/nb/37554929 - xshell客戶端
- 為去除權限對操作的影響,所有操作都以root進行
2 操作
- 1 使用xshell登錄到192.168.100.200
- 2 啟動hbase服務,登錄到hbase客戶端,創建表
# 進到hbase啟動目錄
cd /root/hbase-1.2.6/bin
# 啟動hbase服務
./start-hbase.sh
# 登錄到hbase客戶端
./hbase shell
# 在hbase命令行創建一個表
hbase(main):001:0> create 't2','f2'
- 3 修改flume-conf.properties,其中a1為flume的服務名稱
a1.sources = r1
a1.sinks = k1
a1.channels = c1 # Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/hbase.txt
a1.sources.r1.channels = c1 # Describe the sink
a1.sinks.k1.type = logger
a1.sinks.k1.type = hbase
# 與hbase中創建的表名相同
a1.sinks.k1.table = t2
# 與hbase中創建的表的列簇相同
a1.sinks.k1.columnFamily = f2
a1.sinks.k1.serializer = org.apache.flume.sink.hbase.RegexHbaseEventSerializer
a1.sinks.k1.channel = memoryChannel # Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100 # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
- 4 拷貝jar包
# 如有jar包已經存在,選擇y予以覆蓋
cp /root/hbase-1.2.6/lib/* /root/apache-flume-1.8.0-bin/lib/
- 5 啟動flume服務
# 進入flume的啟動目錄
cd /root/apache-flume-1.8.0-bin/bin
# 啟動flume
./flume-ng agent -c ../conf -f ../conf/flume-conf.properties -n a1 -Dflume.root.logger=INFO,console
- 6 測試
# 創建flume監聽的文件
touch /root/hbase.txt
# 向hbase.txt中寫入字符串
echo 'Iamzhangli'>>/root/hbase.txt
echo 'jiangsuwanhe'>>/root/hbase.txt
- 7 查看hbase中t2表的內容
# 登錄到hbase客戶端
./hbase shell
# 查看t2表,我們會看到剛才加入到hbase.txt中的數據已經進入hbase
hbase(main):001:0> scan 't2'
以上就是flume將數據導入Hbase的過程。