HBase表的數據導出和導入


1. 表數據導出

hbase org.apache.hadoop.hbase.mapreduce.Export test file:///home/hadoop/test (導入到本地)

hbase org.apache.hadoop.hbase.mapreduce.Export test /user/hadoop/test (導入到hdfs上)

#創建一個test表,一個列簇info
hbase(main):004:0* create 'test','info'
0 row(s) in 4.3820 seconds

=> Hbase::Table - test
hbase(main):005:0> put 'test','001','info:name','tom'
0 row(s) in 0.4710 seconds

hbase(main):006:0> put 'test','001','info:age','18'
0 row(s) in 0.0490 seconds

hbase(main):007:0> put 'test','002','info:name','jerry'
0 row(s) in 0.0490 seconds

hbase(main):008:0> put 'test','002','info:age','19'
0 row(s) in 0.0350 seconds

我們將test表導入到hdfs中

[hadoop@SHQZ-PS-IOT-TEST-APP01 ~]$ hbase org.apache.hadoop.hbase.mapreduce.Export  test  /user/hadoop/test

該命令會啟動一個mapreduce程序來完成數據的導出,等待程序執行完成,查看導出后的文件

[hadoop@SHQZ-PS-IOT-TEST-APP01 ~]$ hdfs dfs -ls /user/hadoop/test
Found 2 items
-rw-r--r--   3 hadoop supergroup          0 2018-05-17 21:33 /user/hadoop/test/_SUCCESS
-rw-r--r--   3 hadoop supergroup        284 2018-05-17 21:33 /user/hadoop/test/part-m-00000
[hadoop@SHQZ-PS-IOT-TEST-APP01 ~]$ 

注意:上面以part-m開頭的文件就是導出的數據文件,我們可以看下它的內容

[hadoop@SHQZ-PS-IOT-TEST-APP01 ~]$ hdfs dfs -cat /user/hadoop/test/part-m-00000
SEQ1org.apache.hadoop.hbase.io.ImmutableBytesWritable%org.apache.hadoop.hbase.client.ResultP
l7D~UL001D

001infoage 218

001infoname 2tom (N002F

002infoage 219
!
002infoname 2jerry (

2. 導入數據

導入數據前,一定要在hbase上創建同名表,否則會報錯,找不到表

hbase org.apache.hadoop.hbase.mapreduce.Import test file:///home/hadoop/test (從本地導入)

hbase org.apache.hadoop.hbase.mapreduce.Import test /user/hadoop/test (從hdfs上導入)

# 先刪除之前的test表,然后重新創建
hbase(main):004:0> disable 'test'
0 row(s) in 2.2780 seconds

hbase(main):006:0> drop 'test'
0 row(s) in 1.3260 seconds

#重新創建test表
hbase(main):016:0* create 'test','info'
0 row(s) in 1.2400 seconds

=> Hbase::Table - test

執行命令導入數據,導入目錄一定要是數據文件所在目錄

[hadoop@SHQZ-PS-IOT-TEST-APP01 ~]$ hbase org.apache.hadoop.hbase.mapreduce.Import  test  /user/hadoop/test

和導出命令類似,該命令同樣會啟動一個mapreduce任務來完成數據的導入,之后我們進入hbase shell查看數據是否導入了

hbase(main):020:0* scan 'test'
ROW                                 COLUMN+CELL                                                                                         
 001                                column=info:age, timestamp=1526563694645, value=18                                                  
 001                                column=info:name, timestamp=1526563629119, value=tom                                                
 002                                column=info:age, timestamp=1526563723288, value=19                                                  
 002                                column=info:name, timestamp=1526563706773, value=jerry                                              
2 row(s) in 0.1570 seconds

可以看到,數據已經成功導入。

另外,我在將數據導出到本地,然后再導入到hbase中時,兩個mapreduce任務執行均出現了報錯,但是數據導出和導出成功了,而且數據沒有丟失,尚未搞清楚,待摸索中~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM