Java Code Examples for org.apache.kafka.streams.state.Stores#persistentKeyValueStore()
The following examples show how to use
org.apache.kafka.streams.state.Stores#persistentKeyValueStore() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopologyProducer.java From quarkus-quickstarts with Apache License 2.0 | 5 votes |
@Produces public Topology buildTopology() { StreamsBuilder builder = new StreamsBuilder(); JsonbSerde<WeatherStation> weatherStationSerde = new JsonbSerde<>(WeatherStation.class); JsonbSerde<Aggregation> aggregationSerde = new JsonbSerde<>(Aggregation.class); KeyValueBytesStoreSupplier storeSupplier = Stores.persistentKeyValueStore(WEATHER_STATIONS_STORE); GlobalKTable<Integer, WeatherStation> stations = builder.globalTable( WEATHER_STATIONS_TOPIC, Consumed.with(Serdes.Integer(), weatherStationSerde)); builder.stream( TEMPERATURE_VALUES_TOPIC, Consumed.with(Serdes.Integer(), Serdes.String())) .join( stations, (stationId, timestampAndValue) -> stationId, (timestampAndValue, station) -> { String[] parts = timestampAndValue.split(";"); return new TemperatureMeasurement(station.id, station.name, Instant.parse(parts[0]), Double.valueOf(parts[1])); }) .groupByKey() .aggregate( Aggregation::new, (stationId, value, aggregation) -> aggregation.updateFrom(value), Materialized.<Integer, Aggregation> as(storeSupplier) .withKeySerde(Serdes.Integer()) .withValueSerde(aggregationSerde)) .toStream() .to( TEMPERATURES_AGGREGATED_TOPIC, Produced.with(Serdes.Integer(), aggregationSerde)); return builder.build(); }
Example 2
Source File: CoGroupingApplication.java From kafka-streams-in-action with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { StreamsConfig streamsConfig = new StreamsConfig(getProperties()); Deserializer<String> stringDeserializer = Serdes.String().deserializer(); Serializer<String> stringSerializer = Serdes.String().serializer(); Serde<Tuple<List<ClickEvent>, List<StockTransaction>>> eventPerformanceTuple = StreamsSerdes.EventTransactionTupleSerde(); Serializer<Tuple<List<ClickEvent>, List<StockTransaction>>> tupleSerializer = eventPerformanceTuple.serializer(); Serde<StockTransaction> stockTransactionSerde = StreamsSerdes.StockTransactionSerde(); Deserializer<StockTransaction> stockTransactionDeserializer = stockTransactionSerde.deserializer(); Serde<ClickEvent> clickEventSerde = StreamsSerdes.ClickEventSerde(); Deserializer<ClickEvent> clickEventDeserializer = clickEventSerde.deserializer(); Topology topology = new Topology(); Map<String, String> changeLogConfigs = new HashMap<>(); changeLogConfigs.put("retention.ms", "120000"); changeLogConfigs.put("cleanup.policy", "compact,delete"); KeyValueBytesStoreSupplier storeSupplier = Stores.persistentKeyValueStore(TUPLE_STORE_NAME); StoreBuilder<KeyValueStore<String, Tuple<List<ClickEvent>, List<StockTransaction>>>> storeBuilder = Stores.keyValueStoreBuilder(storeSupplier, Serdes.String(), eventPerformanceTuple).withLoggingEnabled(changeLogConfigs); topology.addSource("Txn-Source", stringDeserializer, stockTransactionDeserializer, "stock-transactions") .addSource("Events-Source", stringDeserializer, clickEventDeserializer, "events") .addProcessor("Txn-Processor", StockTransactionProcessor::new, "Txn-Source") .addProcessor("Events-Processor", ClickEventProcessor::new, "Events-Source") .addProcessor("CoGrouping-Processor", CogroupingProcessor::new, "Txn-Processor", "Events-Processor") .addStateStore(storeBuilder, "CoGrouping-Processor") .addSink("Tuple-Sink", "cogrouped-results", stringSerializer, tupleSerializer, "CoGrouping-Processor"); topology.addProcessor("Print", new KStreamPrinter("Co-Grouping"), "CoGrouping-Processor"); MockDataProducer.produceStockTransactionsAndDayTradingClickEvents(50, 100, 100, StockTransaction::getSymbol); KafkaStreams kafkaStreams = new KafkaStreams(topology, streamsConfig); System.out.println("Co-Grouping App Started"); kafkaStreams.cleanUp(); kafkaStreams.start(); Thread.sleep(70000); System.out.println("Shutting down the Co-Grouping App now"); kafkaStreams.close(); MockDataProducer.shutdown(); }
Example 3
Source File: CoGroupingListeningExampleApplication.java From kafka-streams-in-action with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { StreamsConfig streamsConfig = new StreamsConfig(getProperties()); Deserializer<String> stringDeserializer = Serdes.String().deserializer(); Serializer<String> stringSerializer = Serdes.String().serializer(); Serde<Tuple<List<ClickEvent>, List<StockTransaction>>> eventPerformanceTuple = StreamsSerdes.EventTransactionTupleSerde(); Serializer<Tuple<List<ClickEvent>, List<StockTransaction>>> tupleSerializer = eventPerformanceTuple.serializer(); Serde<StockTransaction> stockTransactionSerde = StreamsSerdes.StockTransactionSerde(); Deserializer<StockTransaction> stockTransactionDeserializer = stockTransactionSerde.deserializer(); Serde<ClickEvent> clickEventSerde = StreamsSerdes.ClickEventSerde(); Deserializer<ClickEvent> clickEventDeserializer = clickEventSerde.deserializer(); Topology topology = new Topology(); Map<String, String> changeLogConfigs = new HashMap<>(); changeLogConfigs.put("retention.ms","120000" ); changeLogConfigs.put("cleanup.policy", "compact,delete"); KeyValueBytesStoreSupplier storeSupplier = Stores.persistentKeyValueStore(TUPLE_STORE_NAME); StoreBuilder<KeyValueStore<String, Tuple<List<ClickEvent>, List<StockTransaction>>>> builder = Stores.keyValueStoreBuilder(storeSupplier, Serdes.String(), eventPerformanceTuple); topology.addSource("Txn-Source", stringDeserializer, stockTransactionDeserializer, "stock-transactions") .addSource( "Events-Source", stringDeserializer, clickEventDeserializer, "events") .addProcessor("Txn-Processor", StockTransactionProcessor::new, "Txn-Source") .addProcessor("Events-Processor", ClickEventProcessor::new, "Events-Source") .addProcessor("CoGrouping-Processor", CogroupingProcessor::new, "Txn-Processor", "Events-Processor") .addStateStore(builder.withLoggingEnabled(changeLogConfigs), "CoGrouping-Processor") .addSink("Tuple-Sink", "cogrouped-results", stringSerializer, tupleSerializer, "CoGrouping-Processor"); topology.addProcessor("Print", new KStreamPrinter("Co-Grouping"), "CoGrouping-Processor"); MockDataProducer.produceStockTransactionsAndDayTradingClickEvents(50, 100, 100, StockTransaction::getSymbol); KafkaStreams kafkaStreams = new KafkaStreams(topology, streamsConfig); kafkaStreams.setGlobalStateRestoreListener(new LoggingStateRestoreListener()); kafkaStreams.setUncaughtExceptionHandler((thread, exception) -> LOG.error("Thread [{}] encountered [{}]", thread.getName(), exception.getMessage()) ); kafkaStreams.setStateListener((newState, oldState) -> { if (oldState == KafkaStreams.State.REBALANCING && newState== KafkaStreams.State.RUNNING) { LOG.info("Topology Layout {}", topology.describe()); LOG.info("Thread metadata {}", kafkaStreams.localThreadsMetadata()); } }); LOG.info("Co-Grouping App Started"); kafkaStreams.cleanUp(); kafkaStreams.start(); Thread.sleep(70000); LOG.info("Shutting down the Co-Grouping metrics App now"); kafkaStreams.close(); MockDataProducer.shutdown(); }