@Override public String getResultantSchema(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { AvroSchemaResolver avroSchemaResolver = new AvroSchemaResolver(getSchemaVersionRetriever()); return avroSchemaResolver.resolveSchema(schemaText); }
private Map<String, Schema> traverseIncludedSchemaTypes(String schemaText, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { List<SchemaVersionKey> includedSchemaVersions = getIncludedSchemaVersions(schemaText); if (includedSchemaVersions == null || includedSchemaVersions.isEmpty()) { return Collections.emptyMap(); } Map<String, Schema> schemaTypes = new HashMap<>(); for (SchemaVersionKey schemaVersionKey : includedSchemaVersions) { Map<String, Schema> collectedSchemas = collectSchemaTypes(schemaVersionKey, schemaParsingStates); if (collectedSchemas != null) { schemaTypes.putAll(collectedSchemas); } } return schemaTypes; }
public String resolveSchema(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); return getResultantSchema(schemaText, schemaParsingStates); }
private String getResultantSchema(String schemaText, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { Map<String, Schema> complexTypes = traverseIncludedSchemaTypes(schemaText, schemaParsingStates); Schema.Parser parser = new Schema.Parser(); parser.addTypes(complexTypes); Schema schema = parser.parse(schemaText); Set<String> visitingTypes = new HashSet<>(); Schema updatedSchema = handleUnionFieldsWithNull(schema, visitingTypes); return (schema == updatedSchema && complexTypes.isEmpty()) ? schemaText : updatedSchema.toString(); }
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws SchemaNotFoundException, InvalidSchemaException { String schemaName = schemaVersionKey.getSchemaName(); SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING); // if it is already parsed then the respective schema types would have been already collected. if (SchemaParsingState.PARSED == schemaParsingState) { return null; } // if it is in parsing state earlier and it is visted again then ther eis circular dependency!! if (SchemaParsingState.PARSING == schemaParsingState) { throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]"); } // this schema is not yet parsed till now if (schemaParsingState == null) { Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates)); Map<String, Schema> complexTypes = new HashMap<>(); collectComplexTypes(schema, complexTypes); schemaParsingStates.put(schemaName, SchemaParsingState.PARSED); return complexTypes; } throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!"); }
@Override protected Schema getParsedSchema(SchemaVersionKey schemaVersionKey) throws InvalidSchemaException, SchemaNotFoundException { return new Schema.Parser().parse(avroSchemaResolver.resolveSchema(schemaVersionKey)); }
@Override public void doInit(Map<String, ?> config) { super.doInit(config); SchemaVersionRetriever schemaVersionRetriever = createSchemaVersionRetriever(); avroSchemaResolver = new AvroSchemaResolver(schemaVersionRetriever); useSpecificAvroReader = (boolean) getValue(config, SPECIFIC_AVRO_READER, false); }
private void collectComplexTypes(Schema schema, Map<String, Schema> complexTypes) { switch (schema.getType()) { case RECORD: complexTypes.put(schema.getFullName(), schema); List<Schema.Field> fields = schema.getFields(); for (Schema.Field field : fields) { Schema fieldSchema = field.schema(); collectComplexTypes(fieldSchema, complexTypes); } break; case ARRAY: complexTypes.put(schema.getFullName(), schema); collectComplexTypes(schema.getElementType(), complexTypes); break; case UNION: complexTypes.put(schema.getFullName(), schema); List<Schema> unionSchemas = schema.getTypes(); for (Schema schemaEntry : unionSchemas) { collectComplexTypes(schemaEntry, complexTypes); } break; case MAP: complexTypes.put(schema.getFullName(), schema); collectComplexTypes(schema.getValueType(), complexTypes); break; default: } }
Schema updatedFieldSchema = handleUnionFieldsWithNull(fieldSchema, visitingTypes); if (fieldSchema != updatedFieldSchema) { hasUnionType = true;
private String getResultantSchema(String schemaText, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { Map<String, Schema> complexTypes = traverseIncludedSchemaTypes(schemaText, schemaParsingStates); Schema.Parser parser = new Schema.Parser(); parser.addTypes(complexTypes); Schema schema = parser.parse(schemaText); Set<String> visitingTypes = new HashSet<>(); Schema updatedSchema = handleUnionFieldsWithNull(schema, visitingTypes); return (schema == updatedSchema && complexTypes.isEmpty()) ? schemaText : updatedSchema.toString(); }
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws SchemaNotFoundException, InvalidSchemaException { String schemaName = schemaVersionKey.getSchemaName(); SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING); // if it is already parsed then the respective schema types would have been already collected. if (SchemaParsingState.PARSED == schemaParsingState) { return null; } // if it is in parsing state earlier and it is visted again then ther eis circular dependency!! if (SchemaParsingState.PARSING == schemaParsingState) { throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]"); } // this schema is not yet parsed till now if (schemaParsingState == null) { Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates)); Map<String, Schema> complexTypes = new HashMap<>(); collectComplexTypes(schema, complexTypes); schemaParsingStates.put(schemaName, SchemaParsingState.PARSED); return complexTypes; } throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!"); }
private void collectComplexTypes(Schema schema, Map<String, Schema> complexTypes) { switch (schema.getType()) { case RECORD: complexTypes.put(schema.getFullName(), schema); List<Schema.Field> fields = schema.getFields(); for (Schema.Field field : fields) { Schema fieldSchema = field.schema(); collectComplexTypes(fieldSchema, complexTypes); } break; case ARRAY: complexTypes.put(schema.getFullName(), schema); collectComplexTypes(schema.getElementType(), complexTypes); break; case UNION: complexTypes.put(schema.getFullName(), schema); List<Schema> unionSchemas = schema.getTypes(); for (Schema schemaEntry : unionSchemas) { collectComplexTypes(schemaEntry, complexTypes); } break; case MAP: complexTypes.put(schema.getFullName(), schema); collectComplexTypes(schema.getValueType(), complexTypes); break; default: } }
Schema updatedFieldSchema = handleUnionFieldsWithNull(fieldSchema, visitingTypes); if (fieldSchema != updatedFieldSchema) { hasUnionType = true;
@Override public String getResultantSchema(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { AvroSchemaResolver avroSchemaResolver = new AvroSchemaResolver(getSchemaVersionRetriever()); return avroSchemaResolver.resolveSchema(schemaText); }
private Map<String, Schema> traverseIncludedSchemaTypes(String schemaText, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { List<SchemaVersionKey> includedSchemaVersions = getIncludedSchemaVersions(schemaText); if (includedSchemaVersions == null || includedSchemaVersions.isEmpty()) { return Collections.emptyMap(); } Map<String, Schema> schemaTypes = new HashMap<>(); for (SchemaVersionKey schemaVersionKey : includedSchemaVersions) { Map<String, Schema> collectedSchemas = collectSchemaTypes(schemaVersionKey, schemaParsingStates); if (collectedSchemas != null) { schemaTypes.putAll(collectedSchemas); } } return schemaTypes; }
public String resolveSchema(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); return getResultantSchema(schemaText, schemaParsingStates); }
@Override public String resolveSchema(SchemaVersionKey schemaVersionKey) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); schemaParsingStates.put(schemaVersionKey.getSchemaName(), SchemaParsingState.PARSING); return getResultantSchema(schemaVersionKey, schemaParsingStates); }
@Override public String resolveSchema(SchemaVersionKey schemaVersionKey) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); schemaParsingStates.put(schemaVersionKey.getSchemaName(), SchemaParsingState.PARSING); return getResultantSchema(schemaVersionKey, schemaParsingStates); }
private String getResultantSchema(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { String schemaText = schemaVersionRetriever.retrieveSchemaVersion(schemaVersionKey).getSchemaText(); return getResultantSchema(schemaText, schemaParsingStates); }
private String getResultantSchema(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { String schemaText = schemaVersionRetriever.retrieveSchemaVersion(schemaVersionKey).getSchemaText(); return getResultantSchema(schemaText, schemaParsingStates); }