用hbase使用mapreduce将数据大批量上传到表中

package HbaseApi.MapreduceApi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.LongWritable;
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 java.io.IOException;
//用hbase使用大批量
public class Write2HbaseDemo {
//将本地的字符串文件转换为Hfie文件上传至表中

    static class MyMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put>
    {

        ImmutableBytesWritable im=new ImmutableBytesWritable();//此类可以经string字符串转换为hfile文件格式,然后通过put操作将Hfile文件放入表中
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {


            String[] split = value.toString().split("\t");//按\t切分
            byte[] bytes = split[0].getBytes();//拿到rowkey
            im.set(bytes);//HFile

            Put put =new Put(bytes);
            put.addColumn("info".getBytes(),"name".getBytes(),"123".getBytes());

            context.write(im,put);//将值put上去
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration entries = HBaseConfiguration.create();
        entries.set("hbase.zookeeper.quorum","192.168.1.137,192.168.1.138,192.168.1.139");
        entries.set("hbase.zookeeper.property.clientPort","2181");
        entries.set("zookeeper.znode.parent","/hbase-unsecure");


        Job exampleRead = Job.getInstance(entries, "ExampleRead");
        exampleRead.setJarByClass(Write2HbaseDemo.class);//输出主类 即main方法所在类
        exampleRead.setMapperClass(MyMapper.class);//map输出类
        exampleRead.setOutputKeyClass(ImmutableBytesWritable.class);//输出key
        exampleRead.setOutputValueClass(Put.class);//输出value

        FileInputFormat.setInputPaths(exampleRead,"/home/alwaysdazz/1.txt");//设置的输入路径
        TableMapReduceUtil.initTableReducerJob("student",null,exampleRead);

        boolean b = exampleRead.waitForCompletion(true);
        if(!b)
        {
            throw new IOException("error with job");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值