Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to implement the serialize method in a custom Kafka Serializer.
Kafka
public byte[] serialize(String topic, [1] data) { if (data == null) { return null; } return data.getBytes(StandardCharsets.UTF_8); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or Object as the data type causes compile errors.
Using byte[] as input is incorrect because serialize expects the original data type.
✗ Incorrect
The serialize method takes the data to serialize, which is a String in this example, so the parameter type should be String.
2fill in blank
mediumComplete the code to implement the deserialize method in a custom Kafka Deserializer.
Kafka
public String deserialize(String topic, [1] data) { if (data == null) { return null; } return new String(data, StandardCharsets.UTF_8); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using String as input causes errors because data is raw bytes.
Using int or Object as input is incorrect.
✗ Incorrect
The deserialize method receives the data as a byte array, so the parameter type must be byte[].
3fill in blank
hardFix the error in the custom Serializer class declaration.
Kafka
public class CustomSerializer implements Serializer<[1]> { // implementation }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using byte[] or Object as generic type causes mismatch errors.
Using Integer when data is String causes type errors.
✗ Incorrect
The Serializer interface should be parameterized with the data type you want to serialize, here String.
4fill in blank
hardFill both blanks to complete the custom Deserializer class declaration and method signature.
Kafka
public class CustomDeserializer implements Deserializer<[1]> { @Override public [2] deserialize(String topic, byte[] data) { if (data == null) return null; return new String(data, StandardCharsets.UTF_8); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using byte[] as generic type or return type causes errors.
Mixing Object and String types causes type mismatch.
✗ Incorrect
The Deserializer generic type and the deserialize method return type must both be String to match the data type.
5fill in blank
hardFill all three blanks to complete the custom SerDes class with serializer, deserializer, and configure method.
Kafka
public class CustomSerDes implements Serializer<[1]>, Deserializer<[2]> { @Override public byte[] serialize(String topic, [3] data) { if (data == null) return null; return data.getBytes(StandardCharsets.UTF_8); } @Override public [2] deserialize(String topic, byte[] data) { if (data == null) return null; return new String(data, StandardCharsets.UTF_8); } @Override public void configure(Map<String, ?> configs, boolean isKey) { // configuration code if needed } @Override public void close() { // cleanup code if needed } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using byte[] or Object as generic or parameter types causes type errors.
Mismatch between generic types and method parameter/return types.
✗ Incorrect
The SerDes class handles String data type for both serializer and deserializer, so all generic and parameter types are String.