HBase的MR实现和hive的整合

本文详细介绍了如何实现HBase的MapReduce操作,包括数据读写和批量加载。同时,对比了HBase与Hive的特点,并阐述了如何整合Hive与HBase,通过Hive SQL查询HBase数据,以及创建Hive内部表和外部表映射HBase表的步骤。
摘要由CSDN通过智能技术生成

Hbase的MR实现

需求一:从mysuer表中读取数据,然后写入到Hbase的另一张表中

HBaseReadWrite:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class HBaseReadWrite extends Configured implements Tool {
   
    @Override
    public int run(String[] strings) throws Exception {
   
        Scan scan = new Scan();
        Configuration conf = super.getConf();
        Job job = Job.getInstance(super.getConf(), "hbaseReadWrite");
        TableMapReduceUtil.initTableMapperJob("myuser",scan, HBaseMapper.class, Text.class, Put.class,job);
        TableMapReduceUtil.initTableReducerJob("myuser2", HBaseReducer.class,job);

        job.setNumReduceTasks(2);

        boolean verbose = true;
        boolean b = job.waitForCompletion(verbose);

        return  b?0:1 ;




    }




    public static void main(String[] args) throws Exception {
   

        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");

        int run = ToolRunner.run(configuration, new HBaseReadWrite(), args);
        System.exit(run);

    }

}

HBaseMapper

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.List;

public class HBaseMapper extends TableMapper <Text, Put> {
   

    @Override
    protected void map(ImmutableBytesWritable key, Result result, Context context) throws IOException, InterruptedException {
   

        byte[] rowKeyBytes = key.get();

        String rowkey = Bytes.toString(rowKeyBytes);

        List<Cell> cells = result.listCells();

        for (Cell cell : cells) {
   

            byte[] family = cell.getFamily();
            byte[] qualifier = cell.getQualifier();
            byte[] rowArray = cell.getRowArray();
            Put put = new Put(rowKeyBytes);

            if ("f1".equals(Bytes.toString(family))) {
   
                if ("name".equals(Bytes.toString(qualifier)) && "age".equals(Bytes.toString(qualifier))) {
   

                    put.add(cell);


                }
                if (!put.isEmpty()) {
   
                    context.write(new Text(rowkey), put);
                }
            }


        }


    }
}

HBaseReducer

     import</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值