MapReduce通过两条路径,两个map,一个job,写到两张HBase表中的做法

本文介绍了如何使用MapReduce通过两个不同的Map任务和一个Job,将数据写入到两张HBase表中。通过设置MapReduce的MultiTableOutputFormat类,可以实现多表输出,而TableOutputFormat.OUTPUT_TABLE配置则用于指定单一表的输出。在Mapper类中,动态设置表名、列族和列名,实现了灵活的数据写入。作者还分享了大数据交流群的信息,供爱好者们交流讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

emmm我知道可以使用一个map和一个job写到多张表,但是,貌似没有找到别人像我这么做的,所以我就写出来试试
map1:

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

static class Mapper1 extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put>{
    private ImmutableBytesWritable tbl1 = new ImmutableBytesWritable (Bytes.toBytes(1));
    @Override
    public void map(LongWritable key ,Text value, Context context) throws IOException,InterruptedException{
        if(逻辑){
            byte[] rowKey = Bytes.toBytes(主键名);
            Put p =new Put(rowKey);
            p.addColumn(Bytes.toBytes(列族名),Bytes.toBytes(列名),Bytes.toBytes());
            //具体各种操作之类的略
            context.write(tbl1,p);
        }
    }
}

然后是map2,也是一样的:

static class Mapper2 extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put>{
    private ImmutableBytesWritable tbl2 = new ImmutableBytesWritable (Bytes.toBytes(2));
    @Override
    public void map(LongWritable key ,Text value, Context context) throws IOException,InterruptedException{
        if(逻辑){
            byte[] rowKey = Bytes.toBytes(主键名);
            Put p =new Put(rowKey);
            p.addColumn(Bytes.toBytes(列族名),Bytes.toBytes(列名),Bytes.toBytes());
            //具体各种操作之类的略
            context.write(tbl2,p);
        }
    }
}

Driver类:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.mapreduce.MultiTableOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;

static class HBaseDriver extends Configured implements Tool{

		@Override
        public int run(String[] strings) throws Exception {
            Job job = Job.getInstance(getConf(),getClass().getSimpleName());
            job.setJarByClass(getClass());
            Configuration conf = job.getConfiguration();
            conf.set("hbase.zookeeper.quorum", "master");
            //hbase存储数据的地方,hbase-site.xml中配置
            conf.set("hbase.rootdir", "hdfs://master:9000/hbase");
			//conf的其他设置就略啦
			
            Path path1=new Path(strings[0]);
            Path path2 = new Path(strings[1]);

            MultipleInputs.addInputPath(job,path1,TextInputFormat.class,Mapper1.class);
            MultipleInputs.addInputPath(job,path2,TextInputFormat.class,Mapper2.class);

            job.setOutputFormatClass(MultiTableOutputFormat.class);
            job.setNumReduceTasks(0);

            return job.waitForCompletion(true)? 0 : 1;
        }

        public static void main(String[] args) throws Exception {
            int exitCode= ToolRunner.run(HBaseConfiguration.create(),new HBaseDriver(),args);
            System.exit(exitCode);
        }
}

job.setOutputFormatClass(MultiTableOutputFormat.class);这个设置能够输出到多张表中,将map的输出类型改成ImmutableBytesWritable行键和Put添加的内容。
而job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE,“表名”)只能输入到某个表中。
当然,如果说表的名字想也写成动态的,可以啊。
在Driver类中,加入:

conf.set("table1",args[2]);
conf.set("table2",args[3]);

这两行的位置要靠前,在MultipleInputs之前
然后在Mapper类中加入如下:

public  void setup(Context context){
		tbl1 = new ImmutableBytesWritable(context.getConfiguration().get("table1"));
	}

类似如此,其他的诸如列族名,列名,也可以通过这样的方式去设置。

大数据扯淡交流群:808565587,没有培训机构,没有杂七杂八,纯粹的交流群,只有爱与同行交流的你。欢迎大家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值