-
Notifications
You must be signed in to change notification settings - Fork 135
/
JoinExample.java
163 lines (145 loc) · 7.22 KB
/
JoinExample.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package samza.examples.cookbook;
import java.io.Serializable;
import org.apache.samza.application.StreamApplication;
import org.apache.samza.application.descriptors.StreamApplicationDescriptor;
import org.apache.samza.operators.KV;
import org.apache.samza.operators.MessageStream;
import org.apache.samza.operators.OutputStream;
import org.apache.samza.operators.functions.JoinFunction;
import org.apache.samza.serializers.JsonSerdeV2;
import org.apache.samza.serializers.KVSerde;
import org.apache.samza.serializers.StringSerde;
import org.apache.samza.system.kafka.descriptors.KafkaInputDescriptor;
import org.apache.samza.system.kafka.descriptors.KafkaOutputDescriptor;
import org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import samza.examples.cookbook.data.AdClick;
import samza.examples.cookbook.data.PageView;
import java.time.Duration;
import java.util.List;
import java.util.Map;
/**
* In this example, we join a stream of Page views with a stream of Ad clicks. For instance, this is helpful for
* analysis on what pages served an Ad that was clicked.
*
* <p> Concepts covered: Performing stream to stream Joins.
*
* To run the below example:
*
* <ol>
* <li>
* Ensure that the topics "pageview-join-input", "adclick-join-input" are created <br/>
* ./deploy/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic pageview-join-input --partitions 2 --replication-factor 1
* ./deploy/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic adclick-join-input --partitions 2 --replication-factor 1
* </li>
* <li>
* Run the application using the run-app.sh script <br/>
* ./deploy/samza/bin/run-app.sh --config-path=$PWD/deploy/samza/config/join-example.properties
* </li>
* <li>
* Produce some messages to the "pageview-join-input" topic <br/>
* ./deploy/kafka/bin/kafka-console-producer.sh --topic pageview-join-input --broker-list localhost:9092 <br/>
* {"userId": "user1", "country": "india", "pageId":"google.com"} <br/>
* {"userId": "user2", "country": "china", "pageId":"yahoo.com"}
* </li>
* <li>
* Produce some messages to the "adclick-join-input" topic with the same pageKey <br/>
* ./deploy/kafka/bin/kafka-console-producer.sh --topic adclick-join-input --broker-list localhost:9092 <br/>
* {"userId": "user1", "adId": "adClickId1", "pageId":"google.com"} <br/>
* {"userId": "user1", "adId": "adClickId2", "pageId":"yahoo.com"}
* </li>
* <li>
* Consume messages from the "pageview-adclick-join-output" topic <br/>
* ./deploy/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic pageview-adclick-join-output --property print.key=true
* </li>
* </ol>
*
*/
public class JoinExample implements StreamApplication, Serializable {
private static final String KAFKA_SYSTEM_NAME = "kafka";
private static final List<String> KAFKA_CONSUMER_ZK_CONNECT = ImmutableList.of("localhost:2181");
private static final List<String> KAFKA_PRODUCER_BOOTSTRAP_SERVERS = ImmutableList.of("localhost:9092");
private static final Map<String, String> KAFKA_DEFAULT_STREAM_CONFIGS = ImmutableMap.of("replication.factor", "1");
private static final String PAGEVIEW_STREAM_ID = "pageview-join-input";
private static final String ADCLICK_STREAM_ID = "adclick-join-input";
private static final String OUTPUT_STREAM_ID = "pageview-adclick-join-output";
@Override
public void describe(StreamApplicationDescriptor appDescriptor) {
StringSerde stringSerde = new StringSerde();
JsonSerdeV2<PageView> pageViewSerde = new JsonSerdeV2<>(PageView.class);
JsonSerdeV2<AdClick> adClickSerde = new JsonSerdeV2<>(AdClick.class);
JsonSerdeV2<JoinResult> joinResultSerde = new JsonSerdeV2<>(JoinResult.class);
KafkaSystemDescriptor kafkaSystemDescriptor = new KafkaSystemDescriptor(KAFKA_SYSTEM_NAME)
.withConsumerZkConnect(KAFKA_CONSUMER_ZK_CONNECT)
.withProducerBootstrapServers(KAFKA_PRODUCER_BOOTSTRAP_SERVERS)
.withDefaultStreamConfigs(KAFKA_DEFAULT_STREAM_CONFIGS);
KafkaInputDescriptor<PageView> pageViewInputDescriptor =
kafkaSystemDescriptor.getInputDescriptor(PAGEVIEW_STREAM_ID, pageViewSerde);
KafkaInputDescriptor<AdClick> adClickInputDescriptor =
kafkaSystemDescriptor.getInputDescriptor(ADCLICK_STREAM_ID, adClickSerde);
KafkaOutputDescriptor<JoinResult> joinResultOutputDescriptor =
kafkaSystemDescriptor.getOutputDescriptor(OUTPUT_STREAM_ID, joinResultSerde);
appDescriptor.withDefaultSystem(kafkaSystemDescriptor);
MessageStream<PageView> pageViews = appDescriptor.getInputStream(pageViewInputDescriptor);
MessageStream<AdClick> adClicks = appDescriptor.getInputStream(adClickInputDescriptor);
OutputStream<JoinResult> joinResults = appDescriptor.getOutputStream(joinResultOutputDescriptor);
JoinFunction<String, PageView, AdClick, JoinResult> pageViewAdClickJoinFunction =
new JoinFunction<String, PageView, AdClick, JoinResult>() {
@Override
public JoinResult apply(PageView pageView, AdClick adClick) {
return new JoinResult(pageView.pageId, pageView.userId, pageView.country, adClick.getAdId());
}
@Override
public String getFirstKey(PageView pageView) {
return pageView.pageId;
}
@Override
public String getSecondKey(AdClick adClick) {
return adClick.getPageId();
}
};
MessageStream<PageView> repartitionedPageViews =
pageViews
.partitionBy(pv -> pv.pageId, pv -> pv, KVSerde.of(stringSerde, pageViewSerde), "pageview")
.map(KV::getValue);
MessageStream<AdClick> repartitionedAdClicks =
adClicks
.partitionBy(AdClick::getPageId, ac -> ac, KVSerde.of(stringSerde, adClickSerde), "adclick")
.map(KV::getValue);
repartitionedPageViews
.join(repartitionedAdClicks, pageViewAdClickJoinFunction,
stringSerde, pageViewSerde, adClickSerde, Duration.ofMinutes(3), "join")
.sendTo(joinResults);
}
static class JoinResult {
public String pageId;
public String userId;
public String country;
public String adId;
public JoinResult(String pageId, String userId, String country, String adId) {
this.pageId = pageId;
this.userId = userId;
this.country = country;
this.adId = adId;
}
}
}