forked from hazelcast/hazelcast-jet-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowedCoGroup.java
222 lines (194 loc) · 9.03 KB
/
WindowedCoGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.hazelcast.core.IMap;
import com.hazelcast.jet.Jet;
import com.hazelcast.jet.JetInstance;
import com.hazelcast.jet.Job;
import com.hazelcast.jet.config.JetConfig;
import com.hazelcast.jet.datamodel.ItemsByTag;
import com.hazelcast.jet.datamodel.Tag;
import com.hazelcast.jet.impl.util.Util;
import com.hazelcast.jet.pipeline.Pipeline;
import com.hazelcast.jet.pipeline.Sinks;
import com.hazelcast.jet.pipeline.Sources;
import com.hazelcast.jet.pipeline.StageWithKeyAndWindow;
import com.hazelcast.jet.pipeline.StreamStageWithKey;
import com.hazelcast.jet.pipeline.WindowGroupAggregateBuilder;
import datamodel.AddToCart;
import datamodel.PageVisit;
import datamodel.Payment;
import java.util.concurrent.locks.LockSupport;
import static com.hazelcast.jet.Util.mapEventNewValue;
import static com.hazelcast.jet.Util.mapPutEvents;
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
import static com.hazelcast.jet.aggregate.AggregateOperations.toList;
import static com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST;
import static com.hazelcast.jet.pipeline.WindowDefinition.sliding;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@SuppressWarnings("Convert2MethodRef") // https://bugs.openjdk.java.net/browse/JDK-8154236
public class WindowedCoGroup {
private static final String TOPIC = "topic";
private static final String PAGE_VISIT = "pageVisit";
private static final String ADD_TO_CART = "addToCart";
private static final String PAYMENT = "payment";
public static void main(String[] args) throws Exception {
System.setProperty("hazelcast.logging.type", "log4j");
// All IMap partitions must receive updates for the watermark to advance
// correctly. Since we use just a handful of keys in this sample, we set a
// low partition count.
System.setProperty("hazelcast.partition.count", "1");
JetConfig cfg = new JetConfig();
cfg.getHazelcastConfig().getMapEventJournalConfig("*").setEnabled(true);
JetInstance jet = Jet.newJetInstance(cfg);
ProducerTask producer = new ProducerTask(jet);
try {
// uncomment one of these
// Pipeline p = aggregate();
// Pipeline p = groupAndAggregate();
// Pipeline p = coGroupAndAggregate();
Pipeline p = coGroupWithBuilder();
Job job = jet.newJob(p);
Thread.sleep(5000);
producer.stop();
job.cancel();
} finally {
producer.stop();
Jet.shutdownAll();
}
}
private static Pipeline aggregate() {
Pipeline p = Pipeline.create();
p.drawFrom(Sources.<PageVisit, Integer, PageVisit>mapJournal(PAGE_VISIT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pv -> pv.timestamp(), 100)
.window(sliding(10, 1))
.aggregate(counting())
.drainTo(Sinks.logger());
return p;
}
private static Pipeline groupAndAggregate() {
Pipeline p = Pipeline.create();
p.drawFrom(Sources.<PageVisit, Integer, PageVisit>mapJournal(PAGE_VISIT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pv -> pv.timestamp(), 100)
.window(sliding(10, 1))
.groupingKey(pv -> pv.userId())
.aggregate(toList())
.drainTo(Sinks.logger());
return p;
}
private static Pipeline coGroupAndAggregate() {
Pipeline p = Pipeline.create();
StreamStageWithKey<PageVisit, Integer> pageVisits = p
.drawFrom(Sources.<PageVisit, Integer, PageVisit>mapJournal(PAGE_VISIT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pv -> pv.timestamp(), 100)
.groupingKey(pv -> pv.userId());
StreamStageWithKey<Payment, Integer> payments = p
.drawFrom(Sources.<Payment, Integer, Payment>mapJournal(PAYMENT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pm -> pm.timestamp(), 100)
.groupingKey(pm -> pm.userId());
StreamStageWithKey<AddToCart, Integer> addToCarts = p
.drawFrom(Sources.<AddToCart, Integer, AddToCart>mapJournal(ADD_TO_CART,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(atc -> atc.timestamp(), 100)
.groupingKey(atc -> atc.userId());
StageWithKeyAndWindow<PageVisit, Integer> windowStage = pageVisits.window(sliding(10, 1));
windowStage.aggregate3(counting(), addToCarts, counting(), payments, counting())
.drainTo(Sinks.logger());
return p;
}
private static Pipeline coGroupWithBuilder() {
Pipeline p = Pipeline.create();
StreamStageWithKey<PageVisit, Integer> pageVisits = p
.drawFrom(Sources.<PageVisit, Integer, PageVisit>mapJournal(PAGE_VISIT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pv -> pv.timestamp(), 100)
.groupingKey(pv -> pv.userId());
StreamStageWithKey<AddToCart, Integer> addToCarts = p
.drawFrom(Sources.<AddToCart, Integer, AddToCart>mapJournal(ADD_TO_CART,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(atc -> atc.timestamp(), 100)
.groupingKey(atc -> atc.userId());
StreamStageWithKey<Payment, Integer> payments = p
.drawFrom(Sources.<Payment, Integer, Payment>mapJournal(PAYMENT,
mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST))
.withTimestamps(pm -> pm.timestamp(), 100)
.groupingKey(pm -> pm.userId());
StageWithKeyAndWindow<PageVisit, Integer> windowStage = pageVisits.window(sliding(10, 1));
WindowGroupAggregateBuilder<Integer, Long> builder = windowStage.aggregateBuilder(counting());
Tag<Long> pageVisitTag = builder.tag0();
Tag<Long> addToCartTag = builder.add(addToCarts, counting());
Tag<Long> paymentTag = builder.add(payments, counting());
builder.build()
.drainTo(Sinks.logger(r -> {
ItemsByTag items = r.result();
return String.format(
"window(%s..%s): id %d%n" +
"pageVisits %s%n" +
"addToCarts %s%n" +
"payments %s",
Util.toLocalTime(r.start()),
Util.toLocalTime(r.end()),
r.getKey(),
items.get(pageVisitTag),
items.get(addToCartTag),
items.get(paymentTag));
}));
return p;
}
private static class ProducerTask implements Runnable {
private final IMap<Object, PageVisit> pageVisit;
private final IMap<Object, AddToCart> addToCart;
private final IMap<Object, Payment> payment;
private volatile boolean keepGoing = true;
private int loadTime = 1;
private int quantity = 21;
private int amount = 31;
private long now = System.currentTimeMillis();
ProducerTask(JetInstance jet) {
pageVisit = jet.getMap(PAGE_VISIT);
addToCart = jet.getMap(ADD_TO_CART);
payment = jet.getMap(PAYMENT);
new Thread(this, "WindowedCoGroup Producer").start();
}
@Override
public void run() {
LockSupport.parkNanos(MILLISECONDS.toNanos(100));
while (keepGoing) {
produceSampleData();
LockSupport.parkNanos(MILLISECONDS.toNanos(1));
now++;
}
}
public void stop() {
keepGoing = false;
}
private void produceSampleData() {
for (int userId = 11; userId < 13; userId++) {
for (int i = 0; i < 2; i++) {
pageVisit.set(TOPIC, new PageVisit(now, userId, loadTime));
addToCart.set(TOPIC, new AddToCart(now, userId, quantity));
payment.set(TOPIC, new Payment(now, userId, amount));
loadTime++;
quantity++;
amount++;
}
}
}
}
}