Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mongo-train
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liangyuhang
mongo-train
Commits
861c3990
Commit
861c3990
authored
May 18, 2021
by
liang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完整实现知微事件分析单个事件的指定分析结果与各项数据输出
parent
ab74b2c3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
592 additions
and
27 deletions
+592
-27
mongo-event/src/main/java/com/liang/mongoevent/controller/MediaController.java
+62
-0
mongo-event/src/main/java/com/liang/mongoevent/controller/SingleController.java
+93
-0
mongo-event/src/main/java/com/liang/mongoevent/controller/WeiboController.java
+25
-0
mongo-event/src/main/java/com/liang/mongoevent/pojo/Media.java
+3
-1
mongo-event/src/main/java/com/liang/mongoevent/pojo/StaNews.java
+19
-0
mongo-event/src/main/java/com/liang/mongoevent/pojo/Weibo.java
+2
-1
mongo-event/src/main/java/com/liang/mongoevent/service/MediaService.java
+21
-0
mongo-event/src/main/java/com/liang/mongoevent/service/MediaServiceImpl.java
+183
-0
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboService.java
+10
-0
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboServiceImpl.java
+108
-24
mongo-event/src/main/java/com/liang/mongoevent/vo/AllEventId.java
+15
-0
mongo-event/src/main/java/com/liang/mongoevent/vo/Location.java
+1
-1
mongo-event/src/main/java/com/liang/mongoevent/vo/SourceProp.java
+15
-0
mongo-event/src/main/java/com/liang/mongoevent/vo/TimeCount.java
+1
-0
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend.java
+1
-0
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend2.java
+17
-0
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend3.java
+16
-0
mongo-event/src/test/java/com/liang/mongoevent/MongoEventApplicationTests.java
+0
-0
No files found.
mongo-event/src/main/java/com/liang/mongoevent/controller/MediaController.java
0 → 100644
View file @
861c3990
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
);
}
}
mongo-event/src/main/java/com/liang/mongoevent/controller/SingleController.java
0 → 100644
View file @
861c3990
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
);
}
}
mongo-event/src/main/java/com/liang/mongoevent/controller/WeiboController.java
View file @
861c3990
...
...
@@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.Map
;
@RestController
@RequestMapping
(
"/weibo"
)
public
class
WeiboController
{
...
...
@@ -46,4 +48,27 @@ public class WeiboController {
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
);
}
}
mongo-event/src/main/java/com/liang/mongoevent/pojo/Media.java
View file @
861c3990
...
...
@@ -2,6 +2,7 @@ package com.liang.mongoevent.pojo;
import
cn.afterturn.easypoi.excel.annotation.Excel
;
import
cn.afterturn.easypoi.excel.annotation.ExcelTarget
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
...
...
@@ -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.mapping.Document
;
import
java.time.LocalDateTime
;
import
java.util.Date
;
@Data
...
...
@@ -30,7 +32,7 @@ public class Media {
private
String
title
;
@Excel
(
name
=
"创建时间"
,
width
=
20.0
,
orderNum
=
"3"
,
format
=
"yyyy-MM-dd HH:mm:ss"
)
private
Dat
e
time
;
private
LocalDateTim
e
time
;
@Excel
(
name
=
"来源"
,
width
=
30.0
,
orderNum
=
"4"
)
private
String
source
;
@Excel
(
name
=
"类型"
,
width
=
20.0
,
orderNum
=
"5"
)
...
...
mongo-event/src/main/java/com/liang/mongoevent/pojo/StaNews.java
0 → 100644
View file @
861c3990
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
;
}
mongo-event/src/main/java/com/liang/mongoevent/pojo/Weibo.java
View file @
861c3990
...
...
@@ -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.mapping.Document
;
import
java.time.LocalDateTime
;
import
java.util.Date
;
@Data
@AllArgsConstructor
@NoArgsConstructor
@CompoundIndex
(
def
=
"{'eventId':1,'time':1}"
)
@CompoundIndex
(
def
=
"{'eventId':1,'time':
-
1}"
)
@Document
public
class
Weibo
{
...
...
mongo-event/src/main/java/com/liang/mongoevent/service/MediaService.java
0 → 100644
View file @
861c3990
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
);
}
mongo-event/src/main/java/com/liang/mongoevent/service/MediaServiceImpl.java
0 → 100644
View file @
861c3990
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
;
}
}
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboService.java
View file @
861c3990
...
...
@@ -2,6 +2,8 @@ package com.liang.mongoevent.service;
import
com.liang.mongoevent.utils.RespBean
;
import
java.util.Map
;
public
interface
WeiboService
{
RespBean
countSex
();
...
...
@@ -13,4 +15,12 @@ public interface WeiboService {
RespBean
topTenFensiNum
();
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
);
}
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboServiceImpl.java
View file @
861c3990
...
...
@@ -2,9 +2,7 @@ package com.liang.mongoevent.service;
import
com.liang.mongoevent.pojo.Weibo
;
import
com.liang.mongoevent.utils.RespBean
;
import
com.liang.mongoevent.vo.Location
;
import
com.liang.mongoevent.vo.TimeCount
;
import
com.liang.mongoevent.vo.Trend
;
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
;
...
...
@@ -81,34 +79,120 @@ public class WeiboServiceImpl implements WeiboService {
@Override
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
.
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
)
Aggregation
.
match
(
Criteria
.
where
(
"eventId"
).
is
(
eventId
)),
Aggregation
.
group
(
"time"
).
first
(
"time"
).
as
(
"time"
),
Aggregation
.
sort
(
Sort
.
Direction
.
DESC
,
"time"
)
);
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
)));
long
count
=
mongoTemplate
.
count
(
query
,
Weibo
.
class
);
map1
.
put
(
df
.
format
(
stime
),
count
);
stime
=
stime
.
plusHours
(
1
);
return
RespBean
.
success
(
"成功"
,
map
);
}
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
;
}
...
...
mongo-event/src/main/java/com/liang/mongoevent/vo/AllEventId.java
0 → 100644
View file @
861c3990
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
;
}
mongo-event/src/main/java/com/liang/mongoevent/vo/Location.java
View file @
861c3990
...
...
@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public
class
Location
{
private
String
id
;
private
String
location
;
private
Long
count
;
}
mongo-event/src/main/java/com/liang/mongoevent/vo/SourceProp.java
0 → 100644
View file @
861c3990
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
;
}
mongo-event/src/main/java/com/liang/mongoevent/vo/TimeCount.java
View file @
861c3990
...
...
@@ -8,6 +8,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public
class
TimeCount
{
private
String
id
;
private
String
time
;
private
long
count
;
}
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend.java
View file @
861c3990
...
...
@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public
class
Trend
{
private
String
id
;
private
String
eventId
;
private
String
stime
;
private
String
etime
;
...
...
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend2.java
0 → 100644
View file @
861c3990
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
;
}
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend3.java
0 → 100644
View file @
861c3990
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
;
}
mongo-event/src/test/java/com/liang/mongoevent/MongoEventApplicationTests.java
View file @
861c3990
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment