nicoloboschi commented on code in PR #20654:
URL: https://github.com/apache/pulsar/pull/20654#discussion_r1243869587
##########
pulsar-client-tools/src/main/java/org/apache/pulsar/client/cli/CmdProduce.java:
##########
@@ -189,6 +208,29 @@ private List<byte[]> generateMessageBodies(List<String>
stringMessages, List<Str
return messageBodies;
}
+ private static byte[] jsonToAvro(String m, org.apache.avro.Schema
avroSchema){
+ try {
+ GenericDatumReader<Object> reader = new
GenericDatumReader<>(avroSchema);
+ JsonDecoder jsonDecoder =
DecoderFactory.get().jsonDecoder(avroSchema, m);
+ GenericDatumWriter<Object> writer = new
GenericDatumWriter<>(avroSchema);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Encoder e = EncoderFactory.get().binaryEncoder(out, null);
+ Object datum = null;
+ while (true) {
+ try {
+ datum = reader.read(datum, jsonDecoder);
+ } catch (EOFException eofException) {
+ break;
+ }
+ writer.write(datum, e);
+ e.flush();
+ }
+ return out.toByteArray();
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot convert " + m + " to AVRO " +
e.getMessage(), e);
Review Comment:
since we use JSON decoder, can you improve the error message saying that you
need to pass a valid JSON ?
##########
pulsar-client-tools/src/test/java/org/apache/pulsar/client/cli/TestCmdProduce.java:
##########
@@ -72,4 +78,26 @@ public void testBuildSchema() {
assertEquals(SchemaType.JSON,
composite2.getKeySchema().getSchemaInfo().getType());
assertEquals(SchemaType.AVRO,
composite2.getValueSchema().getSchemaInfo().getType());
}
+
+ @Test
+ public void generateAvroMessageBodies() throws Exception {
Review Comment:
can you add an example for keyvalue ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]