forked from confluentinc/kafka-streams-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionWindowsExampleDriver.java
159 lines (139 loc) · 8.13 KB
/
SessionWindowsExampleDriver.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
/*
* Copyright Confluent Inc.
*
* 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.
*/
package io.confluent.examples.streams;
import io.confluent.examples.streams.avro.PlayEvent;
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig;
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerializer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.Serdes;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
/**
* This is a sample driver for the {@link SessionWindowsExample}.
* To run this driver please first refer to the instructions in {@link SessionWindowsExample}.
* You can then run this class directly in your IDE or via the command line.
* <p>
* To run via the command line you might want to package as a fatjar first. Please refer to:
* <a href='https://github.com/confluentinc/kafka-streams-examples#packaging-and-running'>Packaging</a>
* <p>
* Once packaged you can then run:
* <pre>
* {@code
* $ java -cp target/kafka-streams-examples-7.0.0-standalone.jar io.confluent.examples.streams.SessionWindowsExampleDriver
* }
* </pre>
*/
public class SessionWindowsExampleDriver {
private static final int NUM_RECORDS_SENT = 8;
public static void main(final String[] args) {
final String bootstrapServers = args.length > 0 ? args[0] : "localhost:9092";
final String schemaRegistryUrl = args.length > 1 ? args[1] : "http://localhost:8081";
producePlayEvents(bootstrapServers, schemaRegistryUrl);
consumeOutput(bootstrapServers);
}
private static void producePlayEvents(final String bootstrapServers, final String schemaRegistryUrl) {
final SpecificAvroSerializer<PlayEvent> playEventSerializer = new SpecificAvroSerializer<>();
final Map<String, String> serdeConfig = Collections.singletonMap(
AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
playEventSerializer.configure(serdeConfig, false);
final Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
final KafkaProducer<String, PlayEvent> playEventProducer = new KafkaProducer<>(producerProperties,
Serdes.String() .serializer(),
playEventSerializer);
final long start = System.currentTimeMillis();
final long billEvenTime = start + SessionWindowsExample.INACTIVITY_GAP.toMillis() / 10;
// create three sessions with different times
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start,
"jo",
new PlayEvent(1L, 10L)));
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
billEvenTime,
"bill",
new PlayEvent(2L, 10L)));
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + SessionWindowsExample.INACTIVITY_GAP.toMillis() / 5,
"sarah",
new PlayEvent(2L, 10L)));
// out-of-order event for jo that is outside inactivity gap so will create a new session
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + SessionWindowsExample.INACTIVITY_GAP.toMillis() + 1,
"jo",
new PlayEvent(1L, 10L)));
// extend current session for bill
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + SessionWindowsExample.INACTIVITY_GAP.toMillis(),
"bill",
new PlayEvent(2L, 10L)));
// new session for sarah
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + 2 * SessionWindowsExample.INACTIVITY_GAP.toMillis(),
"sarah",
new PlayEvent(2L, 10L)));
// send earlier event for jo that will merge the 2 previous sessions
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + SessionWindowsExample.INACTIVITY_GAP.toMillis() / 2,
"jo",
new PlayEvent(1L, 10L)));
// new session for bill
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + 3 * SessionWindowsExample.INACTIVITY_GAP.toMillis(),
"bill",
new PlayEvent(2L, 10L)));
// extend session session for sarah
// new session for sarah
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS,
null,
start + 2 * SessionWindowsExample.INACTIVITY_GAP.toMillis() +
SessionWindowsExample.INACTIVITY_GAP.toMillis() / 5,
"sarah",
new PlayEvent(2L, 10L)));
playEventProducer.close();
}
private static void consumeOutput(final String bootstrapServers) {
final Properties consumerProps = new Properties();
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "session-windows-consumer");
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, Serdes.String().deserializer().getClass());
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, Serdes.Long().deserializer().getClass());
final KafkaConsumer<String, Long> consumer = new KafkaConsumer<>(consumerProps);
consumer.subscribe(Collections.singleton(SessionWindowsExample.PLAY_EVENTS_PER_SESSION));
int received = 0;
while(received < NUM_RECORDS_SENT) {
final ConsumerRecords<String, Long> records = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
records.forEach(record -> System.out.println(record.key() + " = " + record.value()));
received += records.count();
}
consumer.close();
}
}