use of org.apache.kafka.streams.kstream.internals.WindowedDeserializer in project kafka-streams-examples by confluentinc.
the class TopArticlesLambdaExampleTest method shouldProduceTopNArticles.
@Test
public void shouldProduceTopNArticles() throws Exception {
final Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, CLUSTER.schemaRegistryUrl());
final KafkaProducer<String, GenericRecord> producer = new KafkaProducer<>(props);
final GenericRecordBuilder pageViewBuilder = new GenericRecordBuilder(loadSchema("pageview.avsc"));
pageViewBuilder.set("flags", "ARTICLE");
pageViewBuilder.set("industry", "eng");
createPageViews(producer, pageViewBuilder, 3, "news");
createPageViews(producer, pageViewBuilder, 2, "random");
createPageViews(producer, pageViewBuilder, 1, "stuff");
pageViewBuilder.set("industry", "science");
createPageViews(producer, pageViewBuilder, 3, "index");
createPageViews(producer, pageViewBuilder, 2, "about");
createPageViews(producer, pageViewBuilder, 1, "help");
final Map<String, List<String>> expected = new HashMap<>();
expected.put("eng", Arrays.asList("news", "random", "stuff"));
expected.put("science", Arrays.asList("index", "about", "help"));
streams.start();
final Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "top-articles-consumer");
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final Deserializer<Windowed<String>> windowedDeserializer = new WindowedDeserializer<>(Serdes.String().deserializer());
final KafkaConsumer<Windowed<String>, String> consumer = new KafkaConsumer<>(consumerProperties, windowedDeserializer, Serdes.String().deserializer());
consumer.subscribe(Collections.singletonList(TopArticlesLambdaExample.TOP_NEWS_PER_INDUSTRY_TOPIC));
final Map<String, List<String>> received = new HashMap<>();
final long timeout = System.currentTimeMillis() + 30000L;
boolean done = false;
while (System.currentTimeMillis() < timeout && !done) {
final ConsumerRecords<Windowed<String>, String> records = consumer.poll(10);
records.forEach(record -> received.put(record.key().key(), Arrays.asList(record.value().split("\n"))));
done = received.equals(expected);
}
assertThat(received, equalTo(expected));
}
use of org.apache.kafka.streams.kstream.internals.WindowedDeserializer in project kafka-streams-examples by confluentinc.
the class TopArticlesExampleDriver method consumeOutput.
private static void consumeOutput(String bootstrapServers, String schemaRegistryUrl) {
final Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProperties.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "top-articles-lambda-example-consumer");
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final Deserializer<Windowed<String>> windowedDeserializer = new WindowedDeserializer<>(Serdes.String().deserializer());
final KafkaConsumer<Windowed<String>, String> consumer = new KafkaConsumer<>(consumerProperties, windowedDeserializer, Serdes.String().deserializer());
consumer.subscribe(Collections.singleton(TopArticlesLambdaExample.TOP_NEWS_PER_INDUSTRY_TOPIC));
while (true) {
ConsumerRecords<Windowed<String>, String> consumerRecords = consumer.poll(Long.MAX_VALUE);
for (ConsumerRecord<Windowed<String>, String> consumerRecord : consumerRecords) {
System.out.println(consumerRecord.key().key() + "@" + consumerRecord.key().window().start() + "=" + consumerRecord.value());
}
}
}
Aggregations