多种输入的MapReduce程序实例

26 篇文章 0 订阅

数据集

一共有两个文件需要处理,一个为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);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的MapReduce项目实例,实现了对文本中单词出现次数的统计: 1. 定义自己的三个类:DcMapper,DcReducer,DcDriver ```java public class DcMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public class DcReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public class DcDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(DcDriver.class); job.setMapperClass(DcMapper.class); job.setCombinerClass(DcReducer.class); job.setReducerClass(DcReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 2. 编写完代码后,将其打包成JAR文件。 3. 将JAR文件上传到Hadoop集群中,并执行以下命令: ```shell hadoop jar wordcount.jar com.xijing.mapreduce.DcDriver /input /output ``` 其中,/input是输入文件路径,/output是输出文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值