Question: Best practices for schema-agnostic Pulsa...
# general
s
Question: Best practices for schema-agnostic Pulsar Functions? I want to write a generic Pulsar Function that can work with any input/output schema without hardcoding schema types. Current approach: • ✅ Input: Using
AUTO_CONSUME
- works great for reading any schema • ❌ Output: Need exact schema match, but
AUTO_PRODUCE
doesn't exist The challenge: To avoid static schemas, I need to get schema info from Pulsar Admin at runtime, but Pulsar Functions don't have access to
PulsarAdmin
(getting errors when trying). Questions: 1. What's the recommended pattern for schema-agnostic functions? 2. How do I discover output topic schema at runtime without Admin access? 3. Any alternatives to runtime schema discovery for generic functions? Goal: One function that works with multiple topic pairs having different schemas (Avro→Avro, JSON→JSON, Avro→JSON, etc.) without recompiling. Anyone solved this or know the best practices?
n
You may access the schema inside function via:
Copy code
context.getCurrentRecord().getSchema()
➕ 1
In addition, you can also declare the function to accept a
GenericRecord
, and then use
Object getField(String fieldName);
to access each field:
Copy code
public class DemoFunction implements Function<GenericObject, ??>  {

    public Record<GenericObject> process(GenericObject input, Context context) throws Exception {
       ...
       input.getField("FIELD_NAME");
       ...
    }
}
s
Can I use output message schema as "GenericRecord" ? It was failing for me stating auto consume doesnt work with producer.
r
l
n
You may also try the following:
Copy code
FunctionRecordBuilder recordBuilder =
                context.newOutputRecordBuilder(outputSchema).destinationTopic(targetTopic);
...
        recordBuilder.value(outputObject);
        return recordBuilder.build();
it returns a
Record<GenericObject>