当前位置:   article > 正文

【hadoop】汽车销售数据统计分析项目(部分)_hadoop大数据开发实战十一章汽车销售

hadoop大数据开发实战十一章汽车销售

来源:《hadoop大数据开发实战》

实验一:统计乘用车辆和商用车辆的数量和销售额分布

设计思路:

首先,写一个Mapper来映射输出所有乘用车辆(feiyingyun)和商用车辆(yingyun)的记录。

然后,写一个reduce统计出乘用车辆和商用车辆各自的数量,写一个map的映射集合中,其中key是车辆类型,value为车辆类型的数量。

同时,定义一个成员变量,统计乘用车辆和商用车辆的总和。

最后,重写reduce中的cleanup方法,在其中计算出乘用车辆和商用车辆各自的销售额分布

然后,输出到HDFS分布式文件系统中。

程序代码:

  1. package car;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.DoubleWritable;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Mapper;
  13. import org.apache.hadoop.mapreduce.Reducer;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. class CountMap extends Mapper<LongWritable,Text,Text,LongWritable>{
  17. public void map(
  18. LongWritable key,
  19. Text value,
  20. org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>.Context context)
  21. throws java.io.IOException,InterruptedException{
  22. String[] owns=value.toString().trim().split(",");
  23. if(null!=owns&&owns.length>2&&owns[2]!=null) {
  24. if(owns[2].equals("feiyingyun")) {
  25. context.write(new Text("chengyong"), new LongWritable(1));
  26. }else {
  27. context.write(new Text("shangyong"), new LongWritable(1));
  28. }
  29. }
  30. }
  31. }
  32. class CountReduce extends Reducer<Text,LongWritable,Text,DoubleWritable>{
  33. Map<String,Long>maps=new HashMap<String,Long>();
  34. double all=0;
  35. public void reduce(Text key,java.lang.Iterable<LongWritable>values,org.apache.hadoop.mapreduce.Reducer<Text,LongWritable,Text,DoubleWritable>.Context context) throws java.io.IOException,InterruptedException{
  36. long sum=0;
  37. for(LongWritable val:values) {
  38. sum+=val.get();
  39. }
  40. all+=sum;
  41. maps.put(key.toString(), sum);
  42. };
  43. protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, DoubleWritable>.Context context)throws java.io.IOException,InterruptedException {
  44. Set<String>keySet=maps.keySet();
  45. for(String str:keySet) {
  46. long value=maps.get(str);
  47. double percent=value/all;
  48. context.write(new Text(str),new DoubleWritable(percent));
  49. }
  50. };
  51. }
  52. public class Car{
  53. public static void main(String[] args)throws Exception{
  54. Configuration conf=new Configuration();
  55. Job job1=Job.getInstance(conf,Car.class.getName());
  56. job1.setJarByClass(Car.class);
  57. job1.setMapperClass(CountMap.class);
  58. job1.setReducerClass(CountReduce.class);
  59. job1.setMapOutputKeyClass(Text.class);
  60. job1.setMapOutputValueClass(LongWritable.class);
  61. job1.setOutputKeyClass(Text.class);
  62. job1.setOutputValueClass(DoubleWritable.class);
  63. FileInputFormat.addInputPath(job1, new Path(args[0]));
  64. FileOutputFormat.setOutputPath(job1,new Path(args[1]));
  65. job1.waitForCompletion(true);
  66. }
  67. }

car.txt 

  1. shanxi,3,feiyingyun
  2. shanxi,3,yingyun
  3. shanxi,3,feiyingyun
  4. shanxi,3,yingyun
  5. shanxi,3,feiyingyun
  6. shanxi,3,feiyingyun
  7. shanxi,3,feiyingyun
  8. shanxi,3,yingyun
  9. shanxi,3,feiyingyun
  10. shanxi,3,yingyun
  11. shanxi,3,feiyingyun
  12. shanxi,3,feiyingyun
  13. shanxi,3,yingyun

 将上述代码在Eclipse中打包为CountCar.jar。

接下来将car.txt上传到Hadoop分布式文件系统HDFS的根目录下,之后提交本次Job程序。

hadoop jar ./CountCar.jar /car.txt  /car

实验二:统计某年每个月的汽车销售数量的比例

设计思路:

通过一个Mapper映射输出每个月份的汽车销售记录

再通过一个reduce计算出每个月份的销售总数

同时将所有月份的销售数量进行累加

然后用每个月份的汽车销售总数除以各个月份的销售总和,就计算出了每个月的汽车销售数量的比例。

程序代码:

  1. package car1;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.DoubleWritable;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Mapper;
  13. import org.apache.hadoop.mapreduce.Reducer;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. class MouthMap extends Mapper<Object,Text,Text,IntWritable>{
  17. public void map(
  18. Object key,
  19. Text value,
  20. org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable>.Context context)
  21. throws java.io.IOException,InterruptedException{
  22. String[] str=value.toString().trim().split(",");
  23. if(null!=str&&str[1]!=null) {
  24. context.write(new Text(str[0]), new IntWritable(Integer.parseInt(str[1])));
  25. }
  26. }
  27. }
  28. class MouthReduce extends Reducer<Text,IntWritable,Text,DoubleWritable>{
  29. Map<String,Integer>map=new HashMap<String,Integer>();
  30. int all=0;
  31. public void reduce(Text key,java.lang.Iterable<IntWritable>value,org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,DoubleWritable>.Context context) throws java.io.IOException,InterruptedException{
  32. int count=0;
  33. for(IntWritable con:value) {
  34. count+=con.get();
  35. }
  36. all+=count;
  37. map.put(key.toString(), count);
  38. };
  39. protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, DoubleWritable>.Context context)throws java.io.IOException,InterruptedException {
  40. Set<String>keys=map.keySet();
  41. for(String key:keys) {
  42. int value=map.get(key);
  43. double percent=value*1.0/all;
  44. context.write(new Text(key),new DoubleWritable(percent));
  45. }
  46. };
  47. }
  48. public class MouthCount2 {
  49. public static void main(String[] args)throws Exception{
  50. Configuration conf=new Configuration();
  51. Job job=Job.getInstance(conf,MouthCount2.class.getName());
  52. job.setJarByClass(MouthCount2.class);
  53. job.setMapperClass(MouthMap.class);
  54. job.setReducerClass(MouthReduce.class);
  55. job.setMapOutputKeyClass(Text.class);
  56. job.setMapOutputValueClass(IntWritable.class);
  57. job.setOutputKeyClass(Text.class);
  58. job.setOutputValueClass(DoubleWritable.class);
  59. FileInputFormat.addInputPath(job, new Path(args[0]));
  60. FileOutputFormat.setOutputPath(job,new Path(args[1]));
  61. job.waitForCompletion(true);
  62. }
  63. }

 car_month.txt

  1. 1,10
  2. 2,12
  3. 3,10
  4. 4,8
  5. 5,9
  6. 6,12
  7. 7,1
  8. 8,2
  9. 9,3
  10. 10,4
  11. 11,5
  12. 12,6

下面操作与实验一类似

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

闽ICP备14008679号