当前位置:   article > 正文

Hadoop mapreduce课程设计-全球历史平均气温数据分析_hadoop课程设计

hadoop课程设计


前言

例如:随着大数据的不断发展,hadoop这门技术也越来越重要,很多人都开启了学习大数据之路。此次课程设计,我们采用mongodb作为存储,javaweb作为前端,echarts作为可视化工具,kettle和pandas作为数据清洗工具。使用底层mapeduce作为大数据计算。

一、工具介绍

mongodb数据库:它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:*面向集合存储,易存储对象类型的数据。*模式自由。*支持动态查询。*支持完全索引,包含内部对象。*支持查询。

javaweb:个人喜好用java,其实最好可以使用node.js。

echarts:可视化工具,包含多个组件,可以说是既简单又方便的工具。

kettle:Pentaho Data Integration以Java开发,支持跨平台运行,其特性包括:支持100%无编码、拖拽方式开发ETL数据管道;可对接包括传统数据库、文件、大数据平台、接口、流数据等数据源;支持ETL数据管道加入机器学习算法。

pandas:python第三方库,对于简单数据是很好用的工具。

二、mapreduce数据处理

1.数据集准备

 2.要求:对不同洲的平均温度处理--得到各大洲的平均温度

2.1 mapper阶段

  1. import org.apache.hadoop.mapreduce.Mapper;
  2. import java.io.IOException;
  3. /*
  4. 要求:对不同洲的平均温度处理--得到各大洲的平均温度
  5. */
  6. public class TemperMapper extends Mapper<LongWritable, Text,Text,DoubleWritable> {
  7. Text k = new Text();
  8. DoubleWritable v = new DoubleWritable();
  9. @Override
  10. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
  11. String s = value.toString();
  12. String[] split = s.split(",");
  13. String sec=split[0]+"\t"+split[6];
  14. if(!split[7].contains("-99")) {
  15. k.set(sec);
  16. v.set(Double.parseDouble(split[split.length - 1]));
  17. context.write(k, v);
  18. }
  19. }
  20. }

2.2 reduce阶段

  1. package test.temperature.TemperatureInfo.Temperature01;
  2. import org.apache.hadoop.io.LongWritable;
  3. import org.apache.hadoop.io.*;
  4. import org.apache.hadoop.mapreduce.Reducer;
  5. import java.io.IOException;
  6. public class TemperReduce extends Reducer<Text, DoubleWritable, Text,DoubleWritable> {
  7. @Override
  8. protected void reduce(Text key, Iterable<DoubleWritable> values, Reducer<Text, DoubleWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
  9. int num=0;
  10. double sum=0;
  11. for (DoubleWritable val:values){
  12. sum+=val.get();
  13. num++;
  14. }
  15. Double avg= Double.valueOf(sum/num);
  16. context.write(key,new DoubleWritable(avg));
  17. }
  18. }

2.3 分区

  1. package test.temperature.TemperatureInfo.Temperature01;
  2. import org.apache.hadoop.io.*;
  3. import org.apache.hadoop.mapreduce.Partitioner;
  4. public class TemperPartitioner extends Partitioner<Text, DoubleWritable> {
  5. @Override
  6. public int getPartition(Text text, DoubleWritable doubleWritable, int p) {
  7. String year_coun = text.toString();
  8. int partition;
  9. if ((year_coun).contains("Africa")){
  10. partition=0;
  11. }
  12. else if ((year_coun).contains("Asia")) {
  13. partition = 1;
  14. }else if ((year_coun).contains("Australia/South Pacific")) {
  15. partition = 2;
  16. }else if ((year_coun).contains("Europe")) {
  17. partition = 3;
  18. }else if((year_coun).contains("Middle East")){
  19. partition=4;
  20. }else {
  21. partition=5;
  22. }
  23. return partition;
  24. }
  25. }

