Hbase篇--Hbase和MapReduce结合Api
2018-01-17 21:27
148 查看
一.前述
Mapreduce可以自定义Inputforma对象和OutPutformat对象,所以原理上Mapreduce可以和任意输入源结合。
二.步骤
将结果写会到hbase中去。
2.1 Main函数
Mapreduce可以自定义Inputforma对象和OutPutformat对象,所以原理上Mapreduce可以和任意输入源结合。
二.步骤
将结果写会到hbase中去。
2.1 Main函数
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; /** * 分析hdfs 文本 统计单词数量 * 结果输出到 hbase表 * create 'wc','cf' * rowkey: 单词 cf:count=单词数量 * @author root * */ public class WCDemo { /** * * wc * 数据hbase表 rowkey cell存放文本 * 结果输出到 hbase表 * */ public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://node1:8020");//设置hdfs集群nameservices名称 conf.set("hbase.zookeeper.quorum", "node4"); Job job = Job.getInstance(conf); job.setJarByClass(WCDemo.class); job.setMapperClass(WCMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // job.setReducerClass(); //addDependencyJars 本地方式运行: 设置为false // TableMapReduceUtil.initTableReducerJob("wc", WCReducer.class, job); TableMapReduceUtil.initTableReducerJob("wc",WCReducer.class, job, null, null, null, null, false); Path path = new Path("/user/wc"); FileInputFormat.addInputPath(job, path); boolean flag = job.waitForCompletion(true); if(flag) { System.out.println("success~~"); } } }2.2 Mapper函数(和正常的Mapper没啥区别)
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split(" "); for (String w : words) { context.write(new Text(w), new IntWritable(1)); } } }2.3 Reduce函数(主要是把Put对象写出去)
import java.io.IOException; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableReducer; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; public class WCReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> { @Override protected void reduce(Text text, Iterable<IntWritable> iterable, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable i : iterable) { sum += i.get(); } Put put = new Put(text.toString().getBytes()); put.add("cf".getBytes(), "count".getBytes(), (sum+"").getBytes()); context.write(null, put); } }
相关文章推荐
- HBase建表高级属性,hbase应用案例看行键设计,HBase和mapreduce结合,从Hbase中读取数据、分析,写入hdfs,从hdfs中读取数据写入Hbase,协处理器和二级索引
- hbase 结合MapReduce 批量导入
- HBase 结合 MapReduce(HBaseToHDFS )
- HBase 结合 MapReduce(HDFSToHBase)
- Hbase结合MapReduce的批量导入
- Hbase结合Mapreduce示例
- MapReduce结合hbase探查二度人脉
- HBase之java api接口调用与mapreduce整合即从hdfs中通过mapreduce来导入数据到hbase中
- Hbase与mapreduce结合使用
- HBase篇--HBase操作Api和Java操作Hbase相关Api
- Hbase结合mapreduce需要导入的hbase lib及部分问题持续更
- 【转载】HDFS+MapReduce+Hive+HBase 十分钟快速入门(二)
- Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
- Hbase与Hadoop结合出现的jar包冲突问题
- 新浪微博API与百度地图结合显示好友分布
- hbase 与 hive 结合
- HBase入门之九HBase Java API
- MapReduce新版客户端API源码分析
- mapreduce新旧api对比