多MapReduce任务的串联案列(统计单词在每个文件中出现的次数)

任务需求:

 分析:

 代码:

导包:

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-common</artifactId>
            <version>2.10.0</version>
        </dependency>

    </dependencies>

MyMapper1类:

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class MyMapper1 extends Mapper<LongWritable, Text,Text,LongWritable> {
    String fileName="";
    //得到该单词的文件名
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        fileName=inputSplit.getPath().getName();
    }

    //以单词+文件名作为key
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line=value.toString();
        String[] words = line.split(" ");
        Text k=new Text();
        for (String word : words) {
            k.set(word+"-->"+fileName);
            context.write(k,new LongWritable(1));

        }
    }
}

MaReducer1类:

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class MyReducer1 extends Reducer<Text, LongWritable,Text,LongWritable> {
    //统计以单词+文件名作为key出现的次数
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        for (LongWritable l:
             values) {
            sum+=l.get();
        }
        context.write(key,new LongWritable(sum));

    }
}

MyMapper2类:

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MyMapper2 extends Mapper<LongWritable, Text,Text,Text> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line=value.toString();
        String[] ss = line.split("\\t");
        String word=ss[0].split("-->")[0];//得到单词
        String fileName=ss[0].split("-->")[1];//得到文件名
        String count=ss[1];
        //将文件名与次数进行拼接
        context.write(new Text(word),new Text(fileName+"-->"+count));
     
    }
}

MyReducer2类:

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MyReducer2 extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        String s="";
        for (Text value : values) {
            s=s+value.toString()+"\t";
        }
        context.write(key,new Text(s));
    }
}

MyJob类:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //第一个job
        Job job1 = Job.getInstance(conf,"job1");

        job1.setJarByClass(MyDriver.class);
        job1.setMapperClass(MyMapper1.class);
        job1.setReducerClass(MyReducer1.class);

        job1.setOutputKeyClass(Text.class);
        job1.setOutputValueClass(LongWritable.class);

        FileInputFormat.addInputPath(job1,new Path("/home/hadoop/temp/job"));
        FileOutputFormat.setOutputPath(job1,new Path("/home/hadoop/temp/job_RES"));

        FileSystem.get(conf).delete(new Path("/home/hadoop/temp/job_RES"),true);


       job1.waitForCompletion(true);

        
        //第二个job
        Job job2=Job.getInstance(conf,"job2");
        job2.setJarByClass(MyDriver.class);
        job2.setMapperClass(MyMapper2.class);
        job2.setReducerClass(MyReducer2.class);

        job2.setOutputKeyClass(Text.class);
        job2.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job2,new Path("/home/hadoop/temp/job_RES"));
        FileOutputFormat.setOutputPath(job2,new Path("/home/hadoop/temp/job_Output"));

        FileSystem.get(conf).delete(new Path("/home/hadoop/temp/job_Output"),true);


        boolean b = job2.waitForCompletion(true);

        System.exit(b?0:1);


    }
}

猜你喜欢

转载自blog.csdn.net/qq_52135683/article/details/126709465
今日推荐