Commit 861c3990 by liang

完整实现知微事件分析单个事件的指定分析结果与各项数据输出

parent ab74b2c3
package com.liang.mongoevent.controller;
import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.service.MediaService;
import com.liang.mongoevent.utils.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/media")
public class MediaController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MediaService mediaService;
@ApiOperation("数据相同标题聚合并记录渠道及单渠道发布此标题报道数目")
@GetMapping("countTitleAndSingle")
public RespBean countTitleAndSingle(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size){
return mediaService.countTitleAndSingle(page,size);
}
@ApiOperation("百度新闻每小时数据趋势")
@GetMapping("trendPerHour")
public RespBean trendPerHour(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size){
return mediaService.trendPerHour(page,size);
}
@ApiOperation("各级媒体占比计算")
@GetMapping("proportion")
public RespBean proportion(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size){
return mediaService.proportion(page,size);
}
@ApiOperation("事件微信数据每小时趋势统计")
@GetMapping("wechatTrendPerHour")
public RespBean wechatTrendPerHour(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size){
return mediaService.wechatTrendPerHour(page,size);
}
@ApiOperation("总数据趋势")
@GetMapping("totalDataTrend")
public RespBean totalDataTrend(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size){
return mediaService.totalDataTrend(page,size);
}
@ApiOperation("各级媒体占比计算")
@GetMapping("singleProportion")
public Map<String,Long> singleProportion(String eventId){
return mediaService.singleProportion(eventId);
}
}
package com.liang.mongoevent.controller;
import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.pojo.Weibo;
import com.liang.mongoevent.service.MediaService;
import com.liang.mongoevent.service.WeiboService;
import com.liang.mongoevent.utils.RespBean;
import com.liang.mongoevent.vo.Trend2;
import com.liang.mongoevent.vo.Trend3;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/single")
public class SingleController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MediaService mediaService;
@Autowired
private WeiboService weiboService;
/**
*
* @param eventId 40d0e2e2e7ca4c7d10007560
* @return
*/
@ApiOperation("分析单个事件")
@GetMapping("/singleEvent")
public RespBean singleEvent(String eventId){
Map<String, Object> map=new LinkedHashMap<>();
Map<String, Integer> map1=new LinkedHashMap<>();
List<Map> list=new ArrayList<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC,"time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 trend3 : aggregate) {
String time = df.format(trend3.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
AggregationResults<Trend2> aggregate1 = mongoTemplate.aggregate(agg, Weibo.class, Trend2.class);
for (Trend2 trend2 : aggregate1) {
String time = trend2.getTime().substring(0,13);
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
map.put("eventId",eventId);
map.put("性别统计",weiboService.singleCountSex(eventId));
map.put("地域统计",weiboService.singleCountLocation(eventId));
map.put("用户发博数",weiboService.singleCountWeibo(eventId));
map.put("用户粉丝数前十",weiboService.singleTopTenFensiNum(eventId));
map.put("各级媒体占比",mediaService.singleProportion(eventId));
map.put("trend",map1);
list.add(map);
return RespBean.success("成功",list);
}
}
...@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController @RestController
@RequestMapping("/weibo") @RequestMapping("/weibo")
public class WeiboController { public class WeiboController {
...@@ -46,4 +48,27 @@ public class WeiboController { ...@@ -46,4 +48,27 @@ public class WeiboController {
return weiboService.trendPerHour(page,size); return weiboService.trendPerHour(page,size);
} }
@ApiOperation("单个性别统计")
@GetMapping("singleCountSex")
public Map<String,Long> singleCountSex(String eventId){
return weiboService.singleCountSex(eventId);
}
@ApiOperation("单个地域统计")
@GetMapping("singleCountLocation")
public Map<String,Long> singleCountLocation(String eventId){
return weiboService.singleCountLocation(eventId);
}
@ApiOperation("当前用户发博数统计")
@GetMapping("singleCountWeibo")
public Map<String,Long> singleCountWeibo(String eventId){
return weiboService.singleCountWeibo(eventId);
}
@ApiOperation("当前参与用户粉丝数前10")
@GetMapping("singleTopTenFensiNum")
public Map<String,Long> singleTopTenFensiNum(String eventId){
return weiboService.singleTopTenFensiNum(eventId);
}
} }
...@@ -2,6 +2,7 @@ package com.liang.mongoevent.pojo; ...@@ -2,6 +2,7 @@ package com.liang.mongoevent.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -10,6 +11,7 @@ import org.springframework.data.mongodb.core.index.CompoundIndex; ...@@ -10,6 +11,7 @@ import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@Data @Data
...@@ -30,7 +32,7 @@ public class Media { ...@@ -30,7 +32,7 @@ public class Media {
private String title; private String title;
@Excel(name = "创建时间",width = 20.0,orderNum = "3",format = "yyyy-MM-dd HH:mm:ss") @Excel(name = "创建时间",width = 20.0,orderNum = "3",format = "yyyy-MM-dd HH:mm:ss")
private Date time; private LocalDateTime time;
@Excel(name = "来源",width = 30.0,orderNum = "4") @Excel(name = "来源",width = 30.0,orderNum = "4")
private String source; private String source;
@Excel(name = "类型",width = 20.0,orderNum = "5") @Excel(name = "类型",width = 20.0,orderNum = "5")
......
package com.liang.mongoevent.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StaNews {
@Id
private String id;
@Indexed
private String eventId;
private Long wechat;
private Long baidu;
}
...@@ -7,12 +7,13 @@ import org.springframework.data.mongodb.core.index.CompoundIndex; ...@@ -7,12 +7,13 @@ import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@CompoundIndex(def = "{'eventId':1,'time':1}") @CompoundIndex(def = "{'eventId':1,'time':-1}")
@Document @Document
public class Weibo { public class Weibo {
......
package com.liang.mongoevent.service;
import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.utils.RespBean;
import java.util.List;
import java.util.Map;
public interface MediaService {
RespBean countTitleAndSingle(Integer page, Integer size);
RespBean trendPerHour(Integer page, Integer size);
RespBean proportion(Integer page, Integer size);
RespBean wechatTrendPerHour(Integer page, Integer size);
RespBean totalDataTrend(Integer page, Integer size);
Map<String, Long> singleProportion(String eventId);
}
package com.liang.mongoevent.service;
import com.liang.mongoevent.pojo.Media;
import com.liang.mongoevent.pojo.StaNews;
import com.liang.mongoevent.pojo.Weibo;
import com.liang.mongoevent.utils.RespBean;
import com.liang.mongoevent.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Service
public class MediaServiceImpl implements MediaService {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public RespBean countTitleAndSingle(Integer page, Integer size) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.project("title","source").andExclude("_id"),
Aggregation.group("title").first("title").as("title").addToSet("source").as("sourceList").count().as("count"),
Aggregation.limit(20)
).withOptions(AggregationOptions.builder().allowDiskUse(true).build());
List<Map> mappedResults = mongoTemplate.aggregate(agg, Media.class, Map.class).getMappedResults();
return RespBean.success("成功",mappedResults);
}
@Override
public RespBean trendPerHour(Integer page, Integer size) {
List<StaNews> all = mongoTemplate.find(new Query().skip(size*(page-1)).limit(size).with(Sort.by(Sort.Direction.DESC,"time")),StaNews.class);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Map<String, Object> map=new LinkedHashMap<>();
for (StaNews staNews : all) {
Map<String, Integer> map1=new LinkedHashMap<>();
if(staNews.getBaidu()!=0){
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(staNews.getEventId()).and("pt").is("百度新闻")),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC,"time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 trend3 : aggregate) {
String time = df.format(trend3.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
}
map.put(staNews.getEventId(),map1);
}
return RespBean.success("成功",map);
}
@Override
public RespBean proportion(Integer page, Integer size) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("source").first("source").as("source").count().as("count"),
Aggregation.sort(Sort.Direction.DESC,"count"),
Aggregation.skip(size*(page-1)),
Aggregation.limit(size)
);
HashMap<String, Long> map = new LinkedHashMap<>();
AggregationResults<SourceProp> aggregate = mongoTemplate.aggregate(agg, Media.class, SourceProp.class);
for (SourceProp sourceProp : aggregate) {
map.put(sourceProp.getSource(),sourceProp.getCount());
}
return RespBean.success("成功",map);
}
@Override
public RespBean wechatTrendPerHour(Integer page, Integer size) {
List<StaNews> all = mongoTemplate.find(new Query().skip(size*(page-1)).limit(size).with(Sort.by(Sort.Direction.DESC,"time")),StaNews.class);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Map<String, Object> map=new LinkedHashMap<>();
for (StaNews staNews : all) {
Map<String, Integer> map1=new LinkedHashMap<>();
if(staNews.getBaidu()!=0){
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(staNews.getEventId()).and("pt").is("微信")),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC,"time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 trend3 : aggregate) {
String time = df.format(trend3.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
}
map.put(staNews.getEventId(),map1);
}
return RespBean.success("成功",map);
}
@Override
public RespBean totalDataTrend(Integer page, Integer size) {
List<AllEventId> eventIds = mongoTemplate.find(new Query().skip(size * (page - 1)).limit(size)
.with(Sort.by(Sort.Direction.DESC, "time")), AllEventId.class);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Map<String, Object> map=new LinkedHashMap<>();
for (AllEventId allEventId : eventIds) {
Map<String, Integer> map1 = new LinkedHashMap<>();
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(allEventId.getEventId())),
Aggregation.group("time").first("time").as("time"),
Aggregation.sort(Sort.Direction.DESC, "time")
);
AggregationResults<Trend3> aggregate = mongoTemplate.aggregate(agg, Media.class, Trend3.class);
for (Trend3 meida : aggregate) {
String time = df.format(meida.getTime().minusHours(8));
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
AggregationResults<Trend2> aggregate1 = mongoTemplate.aggregate(agg, Weibo.class, Trend2.class);
for (Trend2 weibo : aggregate1) {
String time = weibo.getTime().substring(0,13);
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
map.put(allEventId.getEventId(), map1);
}
return RespBean.success("成功",map);
}
@Override
public Map<String, Long> singleProportion(String eventId) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("source").first("source").as("source").count().as("count"),
Aggregation.sort(Sort.Direction.DESC,"count")
);
HashMap<String, Long> map = new LinkedHashMap<>();
AggregationResults<SourceProp> aggregate = mongoTemplate.aggregate(agg, Media.class, SourceProp.class);
long total=0;
for (SourceProp sourceProp : aggregate) {
map.put(sourceProp.getSource(),sourceProp.getCount());
total+=sourceProp.getCount();
}
map.put("total",total);
return map;
}
}
...@@ -2,6 +2,8 @@ package com.liang.mongoevent.service; ...@@ -2,6 +2,8 @@ package com.liang.mongoevent.service;
import com.liang.mongoevent.utils.RespBean; import com.liang.mongoevent.utils.RespBean;
import java.util.Map;
public interface WeiboService { public interface WeiboService {
RespBean countSex(); RespBean countSex();
...@@ -13,4 +15,12 @@ public interface WeiboService { ...@@ -13,4 +15,12 @@ public interface WeiboService {
RespBean topTenFensiNum(); RespBean topTenFensiNum();
RespBean trendPerHour(Integer page,Integer size); RespBean trendPerHour(Integer page,Integer size);
Map<String,Long> singleCountSex(String eventId);
Map<String,Long> singleCountLocation(String eventId);
Map<String, Long> singleCountWeibo(String eventId);
Map<String, Long> singleTopTenFensiNum(String eventId);
} }
...@@ -2,9 +2,7 @@ package com.liang.mongoevent.service; ...@@ -2,9 +2,7 @@ package com.liang.mongoevent.service;
import com.liang.mongoevent.pojo.Weibo; import com.liang.mongoevent.pojo.Weibo;
import com.liang.mongoevent.utils.RespBean; import com.liang.mongoevent.utils.RespBean;
import com.liang.mongoevent.vo.Location; import com.liang.mongoevent.vo.*;
import com.liang.mongoevent.vo.TimeCount;
import com.liang.mongoevent.vo.Trend;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
...@@ -81,34 +79,120 @@ public class WeiboServiceImpl implements WeiboService { ...@@ -81,34 +79,120 @@ public class WeiboServiceImpl implements WeiboService {
@Override @Override
public RespBean trendPerHour(Integer page,Integer size) { public RespBean trendPerHour(Integer page,Integer size) {
List<String> eventIds = mongoTemplate.findDistinct(new Query().skip(size*(page-1)).limit(size).with(Sort.by(Sort.Direction.DESC,"time")),"eventId", Weibo.class, String.class);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Map<String, Object> map=new LinkedHashMap<>();
for (String eventId : eventIds) {
Aggregation agg = Aggregation.newAggregation( Aggregation agg = Aggregation.newAggregation(
Aggregation.group("eventId").first("eventId").as("eventId").first("time").as("etime").last("time").as("stime"), Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.sort(Sort.Direction.DESC,"stime"), Aggregation.group("time").first("time").as("time"),
Aggregation.skip(size*(page-1)), Aggregation.sort(Sort.Direction.DESC,"time")
Aggregation.limit(size)
); );
ArrayList<Object> list = new ArrayList<>(); Map<String, Integer> map1=new LinkedHashMap<>();
AggregationResults<Trend2> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Trend2.class);
for (Trend2 trend2 : aggregate) {
String time = trend2.getTime().substring(0,13);
if (map1.containsKey(time)) {
int i = map1.get(time) + 1;
map1.put(time, i);
} else {
map1.put(time, 1);
}
}
map.put(eventId,map1);
}
//Aggregation agg = Aggregation.newAggregation(
// Aggregation.group("eventId").first("eventId").as("eventId").first("time").as("etime").last("time").as("stime"),
// Aggregation.sort(Sort.Direction.DESC,"stime"),
// Aggregation.skip(size*(page-1)),
// Aggregation.limit(size)
//);
//
//Map<String, Object> map=new LinkedHashMap<>();
//AggregationResults<Trend> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Trend.class);
//DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
//for (Trend trend : aggregate) {
// LocalDateTime stime = LocalDateTime.parse(trend.getStime().substring(0,13), df);
// LocalDateTime etime = LocalDateTime.parse(trend.getEtime().substring(0,13), df);
// Duration duration = Duration.between(stime,etime);
// long l = duration.toHours();
// Map<String, Object> map1=new LinkedHashMap<>();
//
// for (int i = 0; i <=l; i++) {
// Query query = new Query(Criteria.where("eventId").is(trend.getEventId()).and("time").regex("^" + df.format(stime)));
// long count = mongoTemplate.count(query, Weibo.class);
// map1.put(df.format(stime),count);
// stime=stime.plusHours(1);
// }
// map.put(trend.getEventId(),map1);
//
//}
Map<String, Object> map=new LinkedHashMap<>();
AggregationResults<Trend> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Trend.class);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
for (Trend trend : aggregate) {
LocalDateTime stime = LocalDateTime.parse(trend.getStime().substring(0,13), df);
LocalDateTime etime = LocalDateTime.parse(trend.getEtime().substring(0,13), df);
Duration duration = Duration.between(stime,etime);
long l = duration.toHours();
Map<String, Object> map1=new LinkedHashMap<>();
for (int i = 0; i <=l; i++) {
Query query = new Query(Criteria.where("eventId").is(trend.getEventId()).and("time").regex("^" + df.format(stime))); return RespBean.success("成功",map);
long count = mongoTemplate.count(query, Weibo.class);
map1.put(df.format(stime),count);
stime=stime.plusHours(1);
} }
map.put(trend.getEventId(),map1);
@Override
public Map<String,Long> singleCountSex(String eventId) {
Query query1 = new Query(Criteria.where("eventId").is(eventId).and("sex").is("m"));
Query query2 = new Query(Criteria.where("eventId").is(eventId).and("sex").is("f"));
long male = mongoTemplate.count(query1, Weibo.class);
long female = mongoTemplate.count(query2, Weibo.class);
Map<String,Long> sexMap=new HashMap<>();
sexMap.put("男",male);
sexMap.put("女",female);
return sexMap;
} }
return RespBean.success("成功",map);
@Override
public Map<String,Long> singleCountLocation(String eventId) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("location").first("location").as("location").count().as("count"),
Aggregation.sort(Sort.Direction.DESC,"count")
);
AggregationResults<Location> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Location.class);
Map<String,Long> locationMap=new LinkedHashMap<>();
for (Location location : aggregate) {
locationMap.put(location.getLocation(),location.getCount());
}
return locationMap;
}
@Override
public Map<String, Long> singleCountWeibo(String eventId) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("user_id").first("user_id").as("user_id").first("username").as("username").first("weibo").as("weibo"),
Aggregation.sort(Sort.Direction.DESC,"weibo")
);
AggregationResults<Weibo> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Weibo.class);
Map<String,Long> weiboMap=new LinkedHashMap<>();
for (Weibo weibo : aggregate) {
weiboMap.put(weibo.getUsername(),weibo.getWeibo());
}
return weiboMap;
}
@Override
public Map<String, Long> singleTopTenFensiNum(String eventId) {
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("eventId").is(eventId)),
Aggregation.group("user_id").first("user_id").as("user_id").first("username").as("username").first("fensi").as("fensi"),
Aggregation.sort(Sort.Direction.DESC,"fensi"),
Aggregation.limit(10)
);
AggregationResults<Weibo> aggregate = mongoTemplate.aggregate(agg, Weibo.class, Weibo.class);
Map<String,Long> fensiMap=new LinkedHashMap<>();
for (Weibo weibo : aggregate) {
fensiMap.put(weibo.getUsername(), weibo.getFensi());
}
return fensiMap;
} }
......
package com.liang.mongoevent.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AllEventId {
private String id;
@Indexed
private String eventId;
}
...@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor; ...@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class Location { public class Location {
private String id;
private String location; private String location;
private Long count; private Long count;
} }
package com.liang.mongoevent.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SourceProp {
private String id;
private String source;
private Long count;
private Long total;
}
...@@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; ...@@ -8,6 +8,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class TimeCount { public class TimeCount {
private String id;
private String time; private String time;
private long count; private long count;
} }
...@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; ...@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class Trend { public class Trend {
private String id;
private String eventId; private String eventId;
private String stime; private String stime;
private String etime; private String etime;
......
package com.liang.mongoevent.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Trend2 {
private String id;
private String eventId;
private String time;
}
package com.liang.mongoevent.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Trend3 {
private String id;
private String eventId;
private LocalDateTime time;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment