Search in sources :

Example 6 with SchemaParseConfiguration

use of com.linkedin.avroutil1.compatibility.SchemaParseConfiguration in project avro-util by linkedin.

the class Avro110Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = true;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    parser.setValidate(validateNames);
    parser.setValidateDefaults(validateDefaults);
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    Map<String, Schema> knownByFullName = parser.getTypes();
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    if (configUsed.validateDefaultValues()) {
        // dont trust avro, also run our own
        Avro110SchemaValidator validator = new Avro110SchemaValidator(configUsed, known);
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    // todo - depending on how https://issues.apache.org/jira/browse/AVRO-2742 is settled, may need to use our own validator here
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema)

Example 7 with SchemaParseConfiguration

use of com.linkedin.avroutil1.compatibility.SchemaParseConfiguration in project avro-util by linkedin.

the class Avro111Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = true;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    parser.setValidate(validateNames);
    parser.setValidateDefaults(validateDefaults);
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    Map<String, Schema> knownByFullName = parser.getTypes();
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    if (configUsed.validateDefaultValues()) {
        // dont trust avro, also run our own
        Avro111SchemaValidator validator = new Avro111SchemaValidator(configUsed, known);
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema)

Example 8 with SchemaParseConfiguration

use of com.linkedin.avroutil1.compatibility.SchemaParseConfiguration in project avro-util by linkedin.

the class Avro14Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    SchemaParseResult result = Avro14SchemaAccessUtil.parse(schemaJson, known);
    if (desiredConf != null && !desiredConf.equals(result.getConfigUsed())) {
        // avro 1.4 doesnt validate anything, so if user wants anything stricter we have to do it ourselves
        Schema schema = result.getMainSchema();
        Avro14SchemaValidator validator = new Avro14SchemaValidator(desiredConf, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(schema, validator);
        return new SchemaParseResult(result.getMainSchema(), result.getAllSchemas(), desiredConf);
    } else {
        return result;
    }
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) Schema(org.apache.avro.Schema)

Example 9 with SchemaParseConfiguration

use of com.linkedin.avroutil1.compatibility.SchemaParseConfiguration in project avro-util by linkedin.

the class Avro16Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = false;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    parser.setValidate(validateNames);
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    Map<String, Schema> knownByFullName = parser.getTypes();
    if (configUsed.validateDefaultValues()) {
        // avro 1.6 doesnt properly validate default values, so we have to do it ourselves
        Avro16SchemaValidator validator = new Avro16SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema)

Aggregations

Schema (org.apache.avro.Schema)9 SchemaParseConfiguration (com.linkedin.avroutil1.compatibility.SchemaParseConfiguration)8 SchemaParseResult (com.linkedin.avroutil1.compatibility.SchemaParseResult)8 HashMap (java.util.HashMap)7 DataSchema (com.linkedin.data.schema.DataSchema)1 Test (org.testng.annotations.Test)1