hadoop空气质量分析

数据资源下载:空气质量数据下载
基于map reducer对数据进行分析实现下面的结果输出
在这里插入图片描述
在这里插入图片描述
代码设计:重写map,reducer类,再写一个运行实现类。

package PMCount;

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

 
 
public class AirMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
 
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    
    	  String line = value.toString();
        String[] fields = line.split(",");
        int year = Integer.parseInt(fields[1].substring(0,4));//要求只要年份不需要月份日期,进行数据处理。
        String city = fields[0];
        String quality = fields[2];
        String place = year + " " + city + " " +quality;//分析数据可知可以合并写入当作键值
        context.write(new Text(place),new LongWritable(1));
 
    }
}
package PMCount;

import java.io.IOException;

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


public class AirReducer extends Reducer<Text, LongWritable, Text, LongWritable>{

    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for (LongWritable value : values) {      
        	count++;
        }
        context.write(key,new LongWritable(count));
    } 
}
package PMCount;


import java.io.IOException;
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 org.apache.log4j.BasicConfigurator;




public class AirRunner {

	// Driver端的代码:八股文
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();

        // 如果输出目录已经存在,则先删除
        FileSystem fileSystem = FileSystem.get(configuration);
        Path outputPath = new Path(args[1]);
        if(fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath,true);
        }

        Job job = Job.getInstance(configuration);
        job.setJarByClass(AirRunner.class);

        job.setMapperClass(AirMapper.class);
        job.setReducerClass(AirReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

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

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, outputPath);

        job.waitForCompletion(true);
    }

		 
}

方式一:本地导入数据,本地运行
在这里插入图片描述
方式二:打jar再Hadoop上运行
打包
在这里插入图片描述
上传数据到Hadoop
在这里插入图片描述
运行jar包
在这里插入图片描述
查看数据
在这里插入图片描述

**拓展:**基于map reducer框架例如此题中的,如下
Mapper<LongWritable, Text, Text, LongWritable>
Reducer<Text, LongWritable, Text, LongWritable>
对于传入的LongWritable, Text, Text, LongWritable这几种对象,也能传入自己定义的对象类,实现对与其他要求的数据统计,或者其他功能和要求。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值