hdfs数据写入hbase

数据类型:

aaa,123

bbb,234

ccc,345

hbase中第一列作为rowkey,第二列作为value

package cn.xlzx.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Mutation;
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.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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class HdfsImport {
    private static final String TableName = "mobile";
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ip");
        conf.set("hbase.rootdir", "hdfs://ip:9000/hbase");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set(TableOutputFormat.OUTPUT_TABLE, TableName);

        Job job = Job.getInstance(conf, HdfsImport.class.getSimpleName());
        job.setJarByClass(HdfsImport.class);
        TableMapReduceUtil.addDependencyJars(job);
        job.setMapperClass(HDFS2HBaseMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setReducerClass(HDFS2HBaseReducer.class);
        job.setOutputFormatClass(TableOutputFormat.class);
        FileInputFormat.setInputPaths(job, args[1]);
        job.waitForCompletion(true);
    }

    static class HDFS2HBaseMapper extends Mapper{
        private Text rowKeyText = new Text();
        private Text value = new Text();

        @Override
        protected void map(LongWritable key, Text text,
                           Mapper.Context context)
                throws IOException, InterruptedException {
            String[] splits = text.toString().split(",");
            rowKeyText.set(splits[0]);
            value.set(splits[1] + "," + splits[1]);//name\tage
            context.write(rowKeyText, value);
        }
    }
    static class HDFS2HBaseReducer extends TableReducer {
        @Override
        protected void reduce(Text k2, Iterable v2s,
                              Reducer.Context context)
                throws IOException, InterruptedException {
            Put put = new Put(k2.getBytes());

            for (Text text : v2s) {
                String[] splits = text.toString().split(",");
                put.add("info".getBytes(), "moblie".getBytes(), splits[1].getBytes());
                context.write(NullWritable.get(), put);
            }
        }
    }
}
 

你可能感兴趣的:(hadoop)