question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shading Avro in pulsar-client

See original GitHub issue

Describe the bug

Because we shade Avro in the pulsar-client, the ReflectData API to get a schema from a POJO fails to get the correct schema for generated Avro java classes

To Reproduce

Given an avro schema:

{
  "namespace": "example.avro",
  "type": "record",
  "name": "User",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "favorite_number",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "favorite_color",
      "type": "string"
    },
    {
      "name": "age",
      "type": "int",
      "default": 19
    }
  ]
}

Passing the generated java class from the above schema to the AvroSchema in pulsar-client produces the following result:

System.out.println(new String(org.apache.pulsar.client.impl.schema.AvroSchema.of(User.class).getSchemaInfo().getSchema()));

outputs:

{"type":"record","name":"User","namespace":"example.avro","fields":[{"name":"name","type":["null","string"],"default":null},{"name":"favorite_number","type":["null","int"],"default":null},{"name":"favorite_color","type":["null","string"],"default":null},{"name":"age","type":"int"}]}

which is incorrect while

System.out.println(ReflectData.get().getSchema(User.class));

outputs the correct schema that matches the original schema:

{"type":"record","name":"User","namespace":"example.avro","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":"string"},{"name":"age","type":"int","default":19}]}

Here is the line in Avro responsible for getting the schema from generated classes: https://github.com/apache/avro/blob/release-1.8.2/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java#L277

Here is a project I created that demonstrates this problem:

https://github.com/jerrypeng/TestAvro

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
rocketramancommented, Feb 25, 2020

@vzhikserg Thanks, that will work for me, though an underlying Avro upgrade probably would have been a good time to uncouple the Avro version from Pulsar to avoid this same problem in the future…

5reactions
rocketramancommented, Oct 3, 2019

I’d like to at least have the option to use the Java Pulsar client with an unshaded Avro. We use Avro extensively with Kafka, and while we migrate to Pulsar, its imperative we have compatibility at the Avro message level, which means using the same version of Avro.

Here is an issue we are running into because our messages use Avro 1.9.1 which supports the java.time APIs:

java.lang.RuntimeException: java.lang.NoSuchMethodException: java.time.Instant.<init>()
	at org.apache.pulsar.shade.org.apache.avro.specific.SpecificData.newInstance(SpecificData.java:353) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.specific.SpecificData.newRecord(SpecificData.java:369) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.reflect.ReflectData.newRecord(ReflectData.java:901) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:212) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.reflect.ReflectDatumReader.readField(ReflectDatumReader.java:302) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.reflect.ReflectDatumReader.readField(ReflectDatumReader.java:302) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.shade.org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.client.impl.schema.reader.AvroReader.read(AvroReader.java:52) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.client.impl.schema.StructSchema.decode(StructSchema.java:94) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.client.impl.MessageImpl.getValue(MessageImpl.java:270) ~[pulsar-client-2.4.1.jar:2.4.1]
	at org.apache.pulsar.client.impl.TopicMessageImpl.getValue(TopicMessageImpl.java:143) ~[pulsar-client-2.4.1.jar:2.4.1]
	[...]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pulsar Java client
Both pulsar-client and pulsar-client-admin are shaded packages and they shade dependencies independently. Consequently, the applications using both ...
Read more >
[pulsar] 04/04: [Schema] Fix parse BigDecimal (#14019)
getRecommendedSchema(Conversions.java:73) ~[pulsar-client-2.8.1.jar:2.8.1] at org.apache.pulsar.shade.org.apache.avro.reflect.ReflectData.
Read more >
Is there any way to use Pulsar with schema registry via ...
Currently, there isn't a way to specify the schema when creating a WS producer/consumer. The best option is to specify the AVRO schema...
Read more >
org.apache.pulsar : pulsar-client : 2.8.1 - Maven Central Repository ...
Pulsar Client Java. ... org.apache.pulsar:pulsar-client 2.8.1. content_copy. <?xml version="1.0" encoding="UTF-8"?> ...
Read more >
pulsar-client-2x-shaded » 2.4.2 - Maven Repository
Category/License Group / Artifact Version Up... Annotation Lib Apache 2.0 com.fasterxml.jackson.core » jackson‑annotations 2.9.9 2.1... JSON Lib Apache 2.0 com.fasterxml.jackson.core » jackson‑core 2.9.9 2.1... Compression BSD...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found