Hbase API 整合MapReduce (将HBASE一个表按照需要把数据输出到另一个表中)

package com.czxy.com.czxy.demo05;

import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
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.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class HbaseTableDirver extends Configured implements Tool {
    public static class tableMap extends TableMapper<Text, Put> {
        // 读取 myuser表 将name这一列保存到另一个表中

        @Override
        protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
            // 获取rowKey
            byte[] bytes = key.get();
            // 获取每行name的数据
            byte[] name = value.getValue(Bytes.toBytes("f1"), Bytes.toBytes("name"));
            //创建put对象
            Put put = new Put(bytes);
            // 添加信息
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), name);
            // 输出信息
            context.write(new Text(Bytes.toString(bytes)), put);
        }
    }

    public static class tableReudce extends TableReducer<Text, Put, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
            // 遍历输出
            for (Put value : values) {
                context.write(NullWritable.get(), value);
            }
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration con = new Configuration();
        con.set("hbase.zookeeper.quorum", "node01:2181");
        Job job = Job.getInstance(con);
        job.setJarByClass(HbaseTableDirver.class);
        TableMapReduceUtil.initTableMapperJob(TableName.valueOf("myuser"), new Scan(), tableMap.class, Text.class, Put.class, job);
        TableMapReduceUtil.initTableReducerJob("myuser4", tableReudce.class, job);
        boolean b = job.waitForCompletion(true);
        return b ? 0:1;
    }
  // 启动方法
    public static void main(String[] args) throws Exception {
        ToolRunner.run(new HbaseTableDirver(), args);
    }
}
发布了42 篇原创文章 · 获赞 47 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/qq_43791724/article/details/103626543
今日推荐