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
ab74b2c3
Commit
ab74b2c3
authored
May 14, 2021
by
liang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
性别统计、地域统计、每小时数据趋势、用户发博数统计、参与用户粉丝数前10降序展示
parent
9d9e2c3b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
11 deletions
+79
-11
mongo-event/src/main/java/com/liang/mongoevent/controller/WeiboController.java
+7
-0
mongo-event/src/main/java/com/liang/mongoevent/pojo/Weibo.java
+8
-0
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboService.java
+2
-0
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboServiceImpl.java
+44
-8
mongo-event/src/main/java/com/liang/mongoevent/vo/TimeCount.java
+3
-3
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend.java
+15
-0
No files found.
mongo-event/src/main/java/com/liang/mongoevent/controller/WeiboController.java
View file @
ab74b2c3
...
@@ -7,6 +7,7 @@ import io.swagger.annotations.ApiOperation;
...
@@ -7,6 +7,7 @@ import io.swagger.annotations.ApiOperation;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RestController
...
@@ -39,4 +40,10 @@ public class WeiboController {
...
@@ -39,4 +40,10 @@ public class WeiboController {
return
weiboService
.
topTenFensiNum
();
return
weiboService
.
topTenFensiNum
();
}
}
@ApiOperation
(
"每小时数据趋势"
)
@GetMapping
(
"trendPerHour"
)
public
RespBean
trendPerHour
(
@RequestParam
(
defaultValue
=
"1"
)
Integer
page
,
@RequestParam
(
defaultValue
=
"20"
)
Integer
size
){
return
weiboService
.
trendPerHour
(
page
,
size
);
}
}
}
mongo-event/src/main/java/com/liang/mongoevent/pojo/Weibo.java
View file @
ab74b2c3
...
@@ -3,28 +3,36 @@ package com.liang.mongoevent.pojo;
...
@@ -3,28 +3,36 @@ package com.liang.mongoevent.pojo;
import
lombok.AllArgsConstructor
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
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.util.Date
;
import
java.util.Date
;
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
@CompoundIndex
(
def
=
"{'eventId':1,'time':1}"
)
@Document
public
class
Weibo
{
public
class
Weibo
{
private
String
id
;
private
String
id
;
private
String
username
;
private
String
username
;
private
String
text
;
private
String
text
;
private
Long
vtype
;
private
Long
vtype
;
@Indexed
private
String
time
;
private
String
time
;
private
String
sex
;
private
String
sex
;
private
String
location
;
private
String
location
;
private
String
source
;
private
String
source
;
private
Long
isForward
;
private
Long
isForward
;
@Indexed
private
String
user_id
;
private
String
user_id
;
private
String
mid
;
private
String
mid
;
private
Long
retweet_count
;
private
Long
retweet_count
;
private
Long
reply_count
;
private
Long
reply_count
;
private
String
img_url
;
private
String
img_url
;
@Indexed
private
String
eventId
;
private
String
eventId
;
private
String
url
;
private
String
url
;
private
Long
rstime
;
private
Long
rstime
;
...
...
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboService.java
View file @
ab74b2c3
...
@@ -11,4 +11,6 @@ public interface WeiboService {
...
@@ -11,4 +11,6 @@ public interface WeiboService {
RespBean
countWeibo
();
RespBean
countWeibo
();
RespBean
topTenFensiNum
();
RespBean
topTenFensiNum
();
RespBean
trendPerHour
(
Integer
page
,
Integer
size
);
}
}
mongo-event/src/main/java/com/liang/mongoevent/service/WeiboServiceImpl.java
View file @
ab74b2c3
...
@@ -3,7 +3,8 @@ package com.liang.mongoevent.service;
...
@@ -3,7 +3,8 @@ 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.Location
;
import
com.liang.mongoevent.vo.UserCount
;
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
;
...
@@ -13,10 +14,10 @@ import org.springframework.data.mongodb.core.query.Criteria;
...
@@ -13,10 +14,10 @@ import org.springframework.data.mongodb.core.query.Criteria;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.
util.HashMap
;
import
java.
time.Duration
;
import
java.
util.LinkedHashMap
;
import
java.
time.LocalDateTime
;
import
java.
util.List
;
import
java.
time.format.DateTimeFormatter
;
import
java.util.
Map
;
import
java.util.
*
;
@Service
@Service
public
class
WeiboServiceImpl
implements
WeiboService
{
public
class
WeiboServiceImpl
implements
WeiboService
{
...
@@ -39,7 +40,8 @@ public class WeiboServiceImpl implements WeiboService {
...
@@ -39,7 +40,8 @@ public class WeiboServiceImpl implements WeiboService {
public
RespBean
countLocation
()
{
public
RespBean
countLocation
()
{
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
.
group
(
"location"
).
first
(
"location"
).
as
(
"location"
).
count
().
as
(
"count"
),
Aggregation
.
group
(
"location"
).
first
(
"location"
).
as
(
"location"
).
count
().
as
(
"count"
),
Aggregation
.
sort
(
Sort
.
Direction
.
DESC
,
"count"
));
Aggregation
.
sort
(
Sort
.
Direction
.
DESC
,
"count"
)
);
AggregationResults
<
Location
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Location
.
class
);
AggregationResults
<
Location
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Location
.
class
);
Map
<
String
,
Long
>
locationMap
=
new
LinkedHashMap
<>();
Map
<
String
,
Long
>
locationMap
=
new
LinkedHashMap
<>();
for
(
Location
location
:
aggregate
)
{
for
(
Location
location
:
aggregate
)
{
...
@@ -52,7 +54,8 @@ public class WeiboServiceImpl implements WeiboService {
...
@@ -52,7 +54,8 @@ public class WeiboServiceImpl implements WeiboService {
public
RespBean
countWeibo
()
{
public
RespBean
countWeibo
()
{
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
.
group
(
"user_id"
).
first
(
"user_id"
).
as
(
"user_id"
).
first
(
"username"
).
as
(
"username"
).
first
(
"weibo"
).
as
(
"weibo"
),
Aggregation
.
group
(
"user_id"
).
first
(
"user_id"
).
as
(
"user_id"
).
first
(
"username"
).
as
(
"username"
).
first
(
"weibo"
).
as
(
"weibo"
),
Aggregation
.
sort
(
Sort
.
Direction
.
DESC
,
"weibo"
));
Aggregation
.
sort
(
Sort
.
Direction
.
DESC
,
"weibo"
)
);
AggregationResults
<
Weibo
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Weibo
.
class
);
AggregationResults
<
Weibo
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Weibo
.
class
);
Map
<
String
,
Long
>
weiboMap
=
new
LinkedHashMap
<>();
Map
<
String
,
Long
>
weiboMap
=
new
LinkedHashMap
<>();
for
(
Weibo
weibo
:
aggregate
)
{
for
(
Weibo
weibo
:
aggregate
)
{
...
@@ -66,7 +69,8 @@ public class WeiboServiceImpl implements WeiboService {
...
@@ -66,7 +69,8 @@ public class WeiboServiceImpl implements WeiboService {
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
agg
=
Aggregation
.
newAggregation
(
Aggregation
.
group
(
"user_id"
).
first
(
"user_id"
).
as
(
"user_id"
).
first
(
"username"
).
as
(
"username"
).
first
(
"fensi"
).
as
(
"fensi"
),
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
.
sort
(
Sort
.
Direction
.
DESC
,
"fensi"
),
Aggregation
.
limit
(
10
));
Aggregation
.
limit
(
10
)
);
AggregationResults
<
Weibo
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Weibo
.
class
);
AggregationResults
<
Weibo
>
aggregate
=
mongoTemplate
.
aggregate
(
agg
,
Weibo
.
class
,
Weibo
.
class
);
Map
<
String
,
Long
>
fensiMap
=
new
LinkedHashMap
<>();
Map
<
String
,
Long
>
fensiMap
=
new
LinkedHashMap
<>();
for
(
Weibo
weibo
:
aggregate
)
{
for
(
Weibo
weibo
:
aggregate
)
{
...
@@ -75,5 +79,37 @@ public class WeiboServiceImpl implements WeiboService {
...
@@ -75,5 +79,37 @@ public class WeiboServiceImpl implements WeiboService {
return
RespBean
.
success
(
"成功"
,
fensiMap
);
return
RespBean
.
success
(
"成功"
,
fensiMap
);
}
}
@Override
public
RespBean
trendPerHour
(
Integer
page
,
Integer
size
)
{
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
)
);
ArrayList
<
Object
>
list
=
new
ArrayList
<>();
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
);
}
return
RespBean
.
success
(
"成功"
,
map
);
}
}
}
mongo-event/src/main/java/com/liang/mongoevent/vo/
User
Count.java
→
mongo-event/src/main/java/com/liang/mongoevent/vo/
Time
Count.java
View file @
ab74b2c3
...
@@ -7,7 +7,7 @@ import lombok.NoArgsConstructor;
...
@@ -7,7 +7,7 @@ import lombok.NoArgsConstructor;
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
public
class
User
Count
{
public
class
Time
Count
{
private
String
user_id
;
private
String
time
;
private
L
ong
count
;
private
l
ong
count
;
}
}
mongo-event/src/main/java/com/liang/mongoevent/vo/Trend.java
0 → 100644
View file @
ab74b2c3
package
com
.
liang
.
mongoevent
.
vo
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
Trend
{
private
String
eventId
;
private
String
stime
;
private
String
etime
;
}
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