MapReduce hdfs文件写入hbase表

这篇博客介绍了如何使用Apache Hadoop的MapReduce框架将存储在HDFS上的文件内容写入到HBase表格中。通过创建Mapper和Reducer类,实现了从输入文件解析数据并构造Put对象,然后在Reducer阶段将这些Put对象写入到指定的HBase表中。

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

@[TOMapReduce hdfs文件写入hbase表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
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.input.FileInputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class HDFS2HBASE {

private static class HdfsToHbaseMapper extends Mapper<LongWritable, Text, Text, Put> {
    private String family = "cf";
    private String[] qualifier = {"id","gender","age"};
    //private String[] qualifier = {"id","gender","age","name"};
    private String hdfs_split = ",";
    private String hbase_split = "|";
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //拿到一行文件按分割符分割
        String[] fileds = value.toString().split(this.hdfs_split);
        //读取配置文件
        Configuration conf = context.getConfiguration();
        String row_key = getKey(conf.get("hdfs_index"), fileds);

// String rowvalue = getValue(fileds);
if(fileds.length==qualifier.length){
Put put = new Put(Bytes.toBytes(row_key));
for(int i=0;i<fileds.length;i++){
String qul=qualifier[i];
String val=fileds[i];
put.add(family.getBytes(), qul.getBytes(), val.getBytes());
}
context.write(new Text(row_key), put);
}else {
System.out.println(" ERROR: value length must equale qualifier length “);
//System.err.println(” ERROR: value length must equale qualifier length ");
}

    }

    //for 得到key值
    public String getKey(String index, String[] fileds) {
        String[] indexs = index.split(this.hdfs_split);
        StringBuffer res_key = new StringBuffer();
        //res_key.append(fileds[Integer.parseInt(indexs[0])]);
        for (int i = 0; i < indexs.length; i++) {
            res_key.append(fileds[java.lang.Integer.parseInt(indexs[i])] + this.hbase_split);
        }
        //return res_key.toString();
        return res_key.deleteCharAt(res_key.length() - 1).toString();
    }

    //for 得到value值
    public String getValue(String[] flies) {
        StringBuffer res_value = new StringBuffer();
        //res_value.append(flies[1]);
            for (int i = 1; i < flies.length; i++) {
                res_value.append(flies[i] + ",");
            }


        //res_value.delete(res_value.length()-1,res_value.length());
        //res_value.deleteCharAt(res_value.length()-1);
        return res_value.deleteCharAt(res_value.length() - 1).toString();
    }
}

public static class HdfsToHbaseReducer extends TableReducer<Text, Put, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
        //读出来的每一行数据写入到 fruit_hdfs 表中
        for (Put put : values) {
            context.write(NullWritable.get(), put);
        }
    }
}

public static Job createSubmittableJob(Configuration conf, String[] args) throws IOException {
    String hbase_table_name = args[0];
    String hbase_inPath = args[1];
    String hdfs_index = args[2];
    conf.set("hbase_table_name", hbase_table_name);
    conf.set("hbase_inPath", hbase_inPath);
    conf.set("hdfs_index", hdfs_index);
    Job job = Job.getInstance(conf, HDFS2HBASE.class.getSimpleName());
    TableMapReduceUtil.initTableReducerJob(
            hbase_table_name,        // output table
            HdfsToHbaseReducer.class,    // reducer class
            job);
    job.setNumReduceTasks(0);
    job.setReducerClass(HdfsToHbaseReducer.class);
    job.setJarByClass(HDFS2HBASE.class);
    job.setMapperClass(HdfsToHbaseMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Put.class);
    FileInputFormat.addInputPath(job, new Path(args[1]));
    job.setOutputFormatClass(TableOutputFormat.class);
    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", "11001");//TODO zk client端口   9501
    //conf.set("hbase.zookeeper.quorum", "192.168.21.106,192.168.21.107,192.168.21.108");//TODO zk 服务器  jfhadoop005,jfhadoop006,jfhadoop007
    //conf.set("hbase.zookeeper.quorum", "jfhadoop273,jfhadoop274,jfhadoop275");

    //生产环境
    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 != 3) {
        System.out.println("Wrong number of arguments: " + otherArgs.length);
        System.exit(-1);
    }
    Job job = createSubmittableJob(conf, otherArgs);
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值