MapReduce将HDFS数据清洗到多个Hbase表中

最近一直在对历史数据进行清洗,原始数据是纯数据格式,现在要清洗到hbase中,方便后期跟hive进行整合查询。。
可能现在基本上都使用spark来做清洗了,但是如果受机器本身硬件条件的限制的话,就没法子了,spark根本跑不动,哎,还是老老实实的写MR吧。。话不多说,直接上代码。

import com.gey.hbase.helper.HBaseHelper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.MultiTableOutputFormat;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import java.io.IOException;


/**
 * @author ly
 * @date 2019/7/24 13:59
 * @description
 */
public class HBaseMultiTableOutputApp {

    public static class HBaseMultiTableOutputMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split("\t");
            String createdDate = splits[1];
            String enable ="1";
            String id = splits[4];
            String uid = splits[5];
            //生成rowkey
            String rk = HBaseHelper.getRowkey(id,uid);

            String s = id+"\t"+uid+"\t"+createdDate+"\t"+enable;

            context.write(new Text(rk),new Text(s));
        }
    }

    public static class HBaseMultiTableOutputReducer extends Reducer<Text, Text, ImmutableBytesWritable, Put> {
        //我这里是需要根据id取模,然后根据取模的值将数据存入对应的表
        ImmutableBytesWritable userTb0 = null;
        ImmutableBytesWritable userTb1 = null;
        ImmutableBytesWritable userTb2 = null;
        ImmutableBytesWritable userTb3 = null;
        ImmutableBytesWritable userTb4 = null;
        ImmutableBytesWritable userTb5 = null;
        ImmutableBytesWritable userTb6 = null;
        ImmutableBytesWritable userTb7 = null;
        ImmutableBytesWritable userTb8 = null;
        ImmutableBytesWritable userTb9 = null;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            //初始化表
            userTb0 = new ImmutableBytesWritable(Bytes.toBytes("user_0"));
            userTb1 = new ImmutableBytesWritable(Bytes.toBytes("user_1"));
            userTb2 = new ImmutableBytesWritable(Bytes.toBytes("user_2"));
            userTb3 = new ImmutableBytesWritable(Bytes.toBytes("user_3"));
            userTb4 = new ImmutableBytesWritable(Bytes.toBytes("user_4"));
            userTb5 = new ImmutableBytesWritable(Bytes.toBytes("user_5"));
            userTb6 = new ImmutableBytesWritable(Bytes.toBytes("user_6"));
            userTb7 = new ImmutableBytesWritable(Bytes.toBytes("user_7"));
            userTb8 = new ImmutableBytesWritable(Bytes.toBytes("user_8"));
            userTb9 = new ImmutableBytesWritable(Bytes.toBytes("user_9"));
        }

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values) {
                String[] splits = value.toString().split("\t");
                Put put = new Put(Bytes.toBytes(key.toString()));
                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"), Bytes.toBytes(splits[0]));
                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("uid"), Bytes.toBytes(splits[1]));
                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("epochs"), Bytes.toBytes(splits[3]));
                put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("time"), Bytes.toBytes(splits[2]));
                //对id%10
                Long ret = Long.valueOf(splits[0]) % 10;
                if (ret  == 0){
                    context.write(userTb0,put);
                }else if(ret  == 1){
                    context.write(userTb1,put);
                }else if(ret  == 2){
                    context.write(userTb2,put);
                }else if(ret  == 3){
                    context.write(userTb3,put);
                }else if(ret  == 4){
                    context.write(userTb4,put);
                }else if(ret  == 5){
                    context.write(userTb5,put);
                }else if(ret  == 6){
                    context.write(userTb6,put);
                }else if(ret  == 7){
                    context.write(userTb7,put);
                }else if(ret  == 8){
                    context.write(userTb8,put);
                }else if(ret  == 9){
                    context.write(userTb9,put);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception{

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","192.168.32.101,192.168.32.102,192.168.32.103");
        conf.set("hbase.zookeeper.port", "2181");
        conf.set("zookeeper.znode.parent","/hbase");

        //创建job
        Job job = Job.getInstance(conf, "HBaseMultiTableOutputApp");
        //设置job的处理类
        job.setJarByClass(HBaseMultiTableOutputApp.class);


        job.setMapperClass(HBaseMultiTableOutputMapper.class);
        job.setReducerClass(HBaseMultiTableOutputReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        // 设置输入和输出格式
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(MultiTableOutputFormat.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));

        boolean result = job.waitForCompletion(true);
        System.exit(result?1:0);

    }
}

思考:本来我这里不想用Reducer只用Mapper的,但是不行,报错。。也不知道问题出在哪儿,等有空再来研究研究。。以上是清洗到多个Hbase表。。

如果是清洗到一张表的话,就只需要Mapper即可,直接上代码:

import com.gey.hbase.helper.HBaseHelper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;

/**
 * @author ly
 * @date 2019/6/28 09:47
 * @description
 */
public class HDFS2HBaseApp {
    public static class HDFS2HBaseMapper  extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {

        ImmutableBytesWritable rowkey = new ImmutableBytesWritable();

        private static final String ENABLE_TRUE="true";
        private static final String ENABLE_FALSE="false";

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split("\t");
            String createdDate = splits[1];
            String enable = "1";
            String id = splits[4];
            String uid = splits[5];
            
            String rk = HBaseHelper.getRowkey(id,uid);
            rowkey.set(Bytes.toBytes(rk));

            Put put = new Put(Bytes.toBytes(rk));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"), Bytes.toBytes(id));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("uid"), Bytes.toBytes(uid));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("epochs"), Bytes.toBytes(enable));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("time"), Bytes.toBytes(createdDate));
            context.write(rowkey, put);
        }
    }

    public static void main(String[] args) throws Exception{
        //创建Configuration
        Configuration configuration = new Configuration();        
        configuration.set("hbase.zookeeper.quorum","192.168.32.101,192.168.32.102,192.168.32.103");
        configuration.set("hbase.zookeeper.port", "2181");
        configuration.set("zookeeper.znode.parent","/hbase");

        //创建job
        Job job = Job.getInstance(configuration, "HDFS2HBaseApp");
        //设置job的处理类
        job.setJarByClass(HDFS2HBaseApp.class);

        job.setMapperClass(HDFS2HBaseMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(Put.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));

        TableMapReduceUtil.initTableReducerJob(args[1],null,job);
        job.setNumReduceTasks(1);

        boolean result = job.waitForCompletion(true);
        System.exit(result?1:0);
    }
}

注意:如果是放到CDH环境的集群上跑,注意在yarn上配置一下hbase的jar所在路径,不然会报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/io/ImmutableBytesWritable

err.png

解决方法:
在Yarn的配置页面,输入“hadoop-env”,在右侧中填入hbase路径:

HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/opt/cloudera/parcels/CDH-5.16.1-1.cdh5.16.1.p0.3/lib/hbase/lib/*
classpath1.png

保存后,继续输入“yarn.application.”,在右侧添加hbase路径,如下图所示:

classpath2.png

然后重启就OK了。。

欢迎大家留言讨论
内容将同步到微信公众号,欢迎关注微信公众号:LearnBigData


qrcode.jpg
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容