hadoop学习(3)

求学生成绩平均值 数据格式 学生名称 成绩
Map函数

package com.hadoop.myhadoop1;

import java.io.IOException;
import java.util.StringTokenizer;

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

public class Map extends Mapper<LongWritable,Text,Text,LongWritable> {
    @Override
    protected void map(LongWritable key, Text value,
            Context context)
            throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        String line = value.toString();
        System.out.println(line);
        StringTokenizer tokel = new StringTokenizer(line,"\n");
        while (tokel.hasMoreTokens()) {
            StringTokenizer toke2 = new StringTokenizer(tokel.nextToken());
            String strname = toke2.nextToken();
            String strscorent = toke2.nextToken();
            Text name = new Text(strname);
            LongWritable scorent = new LongWritable(Integer.parseInt(strscorent));
            context.write(name, scorent);
        }
    }
}

Reduce函数

package com.hadoop.myhadoop1;

import java.io.IOException;

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

public class Reduce extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> value,
            Context context)
            throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        int sum = 0;
        int count = 0;

        for (LongWritable scort : value) {
            sum += scort.get();
            count++;
        }
        long aver = sum/count;
        context.write(key, new LongWritable(aver));
    }
}

Main函数

package com.hadoop.myhadoop1;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class Process {
    public static void main(String[] args) throws IOException, ReflectiveOperationException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("fs.default.name", "hdfs://192.168.1.108:9000");
        String[] ars = new String[]{"input","output"};
        String[] otherArgs = new GenericOptionsParser(conf,ars).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage:wodcount");
            System.exit(2);
        }
        Job job = new Job(conf, "Score_Process");
        job.setJarByClass(Process.class);
        job.setMapperClass(Map.class);

        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);

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

        FileInputFormat.setInputPaths(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.out.println(job.waitForCompletion(true)? 1 : 0);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值