Search in sources :

Example 1 with SchemaParseConfiguration

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

the class Avro15Adapter 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);
    if (validateDefaults) {
        // avro 1.5 doesnt properly validate default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    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) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Example 2 with SchemaParseConfiguration

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

the class Avro18Adapter 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);
    // avro 1.8 can (optionally) validate defaults - so even if the user asked for that we have no further work to do
    Map<String, Schema> knownByFullName = parser.getTypes();
    return new SchemaParseResult(mainSchema, knownByFullName, new SchemaParseConfiguration(validateNames, validateDefaults));
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema)

Example 3 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);
    if (validateDefaults) {
        // avro 1.6 doesnt properly validate default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    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) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Example 4 with SchemaParseConfiguration

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

the class Avro19Adapter 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();
    // 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, new SchemaParseConfiguration(validateNames, validateDefaults));
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema)

Example 5 with SchemaParseConfiguration

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

the class Avro17Adapter 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();
    }
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    parser.setValidate(validateNames);
    // must check the method exists before trying to call it
    if (setValidateDefaultsMethod != null) {
        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);
    if (validateDefaults && setValidateDefaultsMethod == null) {
        // older avro 1.7 doesnt support validating default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    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) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Aggregations

SchemaParseResult (com.linkedin.avroutil1.compatibility.SchemaParseResult)8 Schema (org.apache.avro.Schema)8 SchemaParseConfiguration (com.linkedin.avroutil1.compatibility.SchemaParseConfiguration)7 HashMap (java.util.HashMap)7 SchemaValidator (com.linkedin.avroutil1.compatibility.SchemaValidator)4