|
| 1 | +package io.confluent.developer; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.admin.NewTopic; |
| 4 | +import org.apache.kafka.common.serialization.Serde; |
| 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.Topology; |
| 9 | +import org.apache.kafka.streams.kstream.Consumed; |
| 10 | +import org.apache.kafka.streams.kstream.Produced; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.io.FileInputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.Properties; |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | + |
| 22 | +import org.json.JSONArray; |
| 23 | +import org.json.JSONException; |
| 24 | +import org.json.JSONObject; |
| 25 | + |
| 26 | +public class KafkaStreamsApplication { |
| 27 | + |
| 28 | + private static final Logger logger = LoggerFactory.getLogger(KafkaStreamsApplication.class); |
| 29 | + |
| 30 | + static Topology buildTopology(String inputTopic, String outputTopic) { |
| 31 | + Serde<String> stringSerde = Serdes.String(); |
| 32 | + |
| 33 | + StreamsBuilder builder = new StreamsBuilder(); |
| 34 | + |
| 35 | + builder |
| 36 | + .stream(inputTopic, Consumed.with(stringSerde, stringSerde)) |
| 37 | + .peek((k,v) -> logger.info("Observed event: {}", v)) |
| 38 | + .mapValues(s -> transformMessage(s)) |
| 39 | + .peek((k,v) -> logger.info("Transformed event: {}", v)) |
| 40 | + .to(outputTopic, Produced.with(stringSerde, stringSerde)); |
| 41 | + |
| 42 | + return builder.build(); |
| 43 | + } |
| 44 | + |
| 45 | + private static String transformMessage(String jsonContent) { |
| 46 | + try{ |
| 47 | + JSONObject jsonObject = new JSONObject(jsonContent); |
| 48 | + removeEverySecondProperty(jsonObject); |
| 49 | + return jsonObject.toString(); |
| 50 | + }catch(Exception ex){ |
| 51 | + return jsonContent; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static void removeEverySecondProperty(JSONObject jsonObject) { |
| 56 | + JSONArray keys = jsonObject.names(); |
| 57 | + if (keys != null) { |
| 58 | + for (int i = 0; i < keys.length(); i++) { |
| 59 | + if (i % 2 != 0) { |
| 60 | + String key = keys.getString(i); |
| 61 | + jsonObject.remove(key); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public static void main(String[] args) throws Exception { |
| 69 | + if (args.length < 1) { |
| 70 | + throw new IllegalArgumentException("Configuration file is expected as a first argument"); |
| 71 | + } |
| 72 | + |
| 73 | + Properties props = readProperties(args); |
| 74 | + |
| 75 | + KafkaStreams kafkaStreams = new KafkaStreams( |
| 76 | + buildTopology( |
| 77 | + props.getProperty("input.topic.name"), |
| 78 | + props.getProperty("output.topic.name") |
| 79 | + ), |
| 80 | + props); |
| 81 | + |
| 82 | + Runtime.getRuntime().addShutdownHook(new Thread(kafkaStreams::close)); |
| 83 | + kafkaStreams.start(); |
| 84 | + } |
| 85 | + |
| 86 | + private static Properties readProperties(String[] args) throws IOException { |
| 87 | + Properties props = new Properties(); |
| 88 | + try (InputStream inputStream = new FileInputStream(args[0])) { |
| 89 | + props.load(inputStream); |
| 90 | + } |
| 91 | + return props; |
| 92 | + } |
| 93 | +} |
0 commit comments