use of io.confluent.examples.streams.avro.PlayEvent in project kafka-streams-examples by confluentinc.
the class SessionWindowsExampleDriver method producePlayEvents.
private static void producePlayEvents(final String bootstrapServers, final String schemaRegistryUrl) {
final SpecificAvroSerializer<PlayEvent> playEventSerializer = new SpecificAvroSerializer<>();
final Map<String, String> serdeConfig = Collections.singletonMap(AbstractKafkaAvroSerDeConfig.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 / 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 / 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 + 1, "jo", new PlayEvent(1L, 10L)));
// extend current session for bill
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS, null, start + SessionWindowsExample.INACTIVITY_GAP, "bill", new PlayEvent(2L, 10L)));
// new session for sarah
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS, null, start + 2 * SessionWindowsExample.INACTIVITY_GAP, "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 / 2, "jo", new PlayEvent(1L, 10L)));
// new session for bill
playEventProducer.send(new ProducerRecord<>(SessionWindowsExample.PLAY_EVENTS, null, start + 3 * SessionWindowsExample.INACTIVITY_GAP, "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 + SessionWindowsExample.INACTIVITY_GAP / 5, "sarah", new PlayEvent(2L, 10L)));
playEventProducer.close();
}
Aggregations