Search code examples
javaavroconfluent-schema-registry

How to create schema from a Map and register to Schema Registry


Is there a way to create Schema from Map.

I have a map with key-value pairs and want to create Schema from this.

I have seen the org.apache.avro.Schema class(from avro-tools-1.8.2.jar) and there is APIs like below to read JSON and create Schema from it.

public Schema parse(File file) throws IOException {
      return parse(FACTORY.createJsonParser(file));
    }

public Schema parse(InputStream in) throws IOException {
      return parse(FACTORY.createJsonParser(in).disable(
              JsonParser.Feature.AUTO_CLOSE_SOURCE));
    }

public Schema parse(String s, String... more) {
      StringBuilder b = new StringBuilder(s);
      for (String part : more)
        b.append(part);
      return parse(b.toString());
    }

public Schema parse(String s) {
      try {
        return parse(FACTORY.createJsonParser(new StringReader(s)));
      } catch (IOException e) {
        throw new SchemaParseException(e);
      }
    }

Any pointer around how to create Schema from Map? After creating schema I will registry this to Confluent Schema Registry.


Solution

  • I'm not sure about parsing a Map<String, ?> but you can build the schema in code rather than parsing JSON.

    Example

    final Schema valueType = SchemaBuilder.builder().stringType();
    
    Schema mapSchema = SchemaBuilder.map().values(valueType);
    System.out.println(mapSchema);
    // {"type":"map","values":"string"}
    
    Schema recordSchemaWithMap = SchemaBuilder.builder("my.namespace.avro").record("MapData")
            .fields()
            .name("attributes").type(Schema.createMap(valueType)).noDefault()
            .endRecord();
    
    System.out.println(recordSchemaWithMap);
    // {"type":"record","name":"MapData","namespace":"my.namespace.avro","fields":[{"name":"attributes","type":{"type":"map","values":"string"}}]}
    

    This could probably be extended if you would loop over some Map.Entry values and build up the Schema object

    Note: all maps contain string-type keys