2

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>(){})?

1
  • Can you share your code and topology configuration? Basically, map should be applicable for KeyValueMapper, but without your code I can not find the issue. Commented Sep 26, 2023 at 13:07

1 Answer 1

9

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.