forked from hazelcast/hazelcast-jet-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapEnrichment.java
115 lines (103 loc) · 4.72 KB
/
HashMapEnrichment.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
/*
* 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.jet.Jet;
import com.hazelcast.jet.JetInstance;
import com.hazelcast.jet.aggregate.AggregateOperations;
import com.hazelcast.jet.core.DAG;
import com.hazelcast.jet.core.Vertex;
import com.hazelcast.jet.core.processor.Processors;
import trades.GenerateTradesP;
import trades.TickerInfo;
import trades.Trade;
import java.util.Arrays;
import java.util.Map.Entry;
import static com.hazelcast.jet.core.Edge.between;
import static com.hazelcast.jet.core.Edge.from;
import static com.hazelcast.jet.core.processor.DiagnosticProcessors.writeLoggerP;
import static com.hazelcast.jet.core.processor.SourceProcessors.readMapP;
import static com.hazelcast.jet.function.Functions.entryValue;
/**
* This sample shows, how to enrich batch or stream of items with additional
* information by matching them by key. This version first builds a HashMap
* that is distributed to {@link HashJoinP}.
* <p>
* The {@link HashJoinP} expects enrichment table on input ordinal 0 and items
* to enrich on all other ordinals. The edge at ordinal 0 must have {@link
* com.hazelcast.jet.core.Edge#priority(int) priority} set to -1 to ensure, that
* items on this edge are processed before items to enrich.
* <p>
* The {@code readTickerInfoMap} reads the items in distributed way. In order
* to have full copy on each node we need to broadcast and distribute to a
* processor with local parallelism of 1. From there, we broadcast the same
* {@code Map} instance to all local processors. Sending locally does not copy
* the object, thus this will not increase memory usage.
* <p>
* The DAG is as follows:
* <pre>{@code
* +--------------------+
* | Read tickerInfoMap |
* +---------+----------+
* |
* +-----------------+ | TickerInfo
* | Trades source | | (broadcast, distributed edge)
* +--------+--------+ +--------+--------+
* | | Squash to map |
* | Trade +---------------+-+ (localParallelism = 1)
* | (local edge) |
* +--------+--------+ |
* | Joiner +--------------------------+
* +--------+--------+ Map<ticker, tickerInfo>
* | (local, broadcast, priority edge)
* | Object[]{trade, tradeInfo}
* |
* +--------+--------+
* | Sink |
* +-----------------+
* }</pre>
*/
public class HashMapEnrichment {
private static final String TICKER_INFO_MAP_NAME = "tickerInfoMap";
public static void main(String[] args) {
System.setProperty("hazelcast.logging.type", "log4j");
JetInstance instance = Jet.newJetInstance();
Jet.newJetInstance();
try {
TickerInfo.populateMap(instance.getMap(TICKER_INFO_MAP_NAME));
DAG dag = new DAG();
Vertex tradesSource = dag.newVertex("tradesSource", GenerateTradesP::new);
Vertex readTickerInfoMap = dag.newVertex("readTickerInfoMap", readMapP(TICKER_INFO_MAP_NAME));
Vertex collectToMap = dag.newVertex("collectToMap",
Processors.aggregateP(AggregateOperations.toMap(Entry::getKey, entryValue())));
Vertex hashJoin = dag.newVertex("hashJoin", () -> new HashJoinP<>(Trade::getTicker));
Vertex sink = dag.newVertex("sink", writeLoggerP(o -> Arrays.toString((Object[]) o)));
tradesSource.localParallelism(1);
collectToMap.localParallelism(1);
sink.localParallelism(1);
dag.edge(between(readTickerInfoMap, collectToMap)
.broadcast()
.distributed())
.edge(from(collectToMap).to(hashJoin, 0)
.broadcast()
.priority(-1))
.edge(from(tradesSource).to(hashJoin, 1)
.partitioned(Trade::getTicker))
.edge(between(hashJoin, sink));
instance.newJob(dag).join();
} finally {
Jet.shutdownAll();
}
}
}