MapReduce hbase to hdfs

hbase 表数据如下

具体代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
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.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class HbaseToHdfs {

    public static class HbaseToHdfsMapper extends TableMapper<NullWritable, Text> {
        private Text outPut = new Text();
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper.Context context) throws IOException, InterruptedException {
            //
            StringBuffer value_res = new StringBuffer();

            for (KeyValue kv : value.raw()) {

                String clumn_value = new String(kv.getValue(), "utf-8");
                value_res.append(clumn_value + ",");
            }
            //删除逗号
            //value_res.delete(value_res.length()-1,value_res.length());

            StringBuffer value_row = new StringBuffer();
            String clumn_row = new String(value.getRow(), "utf-8");
            value_row.append(clumn_row+",").append(value_res.delete(value_res.length()-1,value_res.length()));

            outPut.set(value_row.toString());
            context.write(NullWritable.get(), outPut);
        }
    }

    public static Job createSubmittableJob(Configuration conf, String[] args) throws IOException {
        String hbase_table_name = args[0];
        String outPut = args[1];
        conf.set("hbase_table_name", hbase_table_name);
        conf.set("outPut", outPut);
        Job job = Job.getInstance(conf, "HbaseToHdfs");
        job.setJarByClass(HbaseToHdfs.class);
        job.setMapperClass(HbaseToHdfsMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setNumReduceTasks(0);
        TableMapReduceUtil.initTableMapperJob(hbase_table_name, new Scan(), HbaseToHdfsMapper.class, NullWritable.class, Text.class, job);
        Path path = new Path(outPut);
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        FileOutputFormat.setOutputPath(job, path);
        return job;
    }

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {
        Configuration conf = HBaseConfiguration.create();
        //设置压缩格式
        conf.set("mapreduce.output.fileoutputformat.compress", "false");
        //设置内存
        conf.set("mapreduce.map.memory.mb", "2048");

        conf.set("hbase.zookeeper.property.clientPort", "9501");
        conf.set("hbase.zookeeper.quorum", "jfhadoop005,jfhadoop006,jfhadoop007");

        String[] otherArgs = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (otherArgs.length != 2) {
            System.exit(-1);
        }
        Job job = createSubmittableJob(conf, otherArgs);
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

结果:

成功!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值