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<LongWritable, Text, Text, Text>{
        private Text rowKeyText = new Text();
        private Text value = new Text();

        @Override
        protected void map(LongWritable key, Text text,
                           Mapper<LongWritable, Text, Text, Text>.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<Text, Text, NullWritable> {
        @Override
        protected void reduce(Text k2, Iterable<Text> v2s,
                              Reducer<Text, Text, NullWritable, Mutation>.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);
            }
        }
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值