org.apache.kafka.streams.kstream.TransformerSupplier Java Examples

The following examples show how to use org.apache.kafka.streams.kstream.TransformerSupplier. 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: StockPerformanceStreamsAndProcessorMultipleValuesApplication.java    From kafka-streams-in-action with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {


        StreamsConfig streamsConfig = new StreamsConfig(getProperties());
        Serde<String> stringSerde = Serdes.String();
        Serde<StockPerformance> stockPerformanceSerde = StreamsSerdes.StockPerformanceSerde();
        Serde<StockTransaction> stockTransactionSerde = StreamsSerdes.StockTransactionSerde();


        StreamsBuilder builder = new StreamsBuilder();

        String stocksStateStore = "stock-performance-store";
        double differentialThreshold = 0.05;

        TransformerSupplier<String, StockTransaction, KeyValue<String, List<KeyValue<String, StockPerformance>>>> transformerSupplier =
                () -> new StockPerformanceMultipleValuesTransformer(stocksStateStore, differentialThreshold);

        KeyValueBytesStoreSupplier storeSupplier = Stores.lruMap(stocksStateStore, 100);
        StoreBuilder<KeyValueStore<String, StockPerformance>> storeBuilder = Stores.keyValueStoreBuilder(storeSupplier, Serdes.String(), stockPerformanceSerde);

        builder.addStateStore(storeBuilder);

        builder.stream("stock-transactions", Consumed.with(stringSerde, stockTransactionSerde))
                .transform(transformerSupplier, stocksStateStore).flatMap((dummyKey,valueList) -> valueList)
                .print(Printed.<String, StockPerformance>toSysOut().withLabel("StockPerformance"));
                //.to(stringSerde, stockPerformanceSerde, "stock-performance");


        KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), streamsConfig);
        MockDataProducer.produceStockTransactionsWithKeyFunction(50, 50, 25, StockTransaction::getSymbol);
        System.out.println("Stock Analysis KStream/Process API App Started");
        kafkaStreams.cleanUp();
        kafkaStreams.start();
        Thread.sleep(70000);
        System.out.println("Shutting down the Stock KStream/Process API Analysis App now");
        kafkaStreams.close();
        MockDataProducer.shutdown();
    }
 
Example #2
Source File: TracingTransformerSupplier.java    From brave with Apache License 2.0 5 votes vote down vote up
TracingTransformerSupplier(KafkaStreamsTracing kafkaStreamsTracing,
  String spanName,
  TransformerSupplier<K, V, R> delegateTransformerSupplier) {
  this.kafkaStreamsTracing = kafkaStreamsTracing;
  this.spanName = spanName;
  this.delegateTransformerSupplier = delegateTransformerSupplier;
}
 
Example #3
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 3 votes vote down vote up
/**
 * Create a map transformer, similar to {@link KStream#map(KeyValueMapper)}, where its mapper
 * action will be recorded in a new span with the indicated name.
 *
 * <p>Simple example using Kafka Streams DSL:
 * <pre>{@code
 * StreamsBuilder builder = new StreamsBuilder();
 * builder.stream(inputTopic)
 *        .transform(kafkaStreamsTracing.map("myMap", (k, v) -> ...)
 *        .to(outputTopic);
 * }</pre>
 */
public <K, V, KR, VR> TransformerSupplier<K, V, KeyValue<KR, VR>> map(String spanName,
  KeyValueMapper<K, V, KeyValue<KR, VR>> mapper) {
  return new TracingTransformerSupplier<>(this, spanName, () ->
    new AbstractTracingTransformer<K, V, KeyValue<KR, VR>>() {
      @Override public KeyValue<KR, VR> transform(K key, V value) {
        return mapper.apply(key, value);
      }
    });
}
 
Example #4
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 3 votes vote down vote up
/**
 * Create a flatMap transformer, similar to {@link KStream#flatMap(KeyValueMapper)}, where its
 * mapper action will be recorded in a new span with the indicated name.
 *
 * <p>Simple example using Kafka Streams DSL:
 * <pre>{@code
 * StreamsBuilder builder = new StreamsBuilder();
 * builder.stream(inputTopic)
 *        .flatTransform(kafkaStreamsTracing.flatMap("myflatMap", (k, v) -> ...)
 *        .to(outputTopic);
 * }</pre>
 */
public <K, V, KR, VR> TransformerSupplier<K, V, Iterable<KeyValue<KR, VR>>> flatMap(
  String spanName,
  KeyValueMapper<K, V, Iterable<KeyValue<KR, VR>>> mapper) {
  return new TracingTransformerSupplier<>(this, spanName, () ->
    new AbstractTracingTransformer<K, V, Iterable<KeyValue<KR, VR>>>() {
      @Override public Iterable<KeyValue<KR, VR>> transform(K key, V value) {
        return mapper.apply(key, value);
      }
    });
}
 
Example #5
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 2 votes vote down vote up
/**
 * Create a tracing-decorated {@link TransformerSupplier}
 *
 * <p>Simple example using Kafka Streams DSL:
 * <pre>{@code
 * StreamsBuilder builder = new StreamsBuilder();
 * builder.stream(inputTopic)
 *        .transform(kafkaStreamsTracing.transformer("my-transformer", myTransformerSupplier)
 *        .to(outputTopic);
 * }</pre>
 */
public <K, V, R> TransformerSupplier<K, V, R> transformer(String spanName,
  TransformerSupplier<K, V, R> transformerSupplier) {
  return new TracingTransformerSupplier<>(this, spanName, transformerSupplier);
}
 
Example #6
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 2 votes vote down vote up
/**
 * Create a filter transformer.
 *
 * WARNING: this filter implementation uses the Streams transform API, meaning that
 * re-partitioning can occur if a key modifying operation like grouping or joining operation is
 * applied after this filter.
 *
 * In that case, consider using {@link #markAsFiltered(String, Predicate)} instead which uses
 * {@link ValueTransformerWithKey} API instead.
 *
 * <p>Simple example using Kafka Streams DSL:
 * <pre>{@code
 * StreamsBuilder builder = new StreamsBuilder();
 * builder.stream(inputTopic)
 *       .transform(kafkaStreamsTracing.filter("myFilter", (k, v) -> ...)
 *       .to(outputTopic);
 * }</pre>
 */
public <K, V> TransformerSupplier<K, V, KeyValue<K, V>> filter(String spanName,
  Predicate<K, V> predicate) {
  return new TracingFilterTransformerSupplier<>(this, spanName, predicate, false);
}
 
Example #7
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 2 votes vote down vote up
/**
 * Create a filterNot transformer.
 *
 * WARNING: this filter implementation uses the Streams transform API, meaning that
 * re-partitioning can occur if a key modifying operation like grouping or joining operation is
 * applied after this filter. In that case, consider using {@link #markAsNotFiltered(String,
 * Predicate)} instead which uses {@link ValueTransformerWithKey} API instead.
 *
 * <p>Simple example using Kafka Streams DSL:
 * <pre>{@code
 * StreamsBuilder builder = new StreamsBuilder();
 * builder.stream(inputTopic)
 *       .transform(kafkaStreamsTracing.filterNot("myFilter", (k, v) -> ...)
 *       .to(outputTopic);
 * }</pre>
 */
public <K, V> TransformerSupplier<K, V, KeyValue<K, V>> filterNot(String spanName,
  Predicate<K, V> predicate) {
  return new TracingFilterTransformerSupplier<>(this, spanName, predicate, true);
}