南修子学Hbase-Hbase-读hbase表并写入另一张表

Mapper类

package org.nanxiuzi.hbase_demo.mr.read;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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 java.io.IOException;
//key我们这里用rowkey    values直接用put  我们在mapper端进行封装,减低reducer端的压力

public class readStudentMapper extends TableMapper<ImmutableBytesWritable, Put> {

    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
        /**
         * 1001	lixiang	23	beijing
         * 1002	zhaoyun	23	shanghai
         * 我们把之前写入的 name和addr读出来然后写到新表
         */
        //1.构建一个put
        Put put = new Put(key.get());
        //2.读数据
        for (Cell cell : value.rawCells()) {
            if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))
            || "addr".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
                put.add(cell);
            }
        }
        //3.写出数据
        context.write(key,put);
    }
}

Reducer类

package org.nanxiuzi.hbase_demo.mr.read;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.NullWritable;

import java.io.IOException;

public class readStudentReducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable> {
    @Override
    protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
        for (Put value : values) {
            context.write(NullWritable.get(),value);
        }
    }
}

Driver类

package org.nanxiuzi.hbase_demo.mr.read;

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

public class readStudentDriver implements Tool {
    private Configuration configuration = null;

    @Override
    public int run(String[] args) throws Exception {
        String soutceTableName = args[0];
        String targetTableName = args[1];
        //1.获取job对象
        Job job = Job.getInstance(configuration);
        //2.设置驱动类路径
        job.setJarByClass(readStudentDriver.class);
        //3.设置Mapper&Mapper输出的KV类型
        TableMapReduceUtil.initTableMapperJob(soutceTableName,
                new Scan(),
                readStudentMapper.class,
                ImmutableBytesWritable.class,
                Put.class, job);
        //4.设置Reducer类
        TableMapReduceUtil.initTableReducerJob(
                targetTableName,
                readStudentReducer.class,
                job);
        //7.提交任务
        boolean b = job.waitForCompletion(true);
        return b ? 0 : 1;
    }

    @Override
    public void setConf(Configuration conf) {
        configuration = conf;
    }

    @Override
    public Configuration getConf() {
        return configuration;
    }

    public static void main(String[] args) {
        try {
            Configuration conf = new Configuration();
            int run = ToolRunner.run(conf, new readStudentDriver(), args);
            System.exit(run);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

创建新的hbase表

 create 'filestudent2','info'

运行任务

 yarn jar /home/hrx/readFhbase/hbase_demo-1.0-SNAPSHOT.jar org.nanxiuzi.hbase_demo.mr.read.readStudentDriver filestudent filestudent2

查看结果

hbase(main):006:0> scan 'filestudent2'
ROW                      COLUMN+CELL                                                        
 1001                    column=info:addr, timestamp=1617157683187, value=beijing           
 1001                    column=info:name, timestamp=1617157683187, value=lixiang           
 1002                    column=info:addr, timestamp=1617157687929, value=shanghai          
 1002                    column=info:name, timestamp=1617157687929, value=zhaoyun           
 1003                    column=info:addr, timestamp=1617157696338, value=xinjiang          
 1003                    column=info:name, timestamp=1617157696338, value=zhangqinag        
 1005                    column=info:addr, timestamp=1617157692253, value=haerbin           
 1005                    column=info:name, timestamp=1617157692253, value=huansdd           
 1006                    column=info:addr, timestamp=1617158486534, value=nanning           
 1006                    column=info:name, timestamp=1617158486534, value=fengtian          
5 row(s)
Took 0.0472 seconds                                                                         
hbase(main):007:0> 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南修子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值