18
18
package org .apache .rocketmq .streams .client ;
19
19
20
20
import com .alibaba .fastjson .JSONObject ;
21
+ import java .io .File ;
21
22
import java .io .Serializable ;
23
+ import java .util .ArrayList ;
24
+ import java .util .HashMap ;
25
+ import java .util .List ;
26
+ import java .util .Map ;
27
+ import org .apache .commons .io .FileUtils ;
28
+ import org .apache .commons .lang3 .tuple .Pair ;
29
+ import org .apache .rocketmq .streams .client .transform .window .SessionWindow ;
22
30
import org .apache .rocketmq .streams .client .transform .window .Time ;
23
31
import org .apache .rocketmq .streams .client .transform .window .TumblingWindow ;
24
32
import org .apache .rocketmq .streams .common .functions .ForEachFunction ;
25
33
import org .apache .rocketmq .streams .common .functions .MapFunction ;
34
+ import org .apache .rocketmq .streams .common .utils .DateUtil ;
35
+ import org .junit .Assert ;
26
36
import org .junit .Test ;
27
37
28
38
public class WindowTest implements Serializable {
@@ -31,7 +41,7 @@ public class WindowTest implements Serializable {
31
41
public void testWindow () {
32
42
StreamBuilder .dataStream ("namespace" , "name" )
33
43
.fromFile ("/Users/duheng/project/opensource/sls_100.txt" , false )
34
- .map ((MapFunction <JSONObject , String >)message -> JSONObject .parseObject (message ))
44
+ .map ((MapFunction <JSONObject , String >) message -> JSONObject .parseObject (message ))
35
45
.window (TumblingWindow .of (Time .seconds (5 )))
36
46
.groupBy ("ProjectName" , "LogStore" )
37
47
.setLocalStorageOnly (true )
@@ -81,4 +91,196 @@ public void foreach(JSONObject o) {
81
91
// }).toPrint().start();
82
92
// }
83
93
94
+ @ Test
95
+ public void testSession () {
96
+ //dataset
97
+ List <String > behaviorList = new ArrayList <>();
98
+
99
+ JSONObject userA = new JSONObject ();
100
+ userA .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
101
+ userA .put ("user" , "userA" );
102
+ userA .put ("movie" , "movie001" );
103
+ userA .put ("flag" , 1 );
104
+ behaviorList .add (userA .toJSONString ());
105
+
106
+ userA = new JSONObject ();
107
+ userA .put ("time" , DateUtil .parse ("2021-09-09 10:00:01" ));
108
+ userA .put ("user" , "userA" );
109
+ userA .put ("movie" , "movie002" );
110
+ userA .put ("flag" , 1 );
111
+ behaviorList .add (userA .toJSONString ());
112
+
113
+ JSONObject userB = new JSONObject ();
114
+ userB .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
115
+ userB .put ("user" , "userB" );
116
+ userB .put ("movie" , "movie003" );
117
+ userB .put ("flag" , 1 );
118
+ behaviorList .add (userB .toJSONString ());
119
+
120
+ JSONObject userC = new JSONObject ();
121
+ userC .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
122
+ userC .put ("user" , "userC" );
123
+ userC .put ("movie" , "movie004" );
124
+ userC .put ("flag" , 1 );
125
+ behaviorList .add (userC .toJSONString ());
126
+
127
+ userC = new JSONObject ();
128
+ userC .put ("time" , DateUtil .parse ("2021-09-09 10:00:06" ));
129
+ userC .put ("user" , "userC" );
130
+ userC .put ("movie" , "movie005" );
131
+ userC .put ("flag" , 1 );
132
+ behaviorList .add (userC .toJSONString ());
133
+
134
+ File dataFile = null ;
135
+ try {
136
+ dataFile = File .createTempFile ("behavior" , ".txt" );
137
+ FileUtils .writeLines (dataFile , behaviorList );
138
+ } catch (Exception e ) {
139
+ e .printStackTrace ();
140
+ }
141
+
142
+ File resultFile = null ;
143
+ try {
144
+ resultFile = File .createTempFile ("behavior.txt" , ".session" );
145
+ } catch (Exception e ) {
146
+ e .printStackTrace ();
147
+ }
148
+
149
+ StreamBuilder .dataStream ("namespace" , "session_test" )
150
+ .fromFile (dataFile .getAbsolutePath (), false )
151
+ .map ((MapFunction <JSONObject , String >) message -> JSONObject .parseObject (message ))
152
+ .window (SessionWindow .of (Time .seconds (5 ), "time" ))
153
+ .groupBy ("user" )
154
+ .setLocalStorageOnly (true )
155
+ .sum ("flag" , "count" )
156
+ .toDataSteam ()
157
+ .toFile (resultFile .getAbsolutePath ()).start (true );
158
+
159
+ try {
160
+ Thread .sleep (1 * 60 * 1000 );
161
+ List <String > sessionList = FileUtils .readLines (resultFile , "UTF-8" );
162
+ Map <String , List <Pair <Pair <String , String >, Integer >>> sessionMap = new HashMap <>(4 );
163
+ for (String line : sessionList ) {
164
+ JSONObject object = JSONObject .parseObject (line );
165
+ String user = object .getString ("user" );
166
+ String startTime = object .getString ("start_time" );
167
+ String endTime = object .getString ("end_time" );
168
+ Integer value = object .getIntValue ("count" );
169
+ if (!sessionMap .containsKey (user )) {
170
+ sessionMap .put (user , new ArrayList <>());
171
+ }
172
+ sessionMap .get (user ).add (Pair .of (Pair .of (startTime , endTime ), value ));
173
+ }
174
+ Assert .assertEquals (3 , sessionMap .size ());
175
+ Assert .assertEquals (1 , sessionMap .get ("userA" ).size ());
176
+ Assert .assertEquals ("2021-09-09 10:00:06" , sessionMap .get ("userA" ).get (0 ).getLeft ().getRight ());
177
+ Assert .assertEquals (2 , sessionMap .get ("userC" ).size ());
178
+ Assert .assertEquals ("2021-09-09 10:00:05" , sessionMap .get ("userC" ).get (0 ).getLeft ().getRight ());
179
+ Assert .assertEquals ("2021-09-09 10:00:06" , sessionMap .get ("userC" ).get (1 ).getLeft ().getLeft ());
180
+ Assert .assertEquals (1 , sessionMap .get ("userB" ).size ());
181
+
182
+ } catch (Exception e ) {
183
+ e .printStackTrace ();
184
+ } finally {
185
+ dataFile .deleteOnExit ();
186
+ resultFile .deleteOnExit ();
187
+ }
188
+
189
+ }
190
+
191
+ @ Test
192
+ public void testCountDistinct () {
193
+ //dataset
194
+ List <String > behaviorList = new ArrayList <>();
195
+
196
+ JSONObject userA = new JSONObject ();
197
+ userA .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
198
+ userA .put ("user" , "userA" );
199
+ userA .put ("page" , "alibaba-inc.com" );
200
+ behaviorList .add (userA .toJSONString ());
201
+
202
+ userA = new JSONObject ();
203
+ userA .put ("time" , DateUtil .parse ("2021-09-09 10:01:00" ));
204
+ userA .put ("user" , "userA" );
205
+ userA .put ("page" , "sina.com" );
206
+ behaviorList .add (userA .toJSONString ());
207
+
208
+ userA = new JSONObject ();
209
+ userA .put ("time" , DateUtil .parse ("2021-09-09 10:03:00" ));
210
+ userA .put ("user" , "userA" );
211
+ userA .put ("page" , "alibaba-inc.com" );
212
+ behaviorList .add (userA .toJSONString ());
213
+
214
+ JSONObject userB = new JSONObject ();
215
+ userB .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
216
+ userB .put ("user" , "userB" );
217
+ userB .put ("page" , "sohu.com" );
218
+ behaviorList .add (userB .toJSONString ());
219
+
220
+ JSONObject userC = new JSONObject ();
221
+ userC .put ("time" , DateUtil .parse ("2021-09-09 10:00:00" ));
222
+ userC .put ("user" , "userC" );
223
+ userC .put ("page" , "qq.com" );
224
+ behaviorList .add (userC .toJSONString ());
225
+
226
+ userC = new JSONObject ();
227
+ userC .put ("time" , DateUtil .parse ("2021-09-09 10:03:06" ));
228
+ userC .put ("user" , "userC" );
229
+ userC .put ("page" , "qq.com" );
230
+ behaviorList .add (userC .toJSONString ());
231
+
232
+ File dataFile = null ;
233
+ try {
234
+ dataFile = File .createTempFile ("behavior" , ".txt" );
235
+ FileUtils .writeLines (dataFile , behaviorList );
236
+ } catch (Exception e ) {
237
+ e .printStackTrace ();
238
+ }
239
+
240
+ File resultFile = null ;
241
+ try {
242
+ resultFile = File .createTempFile ("behavior.txt" , ".session" );
243
+ } catch (Exception e ) {
244
+ e .printStackTrace ();
245
+ }
246
+
247
+ StreamBuilder .dataStream ("namespace" , "count_distinct_test" )
248
+ .fromFile (dataFile .getAbsolutePath (), false )
249
+ .map ((MapFunction <JSONObject , String >) message -> JSONObject .parseObject (message ))
250
+ .window (TumblingWindow .of (Time .minutes (5 ), "time" ))
251
+ .groupBy ("user" )
252
+ .setLocalStorageOnly (true )
253
+ .count_distinct ("page" , "uv" )
254
+ .count_distinct_large ("page" , "uv_large" )
255
+ .count_distinct_2 ("page" ,"uv_2" )
256
+ .toDataSteam ()
257
+ .toFile (resultFile .getAbsolutePath ()).start (true );
258
+
259
+ try {
260
+ Thread .sleep (6 * 60 * 1000 );
261
+ List <String > sessionList = FileUtils .readLines (resultFile , "UTF-8" );
262
+ Map <String , Integer > statisticMap = new HashMap <>(4 );
263
+ for (String line : sessionList ) {
264
+ JSONObject object = JSONObject .parseObject (line );
265
+ String user = object .getString ("user" );
266
+ Integer userVisitCount = object .getInteger ("uv" );
267
+ Integer userVisitCountBasedRocksDB = object .getInteger ("uv_2" );
268
+ Integer userVisitCountLarge = object .getInteger ("uv_large" );
269
+ Assert .assertEquals (userVisitCount , userVisitCountLarge );
270
+ Assert .assertEquals (userVisitCount , userVisitCountBasedRocksDB );
271
+ statisticMap .put (user , userVisitCount );
272
+ }
273
+ Assert .assertEquals (3 , statisticMap .size ());
274
+ Assert .assertEquals (2 , statisticMap .get ("userA" ).longValue ());
275
+ Assert .assertEquals (1 , statisticMap .get ("userB" ).longValue ());
276
+ Assert .assertEquals (1 , statisticMap .get ("userC" ).longValue ());
277
+
278
+ } catch (Exception e ) {
279
+ e .printStackTrace ();
280
+ } finally {
281
+ dataFile .deleteOnExit ();
282
+ resultFile .deleteOnExit ();
283
+ }
284
+ }
285
+
84
286
}
0 commit comments