Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map input KStream <String,String> into <String, CarClass> using KeyValueMapper?

Receiving the Json data which i simply want to map on CarClass and want to create new stream but map method doesn't allow me it to map on custom datatype The method map(KeyValueMapper>) in the type KStream is not applicable for the arguments (new KeyValueMapper>(){})?

like image 319
Megamind Avatar asked Aug 30 '26 03:08

Megamind


1 Answers

From http://docs.confluent.io/current/streams/developer-guide.html#stateless-transformations:

The example changes the value type from byte[] to Integer. For String to CarClass is would be the same.

KStream<byte[], String> stream = ...;

// Java 8+ example, using lambda expressions
// Note how we change the key and the key type (similar to `selectKey`)
// as well as the value and the value type.
KStream<String, Integer> transformed = stream.map(
  (key, value) -> KeyValue.pair(value.toLowerCase(), value.length()));

// Java 7 example
KStream<String, Integer> transformed = stream.map(
  new KeyValueMapper<byte[], String, KeyValue<String, Integer>>() {
    @Override
    public KeyValue<String, Integer> apply(byte[] key, String value) {
      return new KeyValue<>(value.toLowerCase(), value.length());
    }
  });

However, if you want to only modify the value, I would recommend to use mapValues() instead of map().

like image 93
Matthias J. Sax Avatar answered Aug 31 '26 17:08

Matthias J. Sax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!