Search in sources :

Example 6 with PlayEvent

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();
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) SpecificAvroSerializer(io.confluent.kafka.streams.serdes.avro.SpecificAvroSerializer) Properties(java.util.Properties) PlayEvent(io.confluent.examples.streams.avro.PlayEvent)

Aggregations

PlayEvent (io.confluent.examples.streams.avro.PlayEvent)6 Properties (java.util.Properties)6 SpecificAvroSerializer (io.confluent.kafka.streams.serdes.avro.SpecificAvroSerializer)4 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)4 Song (io.confluent.examples.streams.avro.Song)3 AbstractKafkaAvroSerDeConfig (io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Serdes (org.apache.kafka.common.serialization.Serdes)3 KafkaStreams (org.apache.kafka.streams.KafkaStreams)3 KeyValue (org.apache.kafka.streams.KeyValue)3 SpecificAvroSerde (io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde)2 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)2 Bytes (org.apache.kafka.common.utils.Bytes)2 Consumed (org.apache.kafka.streams.Consumed)2 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2 Materialized (org.apache.kafka.streams.kstream.Materialized)2 Serialized (org.apache.kafka.streams.kstream.Serialized)2 HostInfo (org.apache.kafka.streams.state.HostInfo)2