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

The following examples show how to use org.apache.kafka.streams.kstream.ValueMapper. 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: SpanAggregationTopology.java    From zipkin-storage-kafka with Apache License 2.0 5 votes vote down vote up
ValueMapper<List<Span>, List<DependencyLink>> spansToDependencyLinks() {
  return (spans) -> {
    if (spans == null) return new ArrayList<>();
    DependencyLinker linker = new DependencyLinker();
    return linker.putTrace(spans).link();
  };
}
 
Example #2
Source File: DynamicOutputTopic.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public Topology buildTopology(Properties envProps) {
    final StreamsBuilder builder = new StreamsBuilder();
    final String orderInputTopic = envProps.getProperty("input.topic.name");
    final String orderOutputTopic = envProps.getProperty("output.topic.name");
    final String specialOrderOutput = envProps.getProperty("special.order.topic.name");

    final Serde<Long> longSerde = getPrimitiveAvroSerde(envProps, true);
    final Serde<Order> orderSerde = getSpecificAvroSerde(envProps);
    final Serde<CompletedOrder> completedOrderSerde = getSpecificAvroSerde(envProps);

    final ValueMapper<Order, CompletedOrder> orderProcessingSimulator = v -> {
       double amount = v.getQuantity() * FAKE_PRICE;
       return CompletedOrder.newBuilder().setAmount(amount).setId(v.getId() + "-" + v.getSku()).setName(v.getName()).build();
    };

    final TopicNameExtractor<Long, CompletedOrder> orderTopicNameExtractor = (key, completedOrder, recordContext) -> {
          final String compositeId = completedOrder.getId();
          final String skuPart = compositeId.substring(compositeId.indexOf('-') + 1, 5);
          final String outTopic;
          if (skuPart.equals("QUA")) {
              outTopic = specialOrderOutput;
          } else {
              outTopic = orderOutputTopic;
          }
          return outTopic;
    };

    final KStream<Long, Order> exampleStream = builder.stream(orderInputTopic, Consumed.with(longSerde, orderSerde));

    exampleStream.mapValues(orderProcessingSimulator).to(orderTopicNameExtractor, Produced.with(longSerde, completedOrderSerde));

    return builder.build();
}
 
Example #3
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public static <K, VV, EV> KGraph<K, VV, EV> fromEdges(
    KTable<Edge<K>, EV> edges,
    ValueMapper<K, VV> vertexValueInitializer,
    GraphSerialized<K, VV, EV> serialized) {

    KTable<K, VV> vertices = edges
        .toStream()
        .flatMap(new EmitSrcAndTarget<>(vertexValueInitializer))
        .groupByKey(Grouped.with(serialized.keySerde(), new KryoSerde<>()))
        .<VV>reduce((v1, v2) -> v2,
            Materialized.with(serialized.keySerde(), serialized.vertexValueSerde()));

    return new KGraph<>(vertices, edges, serialized);
}
 
Example #4
Source File: SummaryAggregation.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
protected SummaryAggregation(EdgeFoldFunction<K, EV, S> updateFun, Reducer<S> combineFun, ValueMapper<S, T> transform, S initialValue, boolean transientState) {
    this.updateFun = updateFun;
    this.combineFun = combineFun;
    this.transform = transform;
    this.initialValue = initialValue;
    this.transientState = transientState;
}
 
Example #5
Source File: EdgeStream.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
/**
 * @return the vertex DataStream.
 */
@Override
public KStream<K, Void> vertices() {
    ValueMapper<K, Void> vertexValueInitializer = e -> null;
    return edges
        .flatMap(new KGraph.EmitSrcAndTarget<K, Void, EV>(vertexValueInitializer))
        .filter(new FilterDistinctVertices<K>());
}
 
Example #6
Source File: SummaryBulkAggregation.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public SummaryBulkAggregation(EdgeFoldFunction<K, EV, S> updateFun,
                              Reducer<S> combineFun,
                              ValueMapper<S, T> transformFun,
                              S initialVal,
                              long timeMillis,
                              boolean transientState) {
    super(updateFun, combineFun, transformFun, initialVal, transientState);
    this.timeMillis = timeMillis;
}
 
Example #7
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 4 votes vote down vote up
public EmitSrcAndTarget(ValueMapper<K, VV> vertexValueInitializer) {
    this.vertexValueInitializer = vertexValueInitializer;
}
 
Example #8
Source File: SummaryAggregation.java    From kafka-graphs with Apache License 2.0 4 votes vote down vote up
public ValueMapper<S, T> transform() {
    return transform;
}
 
Example #9
Source File: SummaryAggregation.java    From kafka-graphs with Apache License 2.0 4 votes vote down vote up
protected ValueMapper<S, S> aggregator(final KStream<Edge<K>, EV> edgeStream) {
    return new Merger<>(initialValue(), combineFun(), isTransientState());
}
 
Example #10
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 3 votes vote down vote up
/**
 * Create a mapValues transformer, similar to {@link KStream#mapValues(ValueMapper)}, 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)
 *        .transformValues(kafkaStreamsTracing.mapValues("myMapValues", v -> ...)
 *        .to(outputTopic);
 * }</pre>
 */
public <V, VR> ValueTransformerSupplier<V, VR> mapValues(String spanName,
  ValueMapper<V, VR> mapper) {
  return new TracingValueTransformerSupplier<>(this, spanName, () ->
    new AbstractTracingValueTransformer<V, VR>() {
      @Override public VR transform(V value) {
        return mapper.apply(value);
      }
    });
}