电影评分数据分析案例-[Java]
movie.txt
movie:电影id
rate:用户评分
timeStamp:评分时间
uid:用户id
{"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
{"movie":"661","rate":"3","timeStamp":"978302109","uid":"1"}
{"movie":"3114","rate":"4","timeStamp":"978302174","uid":"1"}
{"movie":"608","rate":"4","timeStamp":"978301398","uid":"1"}
{"movie":"1246","rate":"4","timeStamp":"978302091","uid":"1"}
{"movie":"1245","rate":"2","timeStamp":"978299200","uid":"2"}
{"movie":"1246","rate":"5","timeStamp":"978299418","uid":"2"}
{"movie":"3893","rate":"1","timeStamp":"978299535","uid":"2"}
//用到了JSON jar包
需求
1.每个用户评分最高的3条记录
2.每个用户评分的平均值
3.最大方(评分平均值高)的3个用户
4.最热门的3部电影
5.评价最高的3部电影
..............................
/**
* @author Taylor
*/
public class MovieBean {
private String movie;
private int rate;
private long timeStamp;
private int uid;
public MovieBean() {
}
public MovieBean(String movie, int rate, long timeStamp, int uid) {
this.movie = movie;
this.rate = rate;
this.timeStamp = timeStamp;
this.uid = uid;
}
public String getMovie() {
return movie;
}
public void setMovie(String movie) {
this.movie = movie;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Override
public String toString() {
return "MovieBean{" +
"movie='" + movie + '\'' +
", rate='" + rate + '\'' +
", timeStamp='" + timeStamp + '\'' +
", uid='" + uid + '\'' +
'}';
}
}
接着写测试运行类
1.每个用户评分最高的3条记录
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* @author Taylor
*/
public class Test01_Movies {
public static void main(String[] args) throws IOException {
//读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\movie.txt"));
//逐行处理
String line = null;
HashMap<Integer,ArrayList<Integer>> resMap = new HashMap<>();
while ((line =br.readLine()) != null){
//2.1把拿进来的数据转成MovieBean对象
try{
MovieBean mb = JSON.parseObject(line,MovieBean.class);
int uid = mb.getUid();
int rate = mb.getRate();
if (resMap.containsKey(uid)){
ArrayList<Integer> list = resMap.get(uid);
list.add(rate);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(rate);
resMap.put(uid, list);
}
}catch(Exception ignore){
}
}
//排序
for (Map.Entry<Integer, ArrayList<Integer>> entry : resMap.entrySet()) {
Integer uid = entry.getKey();
ArrayList<Integer> list = entry.getValue();
Collections.sort(list,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
System.out.print(uid +"\t");
for (int i = 0; i <Math.min(3,list.size()); i++) {
Integer rate = list.get(i);
System.out.print(rate +"\t");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------------
2.每个用户评分的平均值
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author Taylor
*/
//每个用户评分的平均值
@SuppressWarnings("all")
public class Test2_Movies {
public static void main(String[] args) throws IOException {
//读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\movie.txt"));
//逐行处理
String line = null;
HashMap<Integer,ArrayList<Integer>> resMap = new HashMap<>();
while ((line =br.readLine()) != null){
//2.1把拿进来的数据转成MovieBean对象
try{
MovieBean mb = JSON.parseObject(line,MovieBean.class);
int uid = mb.getUid();
int rate = mb.getRate();
if (resMap.containsKey(uid)){
ArrayList<Integer> list = resMap.get(uid);
list.add(rate);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(rate);
resMap.put(uid, list);
}
}catch(Exception ignore){
}
}
//遍历map 拿出每一个人的uid 和对应的评分集合
for (Map.Entry<Integer,ArrayList<Integer>> entry : resMap.entrySet()){
Integer uid = entry.getKey();
ArrayList<Integer> rates = entry.getValue();
//遍历集合求平均值
int sum=0;
for (Integer rate : rates) {
sum += rate;
}
//求平均值(不保留两位小数)
double avgRate = sum / (rates.size() * 1.0);
//System.out.println(uid+"评论的平均分为 "+avgRate);
//保留两位小数
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(uid+"评论的平均分为 "+df.format(avgRate));
}
}
}
----------------------------------------------------------------------------------------------------------------------------
3.评分平均值最高的3个用户
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* @author Taylor
*/
@SuppressWarnings("all")
public class Test03_Movies {
public static void main(String[] args) throws IOException {
//读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\movie.txt"));
//逐行处理
String line = null;
HashMap<Integer,ArrayList<Integer>> resMap = new HashMap<>();
while ((line =br.readLine()) != null){
//2.1把拿进来的数据转成MovieBean对象
try{
MovieBean mb = JSON.parseObject(line,MovieBean.class);
int uid = mb.getUid();
int rate = mb.getRate();
if (resMap.containsKey(uid)){
ArrayList<Integer> list = resMap.get(uid);
list.add(rate);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(rate);
resMap.put(uid, list);
}
}catch(Exception ignore){
}
}
//用一个Map收集用户id和平均分
HashMap<Integer, Double> result = new HashMap<>();
//遍历map 拿出每一个人的uid和对应的评分集合
for (Map.Entry<Integer, ArrayList<Integer>> entry : resMap.entrySet()) {
Integer uid = entry.getKey();
ArrayList<Integer> rates = entry.getValue();
//遍历集合求平均值
int sum = 0;
for (Integer rate : rates) {
sum += rate;
}
//求平均值
double avgRate = sum / (rates.size() * 1.0);
result.put(uid, avgRate);
}
//对平均分排序
ArrayList<Map.Entry<Integer, Double>> list = new ArrayList<>(result.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() {
@Override
public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) {
return Double.compare(o2.getValue(), o1.getValue());
}
});
for (int i = 0; i < Math.min(3, list.size()); i++) {
System.out.println(list.get(i));
}
}
}
------------------------------------------------------------------------------------
4.最热门的3部电影
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
/**
* @author Taylor
*/
public class Test04_Movies {
public static void main(String[] args) throws Exception {
//读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\movie.txt"));
//逐行处理
HashMap<String,Integer> resMap=new HashMap<>();
String line = null;
while ((line = br.readLine()) != null){
try {
MovieBean mb = JSON.parseObject(line, MovieBean.class);
String movieId = mb.getMovie();
if (resMap.containsKey(movieId)){
Integer cnts = resMap.get(movieId);
cnts++;
resMap.put(movieId, cnts);
}else{
resMap.put(movieId,1);
}
} catch (Exception e) {
}
}
//排序
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(resMap.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String, Integer>>(){
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
for (int i = 0; i < Math.min(3, list.size()); i++) {
System.out.println(list.get(i));
}
}
}
-------------------------------------------------------------------------------------
5.评价最高的3部电影
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
/**
* @author Taylor
*/
public class Test05_Movies {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("D:\\movie.txt"));
String line = null;
HashMap<String,ArrayList<Integer>> resMap = new HashMap<String,ArrayList<Integer>>();
while ((line = br.readLine()) != null){
try {
MovieBean mb = JSON.parseObject(line,MovieBean.class);
String movieId = mb.getMovie();
int rate = mb.getRate();
if (resMap.containsKey(movieId)){
ArrayList<Integer> list = resMap.get(movieId);
list.add(rate);
resMap.remove(movieId,list);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(rate);
resMap.put(movieId,list);
}
} catch (Exception ignore) {
}
}
HashMap<String,Double> result = new HashMap();
//遍历map 求出每一部电影的平均值
for (Map.Entry<String, ArrayList<Integer>> entry : resMap.entrySet()) {
String movieId = entry.getKey();
ArrayList<Integer> list = entry.getValue();
int sum = 0;
for (Integer integer : list) {
sum+=integer;
}
double avgRate = sum / (list.size() * 1.0);
result.put(movieId, avgRate);
}
//排序
ArrayList<Map.Entry<String, Double>> resultList = new ArrayList<>(result.entrySet());
Collections.sort(resultList, new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
return Double.compare(o2.getValue(),o1.getValue());
}
});
for (int i = 0; i < Math.min(3, result.size()); i++) {
System.out.println(resultList.get(i));
}
}
}
上一篇: PostgreSQL JSON数据类型
下一篇: mysql的json数据类型处理基础入门
推荐阅读