Duan代码
This commit is contained in:
parent
ff09c571c0
commit
ba1bb76c8d
|
@ -0,0 +1,30 @@
|
|||
package org.jeecg.duan.java.controller;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.json.JSONArray;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import org.jeecg.duan.java.service.MusicService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Api(tags = "流行音乐分析平台")
|
||||
@RestController
|
||||
@RequestMapping("/cool/music")
|
||||
@Slf4j
|
||||
public class DashboardController {
|
||||
@Autowired
|
||||
MusicService musicService;
|
||||
|
||||
@GetMapping("/getRanking")
|
||||
public Result<JSONArray> getRanking() throws InterruptedException {
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
return musicService.getRanking();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.jeecg.duan.java.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@TableName("comment_list")
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@ApiModel(value="音乐评论排行榜对象", description="流行音乐分析")
|
||||
public class Ranking {
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**主键*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
@ApiModelProperty(value = "主键")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "歌手")
|
||||
private String singer;
|
||||
|
||||
@ApiModelProperty(value = "评论数")
|
||||
private Integer comment;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.jeecg.duan.java.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.jeecg.duan.java.entity.Ranking;
|
||||
|
||||
public interface MusicMapper extends BaseMapper<Ranking> {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.jeecg.modules.cool.music.mapper.MusicMapper">
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,11 @@
|
|||
package org.jeecg.duan.java.service;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
|
||||
|
||||
public interface MusicService {
|
||||
|
||||
public Result<JSONArray> getRanking();
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package org.jeecg.duan.java.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
||||
import org.jeecg.duan.java.entity.Ranking;
|
||||
import org.jeecg.duan.java.mapper.MusicMapper;
|
||||
import org.jeecg.duan.java.service.MusicService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Ranking> implements MusicService {
|
||||
|
||||
@Autowired
|
||||
private MusicMapper musicMapper;
|
||||
|
||||
@Override
|
||||
public Result<JSONArray> getRanking() {
|
||||
ComparatorList comparatorList = new ComparatorList();
|
||||
List<Ranking> rankings = musicMapper.selectList(null);
|
||||
JSONArray result = new JSONArray();
|
||||
System.out.println(rankings);
|
||||
Collections.sort(rankings, comparatorList);
|
||||
for (Ranking ranking : rankings) {
|
||||
System.out.println("\n\n\n\n\n" + ranking);
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("comment", ranking.getComment());
|
||||
object.put("singer", ranking.getSinger());
|
||||
object.put("songName", ranking.getName());
|
||||
result.add(object);
|
||||
}
|
||||
UpdateWrapper<Ranking> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.setSql("comment=comment- FLOOR( RAND()*100 )");
|
||||
musicMapper.update(null,updateWrapper);
|
||||
|
||||
return Result.OK(result);
|
||||
}
|
||||
}
|
||||
|
||||
class ComparatorList implements Comparator<Ranking> {
|
||||
|
||||
@Override
|
||||
public int compare(Ranking o1, Ranking o2) {
|
||||
return o1.getComment() > o2.getComment() ? 1 : -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package org.jeecg.duan.scala
|
||||
|
||||
import com.alibaba.fastjson.{JSONArray, JSONObject}
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
|
||||
import org.jeecg.duan.java.entity.Ranking
|
||||
import org.jeecg.duan.java.mapper.MusicMapper
|
||||
import org.jeecg.modules.drag.config.common.Result
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
|
||||
import java.util.{Collections, Comparator}
|
||||
|
||||
/**
|
||||
* 这个对象提供了排行榜分析的功能。
|
||||
*/
|
||||
object Analysis {
|
||||
|
||||
@Autowired
|
||||
private val musicMapper: MusicMapper = null
|
||||
|
||||
/**
|
||||
* 检索排行榜并根据评论进行排序。
|
||||
*
|
||||
* @return Result 包含根据评论排序的排行榜的 JSON 数组。
|
||||
*/
|
||||
def getRanking: Result[JSONArray] = {
|
||||
val comparatorList: ComparatorList = new ComparatorList
|
||||
val rankings: List[Ranking] = null // 从数据库或其他来源获取排行榜
|
||||
val result: JSONArray = new JSONArray
|
||||
System.out.println(rankings) // 用于调试的日志排行榜
|
||||
Collections sort(rankings, comparatorList) // 对排行榜进行排序
|
||||
for (ranking <- rankings) {
|
||||
System.out.println("\n\n\n\n\n" + ranking) // 打印每个排名信息,用于调试
|
||||
val `object`: JSONObject = new JSONObject
|
||||
`object`.put("comment", ranking.getComment) // 将评论信息添加到 JSON 对象中
|
||||
`object`.put("singer", ranking.getSinger) // 将歌手信息添加到 JSON 对象中
|
||||
`object`.put("songName", ranking.getName) // 将歌曲名称添加到 JSON 对象中
|
||||
result.add(`object`) // 将 JSON 对象添加到结果数组中
|
||||
}
|
||||
val updateWrapper: UpdateWrapper[Ranking] = new UpdateWrapper[Ranking]
|
||||
updateWrapper.setSql("comment=comment- FLOOR( RAND()*100 )") // 随机减少评论数量
|
||||
musicMapper.update(null, updateWrapper) // 更新数据库中的排行榜评论数量
|
||||
return Result.OK(result) // 返回带有排序后排行榜信息的成功结果
|
||||
|
||||
}
|
||||
|
||||
|
||||
private class ComparatorList extends Comparator[Ranking] {
|
||||
override def compare(o1: Ranking, o2: Ranking): Int = {
|
||||
if (o1.getComment > o2.getComment) {
|
||||
1
|
||||
}
|
||||
else {
|
||||
-(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.jeecg.duan
|
||||
|
||||
import org.apache.log4j.{Level, Logger}
|
||||
import org.apache.spark.{SparkConf, SparkContext}
|
||||
|
||||
object App extends {
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
Logger.getLogger("org").setLevel(Level.ERROR)
|
||||
val conf = new SparkConf().setAppName("wordcount")
|
||||
val sc = new SparkContext(conf)
|
||||
|
||||
//数据文件从HDFS上读取
|
||||
//val rdd = sc.textFile("hdfs://vm1:8020/wordcount")
|
||||
|
||||
//模拟数据
|
||||
val data: List[String] = List("abc abc bcd bcd", "111 111 111 111")
|
||||
sc.parallelize(data)
|
||||
.flatMap(_.split(" "))
|
||||
.map((_, 1))
|
||||
.reduceByKey(_ + _)
|
||||
.sortBy(x => x._2, false)
|
||||
.map(x => x._1 + "\t" + x._2)
|
||||
.saveAsTextFile("hdfs://vm1:8020/wordcount")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue