开发者学堂课程【HBase入门教程:HBase MapReduce_2】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/397/detail/5097
HBase MapReduce_2
一、例子
WCJob.java
public static void main(String[]args) throws Exception{
//默认加载 src 下的配置文件
Configuration conf= new Configuration();
conf.set("hbase.zookeeper.quorum","node1,node2,node3");
Job job=Job.getInstance(conf);
conf.set("mapred.jar","D:\\MR\\wc.jar");
Job job=Job.getInstance(comf);
job.setJarByCIass(WCJob.class);
job.setMapperClass(WCMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapoutputValueC1a8s(IntWritable.class);
FileInputFormat.addInputPath(jobnew Path ("/wc/ input/wc"));
// hbase reducer
String targetTable="wc";
TableMapReduceUtil.initTableReducerJob(
targetTable,
// reduce 输出的到哪一张表
WCTableReducer.class, // reducer class job);
boolean flag=job.waitForCompletion(true);
if (flag) {
System.out.println("job success ! ");
}
}
环境方式和 jdk 已经配置好了,并且需要修改源码。运行时候报错了,src 下需要加载文件,把配置文件先设置好,
还是放在 conf 下边,设置完成之后,再次执行,刷新之后,发现程序,还在 run
WCJob.java
public static void main(String[]args) throws Exception{
//默认加载 src 下的配置文件
Configuration conf= new Configuration();
conf.set("hbase.zookeeper.quorum","node1,node2,node3");
Job job=Job.getInstance(conf);
conf.set("mapred.jar","D:\\MR\\wc.jar");
Job job=Job.getInstance(comf);
job.setJarByCIass(WCJob.class);
job.setMapperClass(WCMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapoutputValueC1a8s(IntWritable.class);
FileInputFormat.addInputPath(jobnew Path ("/wc/ input/wc"));
// hbase reducer
String targetTable="wc";
TableMapReduceUtil.initTableReducerJob(
targetTable,
// reduce 输出的到哪一张表
WCTableReducer.class, // reducer class job);
boolean flag=job.waitForCompletion(true);
if (flag) {
System.out.println("job success ! ");
}
}
参数需要按照 TableReducer 这个规则来写,迭代器里有 values,map 输出之后,迭代器里有1,Bytes.toBytes (key.toString())往数据库里插一个数据,通过 bytes 转化成二进制数,把 key 这个单词作为 rokey,每个单词作为一行数据,列族写的是 cf,count 作为总数,定义一个变量位 count。需要创建一张表,这张表里列族名称是 cf。
创建一个表
输入:Creat ‘wc‘,’cf’
回车
0 row(s) in 0.4010 S ec onds
=> Hbase: : Table – wc
hbase( main) :005:0> list
TABLE
phone
tbl
wc
3 row(s) in 0.0080 sec onds
=>[ " phone"
,"tbl", "wc" ]
hbase(main) :006 : 0> scan ‘wc’
ROW COLUMN+CELL
0 row(s) in 0.0190 seconds
hbase(main) 007 ; 0>