猫眼电影网站电影_时长数_分析与可视化

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


public class Movie8Mapper extends Mapper<LongWritable,Text,Text, DoubleWritable> {

    @Override
    protected void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
        //super.map(key,value,context);
        String line=value.toString();   //[0 芳华 9.1 http://maoyan.com/films/1170264 剧情,爱情,战争 中国大陆 大陆上映 136 2017]
        String[] arr=line.split("\00");
        String year=line.substring(line.length()-4);  //从后往前读取
        double time=0;
        if (arr.length>7){
            time=Double.parseDouble(arr[7]);
//            System.out.println(time);
            context.write(new Text(year),new DoubleWritable(time));
        }

    }
}
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class Movie8Reducer extends Reducer<Text, DoubleWritable,Text,DoubleWritable> {
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        //super.reduce(key, values, context);
        double sum=0;
        double sc=0;
        double  avg_time;
        for (DoubleWritable i:values){
            sum+=i.get();
            sc++;
        }
        String str=String.format("%.2f",sum/sc);  //保留两位小数
        avg_time=Double.parseDouble(str);
        context.write(key,new DoubleWritable(avg_time));
    }
}
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.FileOutputStream;
import java.io.IOException;

public class Movie8Runner {
    public static  void  main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf=new Configuration();
        //创建job
        Job job= Job.getInstance(conf,"maoyan");
        //设置输入输出路径
        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //设置运行类
        job.setJarByClass(Movie8Runner.class);
        job.setMapperClass(Movie8Mapper.class);
        job.setReducerClass(Movie8Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);
        System.exit(job.waitForCompletion(true)?0:1);

    }
}
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

matplotlib.rcParams['font.family']='SimHei'   
matplotlib.rcParams['font.sans-serif'] = ['SimHei']

data=pd.read_csv(r"E:\output8\part-r-00000",sep='\t',header=None)
data.columns=['年份','上映电影平均时长']
data.head()

plt.figure(figsize=(9,6))
x=data['年份']
y=data['上映电影平均时长']
plt.plot(x,y,color='r',label='均值')
plt.xlabel('上映时间')
plt.ylabel("平均时长")
# plt.xticks(rotation=30,fontsize=10)
plt.xlim(1980,2017)
plt.legend()
plt.title("上映电影平均时长趋势表")
plt.show()

 

posted @ 2022-08-31 10:03  aq阿桂  阅读(158)  评论(0)    收藏  举报