利用Hadoop中的Mapreducer处理数据

数据集合

1949-10-01 14:21:02    34℃
1949-10-02 14:01:02    36℃
1950-01-01 11:21:02    32℃
1950-10-01 12:21:02    37℃
1949-11-02 14:01:02    37℃
1951-12-01 12:21:02    23℃
1950-10-02 12:21:02    41℃
1950-10-03 12:21:02    27℃
1951-07-01 12:21:02    45℃
1951-07-02 12:21:02    46℃
1950-11-13 12:21:02    37℃
1951-08-08 12:21:02    48℃
1949-07-07 14:01:02    39℃
1949-05-05 14:01:02    32℃
1951-03-03 12:21:02    22℃

Mapper类

package com.neuedu.mapper;


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

import java.io.IOException;


public class temperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private Text outk = new Text();
    private IntWritable outv = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] tokens = line.split("\t");
        String date = tokens[0].trim();
        String tem = tokens[1];
        int temp;
        temp =Character.getNumericValue(tem.charAt(0))*10+Character.getNumericValue(tem.charAt(1));

        String year = date.substring(0, 4);
        outk.set(year);
        outv.set(temp);
        context.write(outk, outv);
    }
}

Reducer类

package com.neuedu.reduce;


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

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparator;

public class temperatureReducer extends Reducer<Text, IntWritable,Text, IntWritable> {
    private Text outk = new Text();
    private IntWritable outv = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
       int max = Integer.MIN_VALUE;

        for (IntWritable value : values) {
            if(value.get()>=max) {
                max = value.get();
            }
        }
        context.write(key, new IntWritable(max));
    }
}

Driver类

package com.neuedu.driver;

import com.neuedu.mapper.temperatureMapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;

import com.neuedu.reduce.temperatureReducer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class temperatureDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(temperatureDriver.class);
        job.setMapperClass(temperatureMapper.class);
        job.setReducerClass(temperatureReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        Path path = new Path("C:\\Users\\72389\\Desktop\\temperature.txt");
        FileInputFormat.setInputPaths(job,path);
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\72389\\Desktop\\output"));
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

运行结果

1949    39
1950    41
1951    48
 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值