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

The following examples show how to use org.apache.kafka.streams.kstream.ValueTransformer. 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: TimeCheckDemo.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 6 votes vote down vote up
public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, AppConfigs.applicationID);
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers);



    StreamsBuilder streamsBuilder = new StreamsBuilder();
    KStream<String, PosInvoice> KS0 = streamsBuilder.stream(AppConfigs.posTopicName,
            Consumed.with(PosSerdes.String(), PosSerdes.PosInvoice())
                    .withTimestampExtractor(new InvoiceTimeExtractor())
    );

    KS0.transformValues(() -> new ValueTransformer<PosInvoice, PosInvoice>() {
        private ProcessorContext context;

        @Override
        public void init(ProcessorContext processorContext) {
            this.context = processorContext;
        }

        @Override
        public PosInvoice transform(PosInvoice invoice) {
            logger.info("Invoice Time: " + new Timestamp(invoice.getCreatedTime()) +
                    " Event Time: " + new Timestamp(context.timestamp()));
            return invoice;
        }

        @Override
        public void close() {
        }
    });

    logger.info("Starting Kafka Streams");
    KafkaStreams myStream = new KafkaStreams(streamsBuilder.build(), props);
    myStream.start();

    Runtime.getRuntime().addShutdownHook(new Thread(myStream::close));
}
 
Example #2
Source File: TracingValueTransformer.java    From brave with Apache License 2.0 5 votes vote down vote up
TracingValueTransformer(KafkaStreamsTracing kafkaStreamsTracing, String spanName,
  ValueTransformer<V, VR> delegateTransformer) {
  this.kafkaStreamsTracing = kafkaStreamsTracing;
  this.tracer = kafkaStreamsTracing.tracer;
  this.spanName = spanName;
  this.delegateTransformer = delegateTransformer;
}
 
Example #3
Source File: KafkaStreamsTracingTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test
public void valueTransformSupplier_should_tag_app_id_and_task_id() {
  ValueTransformer<String, String> processor = fakeValueTransformerSupplier.get();
  processor.init(processorContextSupplier.apply(new RecordHeaders()));
  processor.transform(TEST_VALUE);

  assertThat(spans.get(0).tags())
    .containsOnly(
      entry("kafka.streams.application.id", TEST_APPLICATION_ID),
      entry("kafka.streams.task.id", TEST_TASK_ID));
}
 
Example #4
Source File: Transformer.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Properties properties = new Properties();
    for (String arg : args) {
        String[] split = arg.split("=");
        properties.put(split[0], split[1]);
    }

    String appId = properties.getProperty(StreamsConfig.APPLICATION_ID_CONFIG);
    if (appId == null) {
        properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "apicurio-registry-transformer");
    }

    String inputTopic = properties.getProperty("input-topic");
    if (inputTopic == null) {
        throw new IllegalArgumentException("Missing input topic!");
    }

    String outputTopic = properties.getProperty("output-topic");
    if (outputTopic == null) {
        throw new IllegalArgumentException("Missing output topic!");
    }

    String fnType = properties.getProperty("type");
    if (fnType == null) {
        throw new IllegalArgumentException("Missing transformation type!");
    }
    Type type = Type.valueOf(fnType);

    log.info(String.format("Transforming: %s --> %s [%s]", inputTopic, outputTopic, type));

    StreamsBuilder builder = new StreamsBuilder();
    KStream<String, byte[]> input = builder.stream(
        inputTopic,
        Consumed.with(Serdes.String(), Serdes.ByteArray())
    );

    input.transformValues(() -> new ValueTransformer<byte[], byte[]>() {
        @Override
        public void init(ProcessorContext context) {
        }

        @Override
        public byte[] transform(byte[] value) {
            return type.apply(value);
        }

        @Override
        public void close() {
        }
    }).to(outputTopic, Produced.with(Serdes.String(), Serdes.ByteArray()));

    Topology topology = builder.build(properties);
    KafkaStreams streams = new KafkaStreams(topology, properties);

    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));

    streams.start();
}
 
Example #5
Source File: TracingValueTransformerSupplier.java    From brave with Apache License 2.0 4 votes vote down vote up
/** This wraps transform method to enable tracing. */
@Override public ValueTransformer<V, VR> get() {
  return new TracingValueTransformer<>(kafkaStreamsTracing, spanName, delegateTransformerSupplier.get());
}