HBase学习笔记(5)——MapReduce操作Hbase

本文介绍如何使用HBase和MapReduce实现大数据统计,通过自定义Mapper和Reducer类,统计HBase表中的行数。文章详细解释了TableMapper和TableReducer的实现原理及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现原理

Hbase对MapReduce提供支持,它实现了TableMapper类和TableReducer类,我们只需要继承这两个类即可

写重Mapper类继承TableMapper<Text, IntWritable>

map的参数(ImmutableBytesWritable key, Result value, Context context)
ImmutableBytesWritable key:行键
Result value: 一行的所有结果
Context context:上下文搬运

重写Reducer类继承TableReducer<Text, IntWritable, KEYOUT>

reduce(Text key, Iterable values, Context context)
key:从map接收到的入的key
values:从map接收到的入的value
contex:上下文搬运

统计该表格中一共有多少行数据

代码入下:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.stream.StreamSupport;

/**
 * 该类的作用:
 * 统计该表格中一共有多少行数据
 * 注:一个rowkey表示一行
 */
public class CountMR extends Configured implements Tool {
    public static void main(String[] args) throws Exception {
        ToolRunner.run(new CountMR(),args);
    }
    @Override
    public int run(String[] strings) throws Exception {
        //任务的配置
        //获得当前的运行环境变量,如果不是在本机上跑,用set设置运行的环境
        Configuration conf=getConf();
        //获得job
        Job job = Job.getInstance();
        //对job设置运行主类
        job.setJarByClass(this.getClass());
        //设置运行job的名字
        job.setJobName("CountMR");
        //配置hbasr的mapper,table名字是输入获得,对表进行扫描的scan,运行的mapper类是CMappaer.class,map输出的key是Text 类,和输出的value是IntWritable类
        TableMapReduceUtil.initTableMapperJob(conf.get("intable"),new Scan(),CMappaer.class,Text.class,IntWritable.class,job);
        //配置hbase的reducer,输出的表名,装配的reducer,job
        TableMapReduceUtil.initTableReducerJob(conf.get("outtable"),CReducer.class,job);
        //提交任务
        job.waitForCompletion(true);
        return 0;
    }

    //TableMapper自带去hbase中读取数据
    //TableMapper每次读取一行数据,即一个rowkey
    //key是rowkey   valuse是result(一行内容)
    public static class CMappaer extends TableMapper<Text, IntWritable>{
        @Override                       
        //ImmutableBytesWritable key行键   Result value: 一行的所有结果     Context contex上下文搬运
        //******value是值,大部分操作是对value*********
        protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
            TableSplit sp = (TableSplit)context.getInputSplit();  //获得表名
            String tb_name=Bytes.toString(sp.getTableName());
            //将表名设置为map的输出key,value设置为1(表示一行)
            context.write(new Text(tb_name),new IntWritable(1));
        }
    }
    //TableReduce 输出的key value
    //key值不论写什么都不输出
    //value值必须组成一个Put Delete Update
    //Text是输入的key类型 , IntWritable是输入的key类型,NullWritable是key的输出类型
    public static class CReducer extends TableReducer<Text,IntWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            long sum = StreamSupport.stream(values.spliterator(), false).count();   //获得所有values的数值总和
            //进行数据输出
            //把数据装载到一个put中进行输出
            //表名 =rowkey
            //行数 info:count
            //接收结果的表需要自己预先定义
            Put put=new Put(key.getBytes());
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("count"),Bytes.toBytes(sum+""));
            context.write(NullWritable.get(),put);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值