分享

使用hive往hbase当中导入数据

问题导读
1、使用hive往hbase当中导入数据有哪些方式?
2、使用Bulk的方式直接生成HFile,导入hbase中有哪些流程?
3、哪种方式最好,为什么?






 我们可以有很多方式可以把数据导入到hbase当中,比如说用map-reduce,使用TableOutputFormat这个类,但是这种方式不是最优的方式。

  Bulk的方式直接生成HFiles,写入到文件系统当中,这种方式的效率很高。

  一般的步骤有两步
  (1)使用ImportTsv或者import工具或者自己写程序用hive/pig生成HFiles
  (2)用completebulkload把HFiles加载到hdfs上

  ImportTsv能把用Tab分隔的数据很方便的导入到hbase当中,但还有很多数据不是用Tab分隔的 下面我们介绍如何使用hive来导入数据到hbase当中。

  1.准备输入内容
  a.创建一个tables.ddl文件

  1. -- pagecounts data comes from http://dumps.wikimedia.org/other/
  2. pagecounts-raw/
  3. -- documented http://www.mediawiki.org/wiki/Analytics/Wikistats
  4. -- define an external table over raw pagecounts data
  5. CREATE TABLE IF NOT EXISTS pagecounts (projectcode STRING, pagename
  6. STRING, pageviews STRING, bytes STRING)
  7. ROW FORMAT
  8. DELIMITED FIELDS TERMINATED BY ' '
  9. LINES TERMINATED BY '\n'
  10. STORED AS TEXTFILE
  11. LOCATION '/tmp/wikistats';
  12. -- create a view, building a custom hbase rowkey
  13. CREATE VIEW IF NOT EXISTS pgc (rowkey, pageviews, bytes) AS
  14. SELECT concat_ws('/',
  15. projectcode,
  16. concat_ws('/',
  17. pagename,
  18. regexp_extract(INPUT__FILE__NAME, 'pagecounts-(\\d{8}-\\d{6})\
  19. \..*
  20.   b.创建HFils分隔文件,例子:sample.hql
  21. [code]-- prepate range partitioning of hfiles
  22. ADD JAR /usr/lib/hive/lib/hive-contrib-0.11.0.1.3.0.0-104.jar;
  23. SET mapred.reduce.tasks=1;
  24. CREATE TEMPORARY FUNCTION row_seq AS 'org.apache.hadoop.hive.contrib.udf.
  25. UDFRowSequence';
  26. -- input file contains ~4mm records. Sample it so as to produce 5 input
  27. splits.
  28. INSERT OVERWRITE TABLE hbase_splits
  29. SELECT rowkey FROM
  30. (SELECT rowkey, row_seq() AS seq FROM pgc
  31. TABLESAMPLE(BUCKET 1 OUT OF 10000 ON rowkey) s
  32. ORDER BY rowkey
  33. LIMIT 400) x
  34. WHERE (seq % 100) = 0
  35. ORDER BY rowkey
  36. LIMIT 4;
  37. -- after this is finished, combined the splits file:
  38. dfs -cp /tmp/hbase_splits_out/* /tmp/hbase_splits;
复制代码


  c.创建hfiles.hql
  1. ADD JAR /usr/lib/hbase/hbase-0.94.6.1.3.0.0-104-security.jar;
  2. ADD JAR /usr/lib/hive/lib/hive-hbase-handler-0.11.0.1.3.0.0-104.jar;
  3. SET mapred.reduce.tasks=5;
  4. SET hive.mapred.partitioner=org.apache.hadoop.mapred.lib.
  5. TotalOrderPartitioner;
  6. SET total.order.partitioner.path=/tmp/hbase_splits;
  7. -- generate hfiles using the splits ranges
  8. INSERT OVERWRITE TABLE hbase_hfiles
  9. SELECT * FROM pgc
  10. CLUSTER BY rowkey;
复制代码


  2.导入数据
  注意:/$Path_to_Input_Files_on_Hive_Client是hive客户端的数据存储目录
  1. mkdir /$Path_to_Input_Files_on_Hive_Client/wikistats
  2. wget http://dumps.wikimedia.org/other/pagecounts-raw/2008/2008-10/
  3. pagecounts-20081001-000000.gz
  4. hadoop fs -mkdir /$Path_to_Input_Files_on_Hive_Client/wikistats
  5. hadoop fs -put pagecounts-20081001-000000.
  6. gz /$Path_to_Input_Files_on_Hive_Client/wikistats/
复制代码



  3.创建必要的表
  注意:$HCATALOG_USER是HCatalog服务的用户(默认是hcat)
  1. $HCATALOG_USER-f /$Path_to_Input_Files_on_Hive_Client/tables.ddl
复制代码


  执行之后,我们会看到如下的提示:
  1. OK
  2. Time taken: 1.886 seconds
  3. OK
  4. Time taken: 0.654 seconds
  5. OK
  6. Time taken: 0.047 seconds
  7. OK
  8. Time taken: 0.115 seconds
复制代码



  4.确认表已经正确创建
  执行以下语句
  1. $HIVE_USER-e "select * from pagecounts limit 10;"<span style="background-color: rgb(255, 255, 255); line-height: 1.5;"> </span>
复制代码


  执行之后,我们会看到如下的提示:
  1. ...
  2. OK
  3. aa Main_Page 4 41431
  4. aa Special:ListUsers 1 5555
  5. aa Special:Listusers 1 1052
复制代码


  再执行
  1. $HIVE_USER-e "select * from pgc limit 10;"
复制代码


       执行之后,我们会看到如下的提示:
  1. ...
  2. OK
  3. aa/Main_Page/20081001-000000 4 41431
  4. aa/Special:ListUsers/20081001-000000 1 5555
  5. aa/Special:Listusers/20081001-000000 1 1052
  6. ...
复制代码



  5.生成HFiles分隔文件
  1. $HIVE_USER-f /$Path_to_Input_Files_on_Hive_Client/sample.hql
  2. hadoop fs -ls /$Path_to_Input_Files_on_Hive_Client/hbase_splits<span style="background-color: rgb(255, 255, 255); line-height: 1.5;"> </span>
复制代码


  为了确认,执行以下命令
  1. hadoop jar /usr/lib/hadoop/contrib/streaming/hadoop-streaming-1.2.0.1.
  2. 3.0.0-104.jar -libjars /usr/lib/hive/lib/hive-exec-0.11.0.1.3.0.0-104.
  3. jar -input /tmp/hbase_splits -output /tmp/hbase_splits_txt -inputformat
  4. SequenceFileAsTextInputFormat
复制代码


  执行之后,我们会看到如下的提示:
  1. ...
  2. INFO streaming.StreamJob: Output: /tmp/hbase_splits_txt
复制代码


  再执行这一句
  1. hadoop fs -cat /tmp/hbase_splits_txt/*
复制代码


  执行之后,我们会看到类似这样的结果

  1. 1 61 66 2e 71 2f 4d 61 69 6e 5f 50 61 67 65 2f 32 30 30 38 31 30 30 31 2d 30
  2. 30 30 30 30 30 00 (null)
  3. 01 61 66 2f 31 35 35 30 2f 32 30 30 38 31 30 30 31 2d 30 30 30 30 30 30 00
  4. (null)
  5. 01 61 66 2f 32 38 5f 4d 61 61 72 74 2f 32 30 30 38 31 30 30 31 2d 30 30 30
  6. 30 30 30 00 (null)
  7. 01 61 66 2f 42 65 65 6c 64 3a 31 30 30 5f 31 38 33 30 2e 4a 50 47 2f 32 30
  8. 30 38 31 30 30 31 2d 30 30 30 30 30 30 00 (null)
复制代码
  

  7.生成HFiles
  1. HADOOP_CLASSPATH=/usr/lib/hbase/hbase-0.94.6.1.3.0.0-104-security.jar hive -f /$Path_to_Input_Files_on_Hive_Client/hfiles.hql
复制代码


  以上内容是hdp的用户手册中推荐的方式,然后我顺便也从网上把最后的一步的命令格式给找出来了
  1. hadoop jar hbase-VERSION.jar completebulkload /user/todd/myoutput mytable
复制代码




, 1))),
pageviews, bytes
FROM pagecounts;
-- create a table to hold the input split partitions
CREATE EXTERNAL TABLE IF NOT EXISTS hbase_splits(partition STRING)
ROW FORMAT
SERDE 'org.apache.hadoop.hive.serde2.binarysortable.
BinarySortableSerDe'
STORED AS
INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.
HiveNullValueSequenceFileOutputFormat'
LOCATION '/tmp/hbase_splits_out';
-- create a location to store the resulting HFiles
CREATE TABLE hbase_hfiles(rowkey STRING, pageviews STRING, bytes STRING)
STORED AS
INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.hbase.HiveHFileOutputFormat'
TBLPROPERTIES('hfile.family.path' = '/tmp/hbase_hfiles/w');[/code]

  b.创建HFils分隔文件,例子:sample.hql

  1. -- prepate range partitioning of hfiles
  2. ADD JAR /usr/lib/hive/lib/hive-contrib-0.11.0.1.3.0.0-104.jar;
  3. SET mapred.reduce.tasks=1;
  4. CREATE TEMPORARY FUNCTION row_seq AS 'org.apache.hadoop.hive.contrib.udf.
  5. UDFRowSequence';
  6. -- input file contains ~4mm records. Sample it so as to produce 5 input
  7. splits.
  8. INSERT OVERWRITE TABLE hbase_splits
  9. SELECT rowkey FROM
  10. (SELECT rowkey, row_seq() AS seq FROM pgc
  11. TABLESAMPLE(BUCKET 1 OUT OF 10000 ON rowkey) s
  12. ORDER BY rowkey
  13. LIMIT 400) x
  14. WHERE (seq % 100) = 0
  15. ORDER BY rowkey
  16. LIMIT 4;
  17. -- after this is finished, combined the splits file:
  18. dfs -cp /tmp/hbase_splits_out/* /tmp/hbase_splits;
复制代码


  c.创建hfiles.hql
  1. ADD JAR /usr/lib/hbase/hbase-0.94.6.1.3.0.0-104-security.jar;
  2. ADD JAR /usr/lib/hive/lib/hive-hbase-handler-0.11.0.1.3.0.0-104.jar;
  3. SET mapred.reduce.tasks=5;
  4. SET hive.mapred.partitioner=org.apache.hadoop.mapred.lib.
  5. TotalOrderPartitioner;
  6. SET total.order.partitioner.path=/tmp/hbase_splits;
  7. -- generate hfiles using the splits ranges
  8. INSERT OVERWRITE TABLE hbase_hfiles
  9. SELECT * FROM pgc
  10. CLUSTER BY rowkey;
复制代码


  2.导入数据
  注意:/$Path_to_Input_Files_on_Hive_Client是hive客户端的数据存储目录
  1. mkdir /$Path_to_Input_Files_on_Hive_Client/wikistats
  2. wget http://dumps.wikimedia.org/other/pagecounts-raw/2008/2008-10/
  3. pagecounts-20081001-000000.gz
  4. hadoop fs -mkdir /$Path_to_Input_Files_on_Hive_Client/wikistats
  5. hadoop fs -put pagecounts-20081001-000000.
  6. gz /$Path_to_Input_Files_on_Hive_Client/wikistats/
复制代码



  3.创建必要的表
  注意:$HCATALOG_USER是HCatalog服务的用户(默认是hcat)
  1. $HCATALOG_USER-f /$Path_to_Input_Files_on_Hive_Client/tables.ddl
复制代码


  执行之后,我们会看到如下的提示:
  1. OK
  2. Time taken: 1.886 seconds
  3. OK
  4. Time taken: 0.654 seconds
  5. OK
  6. Time taken: 0.047 seconds
  7. OK
  8. Time taken: 0.115 seconds
复制代码



  4.确认表已经正确创建
  执行以下语句
  1. $HIVE_USER-e "select * from pagecounts limit 10;"<span style="background-color: rgb(255, 255, 255); line-height: 1.5;"> </span>
复制代码


  执行之后,我们会看到如下的提示:
  1. ...
  2. OK
  3. aa Main_Page 4 41431
  4. aa Special:ListUsers 1 5555
  5. aa Special:Listusers 1 1052
复制代码


  再执行
  1. $HIVE_USER-e "select * from pgc limit 10;"
复制代码


       执行之后,我们会看到如下的提示:
  1. ...
  2. OK
  3. aa/Main_Page/20081001-000000 4 41431
  4. aa/Special:ListUsers/20081001-000000 1 5555
  5. aa/Special:Listusers/20081001-000000 1 1052
  6. ...
复制代码



  5.生成HFiles分隔文件
  1. $HIVE_USER-f /$Path_to_Input_Files_on_Hive_Client/sample.hql
  2. hadoop fs -ls /$Path_to_Input_Files_on_Hive_Client/hbase_splits<span style="background-color: rgb(255, 255, 255); line-height: 1.5;"> </span>
复制代码


  为了确认,执行以下命令
  1. hadoop jar /usr/lib/hadoop/contrib/streaming/hadoop-streaming-1.2.0.1.
  2. 3.0.0-104.jar -libjars /usr/lib/hive/lib/hive-exec-0.11.0.1.3.0.0-104.
  3. jar -input /tmp/hbase_splits -output /tmp/hbase_splits_txt -inputformat
  4. SequenceFileAsTextInputFormat
复制代码


  执行之后,我们会看到如下的提示:
  1. ...
  2. INFO streaming.StreamJob: Output: /tmp/hbase_splits_txt
复制代码


  再执行这一句
  1. hadoop fs -cat /tmp/hbase_splits_txt/*
复制代码


  执行之后,我们会看到类似这样的结果

  1. 1 61 66 2e 71 2f 4d 61 69 6e 5f 50 61 67 65 2f 32 30 30 38 31 30 30 31 2d 30
  2. 30 30 30 30 30 00 (null)
  3. 01 61 66 2f 31 35 35 30 2f 32 30 30 38 31 30 30 31 2d 30 30 30 30 30 30 00
  4. (null)
  5. 01 61 66 2f 32 38 5f 4d 61 61 72 74 2f 32 30 30 38 31 30 30 31 2d 30 30 30
  6. 30 30 30 00 (null)
  7. 01 61 66 2f 42 65 65 6c 64 3a 31 30 30 5f 31 38 33 30 2e 4a 50 47 2f 32 30
  8. 30 38 31 30 30 31 2d 30 30 30 30 30 30 00 (null)
复制代码
  

  7.生成HFiles
  1. HADOOP_CLASSPATH=/usr/lib/hbase/hbase-0.94.6.1.3.0.0-104-security.jar hive -f /$Path_to_Input_Files_on_Hive_Client/hfiles.hql
复制代码


  以上内容是hdp的用户手册中推荐的方式,然后我顺便也从网上把最后的一步的命令格式给找出来了
  1. hadoop jar hbase-VERSION.jar completebulkload /user/todd/myoutput mytable
复制代码




已有(1)人评论

跳转到指定楼层
buildhappy 发表于 2014-9-22 08:37:12
点赞 学习了  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条