2.4 Driver阶段

  1. package test.temperature.TemperatureInfo.Temperature01;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.*;
  5. import org.apache.hadoop.mapreduce.Job;
  6. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  7. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  8. import java.io.IOException;
  9. public class TemperDriver {
  10. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  11. // 1 获取配置信息以及获取job对象
  12. Configuration conf = new Configuration();
  13. Job job = Job.getInstance(conf);
  14. // 2 关联本Driver程序的jar
  15. job.setJarByClass(TemperDriver.class);
  16. // 3 关联Mapper和Reducer的jar
  17. job.setMapperClass(TemperMapper.class);
  18. job.setReducerClass(TemperReduce.class);
  19. // 4 设置Mapper输出的kv类型
  20. job.setMapOutputKeyClass(Text.class);
  21. job.setMapOutputValueClass(DoubleWritable.class);
  22. // 5 设置最终输出kv类型
  23. job.setOutputKeyClass(Text.class);
  24. job.setOutputValueClass(DoubleWritable.class);
  25. //设置分区--分区与输出是不一样的,输出!=分区
  26. job.setPartitionerClass(TemperPartitioner.class);
  27. job.setNumReduceTasks(6);
  28. // 6 设置输入和输出路径
  29. FileInputFormat.setInputPaths(job, new Path("D:\\desk\\city_temperature.csv"));
  30. FileOutputFormat.setOutputPath(job, new Path("D:\\desk\\Temperature\\25年各大洲的平均温度"));
  31. // 7 提交job
  32. boolean result = job.waitForCompletion(true);
  33. System.exit(result ? 0 : 1);
  34. }
  35. }

3.结果展示

 

 4.将数据放入mongodb数据库

4.1 ktr展示

4.2 mongodb数据展示

 

 

5.使用pandas和pyecharts将数据可视化

5.1 代码展示

  1. import pandas as pd
  2. from numpy import double
  3. from pyecharts import options as opts
  4. from pyecharts.charts import *
  5. from pyecharts.globals import *
  6. def zone_Temperature():
  7. df = pd.read_csv(r"D:\desk\Temperature\25年各大洲的平均温度\part-r-00000.csv", encoding='utf-8',header=None)
  8. df2 = pd.read_csv(r"D:\desk\Temperature\25年各大洲的平均温度\part-r-00001.csv", encoding='utf-8',header=None)
  9. df3 = pd.read_csv(r"D:\desk\Temperature\25年各大洲的平均温度\part-r-00002.csv", encoding='utf-8',header=None)
  10. df4 = pd.read_csv(r"D:\desk\Temperature\25年各大洲的平均温度\part-r-00003.csv", encoding='utf-8',header=None)
  11. df5 = pd.read_csv(r"D:\desk\Temperature\25年各大洲的平均温度\part-r-00004.csv", encoding='utf-8',header=None)
  12. dfc=df[0].str.split("\t")
  13. dfc2=df2[0].str.split("\t")
  14. dfc3=df3[0].str.split("\t")
  15. dfc4=df4[0].str.split("\t")
  16. dfc5=df5[0].str.split("\t")
  17. label=dfc[0][0]
  18. label2=dfc2[0][0]
  19. label3=dfc3[0][0]
  20. label4=dfc4[0][0]
  21. label5=dfc5[0][0]
  22. year=[]
  23. temper,temper2,temper3,temper4,temper5=[],[],[],[],[]
  24. for i in range(0,25):
  25. year.append(dfc[i][1])
  26. temper.append(dfc[i][2])
  27. temper2.append (dfc2[i][2])
  28. temper3.append (dfc3[i][2])
  29. temper4.append (dfc4[i][2])
  30. temper5.append (dfc5[i][2])
  31. line=(
  32. Line()
  33. .add_xaxis(xaxis_data=year)
  34. .add_yaxis(series_name=label,
  35. y_axis=temper)
  36. .add_yaxis (series_name=label2,
  37. y_axis=temper2)
  38. .add_yaxis (series_name=label3,
  39. y_axis=temper3)
  40. .add_yaxis (series_name=label4,
  41. y_axis=temper4)
  42. .add_yaxis (series_name=label5,
  43. y_axis=temper5)
  44. .set_global_opts(title_opts=opts.TitleOpts(title="主要大洲25年平均温度")
  45. ,yaxis_opts=opts.AxisOpts(min_=60))
  46. .set_series_opts(
  47. markline_opts=opts.MarkLineOpts (
  48. data=[
  49. opts.MarkPointItem(type_="average", name="平均值")
  50. ]
  51. ),
  52. )
  53. )
  54. return Line

5.2 调用python函数生成html ,html展示

 6.定义前端jsp页面,将html嵌入sp

7.在前端中展示mongodb数据库内容

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/768148
推荐阅读
相关标签
  

闽ICP备14008679号