hbase mapreduce例子

参考:

http://hbase.apache.org/book/mapreduce.html

http://genius-bai.iteye.com/blog/641927

 

 

 

HBase 自带例子

hbase-0.20.3\src\test

计算表的总行数(org.apache.hadoop.hbase.mapreduce.RowCounter)

bin/hadoop jar /home/iic/hbase-0.20.3/hbase-0.20.3.jar rowcounter scores grade

结果

10/04/12 17:08:05 INFO mapred.JobClient:     ROWS=2

 

对HBase的列进行Lucene索引(examples.TestTableIndex)

对表mrtest的列contents进行索引,使用lucene-core-2.2.0.jar,需把它加入类路径。lucene-core-2.2.0.jar加入到examples.zip/lib目录下,同时代码中必须指定job.setJarByClass(TestTableIndex.class);不然lucene不识别

 

bin/hadoop fs -rmr testindex

bin/hadoop jar  examples.zip  examples.TestTableIndex

 

 

先从文件中产生适合HBase的HFiles文件,再倒入到Hbase中,加快导入速度

examples.TestHFileOutputFormat

输入的数据,由例子自动生成,其中Key是前面补0的十位数“0000000001”。

输出数据目录:/user/iic/hbase-hfile-test

bin/hadoop fs -rmr hbase-hfile-test

bin/hadoop jar  examples.zip  examples.TestHFileOutputFormat

加载生成的数据到Hbae中(要先安装JRuby,才能执行)

export PATH=$PATH:/home/iic/jruby-1.4.0/bin/

echo $PATH

 

vi bin/loadtable.rb

require '/home/iic/hbase-0.20.3/hbase-0.20.3.jar'
require '/home/iic/hadoop-0.20.2/hadoop-0.20.2-core.jar'
require '/home/iic/hadoop-0.20.2/lib/log4j-1.2.15.jar'
require '/home/iic/hadoop-0.20.2/lib/commons-logging-1.0.4.jar'
require '/home/iic/hbase-0.20.3/lib/zookeeper-3.3.0.jar'
require '/home/iic/hbase-0.20.3/lib/commons-cli-2.0-SNAPSHOT.jar'

$CLASSPATH <<'/home/iic/hbase-0.20.3/conf';

 

delete table "hbase-test" 

 

jruby  bin/loadtable.rb  hbase-test  /user/iic/hbase-hfile-test

 

查看其使用方式

(bin/hbase org.jruby.Main bin/loadtable.rb

Usage: loadtable.rb TABLENAME HFILEOUTPUTFORMAT_OUTPUT_DIR

其使用JRuby

 

注意:此种方式,必须解决几个问题

1:your MapReduce job ensures a total ordering among all keys ,by default distributes keys among reducers using a Partitioner that hashes on the map task output key。(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks

默认MR在使用默认的default hash Partitioner 分配Key给Reducer的时候,如果Key是0~4,有2个Task,则

reducer 0 would have get keys 0, 2 and 4 whereas reducer 1 would get keys 1 and 3 (in order).

则生成的Block里面的Start key 和 End Key次序讲混乱,

 

System.out.println((new ImmutableBytesWritable("0".getBytes())
    .hashCode() & Integer.MAX_VALUE)

 

所以需要实现自己的Hash Partitioner ,生成the keys need to be orderd so reducer 0 gets keys 0-2 and reducer 1 gets keys 3-4 (See TotalOrderPartitioner up in hadoop for more on what this means).

 

验证导入的行数

bin/hadoop jar /home/iic/hbase-0.20.3/hbase-0.20.3.jar rowcounter hbase-test info 

 

HFile生成例子2:

此种例子,只适合第一次海量导入数据,因为bin/loadtable.rb每次都替换所有的文件。

对于后续的数据操作,可以使用Map文本文件+Hbase Table直接操作Insert的功能。

或者保证新增加的Key跟原来没有冲突,按照bin/loadtable.rb的逻辑,添加新的Block。

 

生成1KW数据的test_1kw.log:0,content0,1271222976817

/home/bmb/jdk1.6.0_16/bin/java  -cp examples.zip examples.CreateLogFile  test_1kw.log 10000000

bin/hadoop fs -put test_1kw.log hadoop-performance-test

只用1个Reduce Task,避免Total Order Key的问题

bin/hadoop jar examples.zip examples.TestCreateHFileMR  hadoop-performance-test  hadoop-hbase-hfile-test  1

生成Hbase Hfile文件才花了一点时间,比性能测试生成1KW的HBase数据快了N多。

10/04/15 14:22:59--10/04/15 14:25:22

导入Hbase

jruby  bin/loadtable.rb  hbase-test2  hadoop-hbase-hfile-test

 

验证导入的行数

bin/hadoop jar /home/iic/hbase-0.20.3/hbase-0.20.3.jar rowcounter hbase-test2 info

 

其他HBase MapReduce例子
http://www.hadoop.org.cn/mapreduce/hbase-mapreduce/

http://www.spicylogic.com/allenday/blog/2008/08/28/hbase-bulk-load-import-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值