HBase表之间的数据迁移(使用MapReduce实现)

目录

一、案例需求:

二、代码实现

0、创建Maven工程并导入依赖

1、自定义Mapper类ReadStudentMapper

2、自定义Reducer类WriteStudentReducer

3、自定义Runner类MRRunner

4、运行代码和查看结果


一、案例需求:

在HBase中有一张测试用表"table777",表中数据如下:

HBase表之间的数据迁移(使用MapReduce实现)_第1张图片

现在需要从该表中抽取每一行数据的部分字段(name、phone),然后写入到HBase中的另一张测试用表"table777_mr",使用MapReduce来实现。

二、代码实现

0、创建Maven工程并导入依赖

    
        
            org.apache.hbase
            hbase-server
            1.3.3
        

        
            org.apache.hbase
            hbase-client
            1.3.3
        
        
            org.apache.hbase
            hbase-common
            1.3.3
        

        
            commons-logging
            commons-logging
            1.2
        
        
            log4j
            log4j
            1.2.17
        
    

1、自定义Mapper类ReadStudentMapper

作用:用于读取table777表中的一行行数据

package xsluo;

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;

public class ReadStudentMapper extends TableMapper {
    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
        //将table777测试表中每条记录的name和phone提取出来,相当于将每一行数据读取出来放入到Put对象中。
        Put put = new Put(key.get());
        //遍历添加column行
        for(Cell cell: value.rawCells()){
            //添加/克隆列族:information
            if("information".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){
                //添加/克隆列:name
                if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
                    //将该列cell加入到put对象中
                    put.add(cell);
                }
            }else if ("contact".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){
                //添加/克隆列族:contact
                //添加/克隆列:phone
                if ("phone".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
                    //将该列cell加入到put对象中
                    put.add(cell);
                }
            }
        }
        //将从table777表读取到的每行数据写入到context中作为map的输出
//        System.out.println(put.toString());
        context.write(key, put);
    }

}

2、自定义Reducer类WriteStudentReducer

作用:用于将读取到的一行行数据写入到 table777_mr 表中

package xsluo;

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 WriteStudentReducer extends TableReducer {
    @Override
    protected void reduce(ImmutableBytesWritable key, Iterable values, Context context) throws IOException, InterruptedException {
        //读出来的每一行数据写入到table777_mr表中
        for (Put put : values) {
            context.write(NullWritable.get(),put);
        }
    }
}

3、自定义Runner类MRRunner

package xsluo;

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.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;
import java.io.IOException;

public class MRRunner extends Configured implements Tool {

    public static void main(String[] args) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        //集群配置↓
        configuration.set("hbase.zookeeper.quorum", "192.168.2.100,192.168.2.101,192.168.2.102");
        configuration.set("hbase.master", "192.168.2.100:60000");
        int status = ToolRunner.run(configuration, new MRRunner(), args);
        System.exit(status);
    }

    public int run(String[] strings) throws Exception {
        //获取Configuration对象
        Configuration conf = this.getConf();

        //创建Job任务
        Job job = Job.getInstance(conf, this.getClass().getSimpleName());
        job.setJarByClass(MRRunner.class);

        //配置Job
        Scan scan = new Scan();
        scan.setCacheBlocks(false);
        scan.setCaching(500);

        //设置Mapper,注意导入的是mapreduce包下的,不是mapred包下的,后者是老版本
        TableMapReduceUtil.initTableMapperJob(
                "table777", //数据源表名
                scan,   //scan扫描控制器
                ReadStudentMapper.class,    //设置Mapper类
                ImmutableBytesWritable.class,   //设置Mapper输出key类型
                Put.class,  //设置Mapper输出value类型
                job //设置给哪个Job
        );

        //设置Reducer
        TableMapReduceUtil.initTableReducerJob(
                "table777_mr",
                WriteStudentReducer.class,
                job
        );

        //设置Reduce数量,最少1个
        job.setNumReduceTasks(1);

        boolean isSuccess = job.waitForCompletion(true);
        if(!isSuccess){
            throw new IOException("Job running with error");
        }
        return isSuccess ? 0 : 1;
    }
}

4、运行代码和查看结果

提示:运行任务前,先启动Zookeeper、Hadoop和HBase服务,然后,如果待数据导入的表不存在,则需要提前创建。

执行完毕后,查看导入结果:

HBase表之间的数据迁移(使用MapReduce实现)_第2张图片

 

你可能感兴趣的:(HBase,Java,hbase)