josephxsxn

KTable Example 1

Jul 11th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package com.blah.ktable;
  2.  
  3. import java.util.Properties;
  4.  
  5. import org.apache.kafka.common.serialization.Serdes;
  6. import org.apache.kafka.streams.KafkaStreams;
  7. import org.apache.kafka.streams.StreamsBuilder;
  8. import org.apache.kafka.streams.StreamsConfig;
  9. import org.apache.kafka.streams.kstream.ForeachAction;
  10. import org.apache.kafka.streams.kstream.KTable;
  11.  
  12. public class KTableReader {
  13.    
  14.      public static void main(final String[] args) throws Exception {
  15.             Properties config = new Properties();
  16.             config.put(StreamsConfig.APPLICATION_ID_CONFIG, "test_ktable1");
  17.             config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka-dev01.socialgist.local:9092");
  18.             config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  19.             config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  20.             config.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, "10000");
  21.      
  22.             StreamsBuilder builder = new StreamsBuilder();
  23.             KTable<String, String> source = builder.table("test_ktable1");
  24.             source.toStream().foreach(new ForeachAction<String, String>() {
  25.                 public void apply(String key, String value) {
  26.                     System.out.println(key + ": " + value);
  27.                 }
  28.              });
  29.                      
  30.             KafkaStreams streams = new KafkaStreams(builder.build(), config);
  31.             streams.start();
  32.         }
  33.      
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment