Hbase篇--Hbase和MapReduce结合Api

一.前述

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);
    }
}

 

转载于:https://www.cnblogs.com/LHWorldBlog/p/8299420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值