Home > Blockchain >  Convert JSON to Avro in Jmeter
Convert JSON to Avro in Jmeter

Time:02-07

I need to convert a JSON message to Avro so that I can send it to Kafka.

I have the sample JSON request and also the schema from an avsc file for Avro.

Any ideas how I can do this please?

Thanks

CodePudding user response:

You will need:

  1. To download Avro Java libraries and put them (along with the dependencies) to JMeter Classpath

  2. To restart JMeter to pick up the libraries

  3. To add a suitable JSR223 Test Element with the relevant Groovy code to perform the conversion, a piece of example code is below:

    String schemaJson = 'your schema here'
    String genericRecordStr = 'your json payload here'
    def schemaParser = new org.apache.avro.Schema.Parser()
    def schema = schemaParser.parse(schemaJson)
    def decoderFactory = new org.apache.avro.io.DecoderFactory()
    def decoder = decoderFactory.jsonDecoder(schema, genericRecordStr)
    def reader = new org.apache.avro.generic.GenericDatumReader()(schema)
    def record = reader.read(null, decoder)
    
  4. To send the message to Kafka, out of box JMeter doesn't support Kafka a couple of options are described in the Apache Kafka - How to Load Test with JMeter article

CodePudding user response:

Assuming by "Avro", you are using the Schema Registry. If you're able to install the Kafka REST Proxy, you could send JSON events to it, then the proxy can be configured to integrate with the Registry to send Avro objects to the brokers

Or you can write a custom sampler, as linked in the other answer, but you'd swap the StringSerializer for an Avro Serializer implementation (you'd need to add that to the Jmeter classpath)

  •  Tags:  
  • Related