current position:Home>MapReduce average score how to find the average of each line

MapReduce average score how to find the average of each line

2022-02-02 18:26:39 CSDN Q & A

img




Refer to the answer 1:
package com.hpu.hadoop.test;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class MyMapper extends Mapper<LongWritable, Text,Text, DoubleWritable> {    private Text K;    private DoubleWritable V;    @Override    protected void setup(Context context) throws IOException, InterruptedException {        K = new Text();        V = new DoubleWritable();    }    @Override    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {        String[] split = value.toString().split("\t");        K.set(split[0]);        double sum = Double.parseDouble(split[1]) + Double.parseDouble(split[2]) + Double.parseDouble(split[3]);        V.set(sum/3);        context.write(K,V);    }}
package com.hpu.hadoop.test;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.DoubleWritable;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 = Job.getInstance(conf);        job.setJarByClass(MyDriver.class);        job.setMapperClass(MyMapper.class);        job.setNumReduceTasks(0);        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(DoubleWritable.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(DoubleWritable.class);        // Specify the input 、 The output path         FileInputFormat.setInputPaths(job,new Path(""));        FileOutputFormat.setOutputPath(job,new Path(""));        boolean b = job.waitForCompletion(true);        System.exit(b?0:1);    }}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021826378339.html

Random recommended