多种输入的MapReduce程序实例

本文介绍了一种使用Hadoop的MultipleInputs功能整合不同格式数据集的方法,并提供了具体示例代码,展示了如何处理两个分别包含学校简称及全称的文件,最终输出格式化的学校名称数据。

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

数据集

一共有两个文件需要处理,一个为file1,一个为file2。如下。


/test/file1,文件内容如下。每行数据以制表符分开
cqnu 重庆师范大学
cqu 重庆大学


/test/file2,文件内容如下。每行数据以,分开。
thu,清华大学
pku,北京大学
nju,南京大学

任务

把两个文件的内容整合成如下格式
学校简称:cqnu 学校全称:重庆师范大学
学校简称:cqu 学校全称:重庆大学
学校简称:nju 学校全称:南京大学
学校简称:pku 学校全称:北京大学
学校简称:thu 学校全称:清华大学

解决方法

采用MultipleInputs来处理,详见API文档
MultipleInputs

示例代码

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class TestJoin {

    //file1的Mapper
    public static class TestJoinMapperByKeyValue extends Mapper<Text, Text, Text, Text>{

        public void map(Text key, Text value, Context context) throws IOException, InterruptedException{
            context.write(key, value);
        }
    }
    //file2的Mapper 
    public static class TestJoinMapperByText extends Mapper<LongWritable, Text, Text, Text>{

        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
            String []str = value.toString().split(",");
            context.write(new Text(str[0]),new Text( str[1]));
        }
    }

    public static class TestJoinReducer extends Reducer<Text, Text, Text, Text>{

        public void reduce(Text key, Iterable<Text> value, Context context) throws IOException, InterruptedException{
            for(Text v : value){
                Text k = new Text();
                k.set("学校简称:" + key.toString());
                Text val = new Text();
                val.set("学校全称:" + v.toString());
                context.write(k, val);
            }
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if(args.length < 2){
            System.out.println("args must be three");return ;
        }
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,"TestJoin");
        job.setJarByClass(TestJoin.class);


        job.setReducerClass(TestJoinReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        //多种输入
        //不同的输入文件采用不同的Mapper处理
        MultipleInputs.addInputPath(job, new Path(args[0]), KeyValueTextInputFormat.class, TestJoinMapperByKeyValue.class);
        MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, TestJoinMapperByText.class);

        FileOutputFormat.setOutputPath(job, new Path(args[2]));
        System.exit(job.waitForCompletion(true)?0:1);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值