eolivelli commented on pull request #9614:
URL: https://github.com/apache/pulsar/pull/9614#issuecomment-784869893


   @codelipenghui @congbobo184 
   
    When you are inside a Sink<GenericRecord> you have this situation and you 
can access all of the schema info from the `Record`
   
   ```
   Record<GenericRecord> record = ....;
   GenericRecord value = record.getValue();
   SchemaInfo schemaInfo = record.getSchema().getSchemaInfo();
   SchemaType type = schemaInfo.getType();
   value.getFields().forEach( f -> {
       org.apache.avro.Schema.Field avroField = 
f.unwrap(org.apache.avro.Schema.Field.class);
       org.apache.avro.Schema fieldSchema = avroField.schema();
       log.info("Field {} unwrapped as {} schema {}", f, avroField, 
fieldSchema);
   });
   
   ```
   
   Unfortunately GenericRecord is used both to receive and to send values 
from/to Pulsar.
   
   We cannot easily add `GenericRecord#getSchema`
   We already have `GenericRecord#getSchemaVersion` and this is already akward 
for people that use GenericRecord to produce messages.
   
   Do you think we should add something like
   
   ```
   interface GenericRecord {
        /**
          * Access Schema information from the underlying Schema implementation.
          * This API is useful only while receiving messages.
          * Returns null in case that the requested Schema type is not available
         */
        default <T> T unwrapSchema(Class<T> schemaType) {
            return null;
        }
        /**
          * Access Schema information.
          * This API is useful only while receiving messages.
          * Returns null in case that the requested information is not 
available.
         */
        default Optional<SchemaInfo> getSchemaInfo() {
            return Options.empty();
        }
   }
   ```
   
   I will also like it, as it will help users that are not inside a Sink and 
they are receiving messages with the `Consumer` or `Reader` API.
   
   In that case I would make `getSchemaVersion()` an optional method by adding 
a default implementation and adding a note on the JavaDoc that the method is 
intended to be used only while receiving messages.
   
   


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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to