Search in sources :

Example 1 with AvroSchemaField

use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.

the class AvscSchemaWriter method emitRecordFields.

protected void emitRecordFields(AvroRecordSchema schema, AvscWriterContext context, AvscWriterConfig config, JsonObjectBuilder output) {
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    List<AvroSchemaField> fields = schema.getFields();
    for (AvroSchemaField field : fields) {
        JsonObjectBuilder fieldBuilder = Json.createObjectBuilder();
        fieldBuilder.add("name", field.getName());
        if (field.hasDoc()) {
            fieldBuilder.add("doc", field.getDoc());
        }
        AvroSchema fieldSchema = field.getSchema();
        fieldBuilder.add("type", writeSchema(fieldSchema, context, config));
        if (field.hasDefaultValue()) {
            AvroLiteral defaultValue = field.getDefaultValue();
            JsonValue defaultValueLiteral = writeDefaultValue(fieldSchema, defaultValue);
            fieldBuilder.add("default", defaultValueLiteral);
        }
        // TODO - order
        // TODO - aliases
        arrayBuilder.add(fieldBuilder);
    }
    output.add("fields", arrayBuilder);
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) JsonValue(javax.json.JsonValue) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) AvroLiteral(com.linkedin.avroutil1.model.AvroLiteral)

Example 2 with AvroSchemaField

use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.

the class AvscParserTest method testSimpleParse.

@Test
public void testSimpleParse() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroSchema schema = result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    Assert.assertEquals(schema.type(), AvroType.RECORD);
    AvroRecordSchema recordSchema = (AvroRecordSchema) schema;
    Assert.assertEquals(recordSchema.getFullName(), "com.acme.TestRecord");
    List<AvroSchemaField> fields = recordSchema.getFields();
    Assert.assertNotNull(fields);
    Assert.assertEquals(fields.size(), 11);
    Assert.assertEquals(fields.get(0).getPosition(), 0);
    Assert.assertEquals(fields.get(0).getName(), "booleanField");
    Assert.assertEquals(fields.get(0).getSchema().type(), AvroType.BOOLEAN);
    Assert.assertEquals(fields.get(1).getPosition(), 1);
    Assert.assertEquals(fields.get(1).getName(), "intField");
    Assert.assertEquals(fields.get(1).getSchema().type(), AvroType.INT);
    Assert.assertEquals(fields.get(2).getPosition(), 2);
    Assert.assertEquals(fields.get(2).getName(), "longField");
    Assert.assertEquals(fields.get(2).getSchema().type(), AvroType.LONG);
    Assert.assertEquals(fields.get(3).getPosition(), 3);
    Assert.assertEquals(fields.get(3).getName(), "floatField");
    Assert.assertEquals(fields.get(3).getSchema().type(), AvroType.FLOAT);
    Assert.assertEquals(fields.get(4).getPosition(), 4);
    Assert.assertEquals(fields.get(4).getName(), "doubleField");
    Assert.assertEquals(fields.get(4).getSchema().type(), AvroType.DOUBLE);
    Assert.assertEquals(fields.get(5).getPosition(), 5);
    Assert.assertEquals(fields.get(5).getName(), "bytesField");
    Assert.assertEquals(fields.get(5).getSchema().type(), AvroType.BYTES);
    Assert.assertEquals(fields.get(6).getPosition(), 6);
    Assert.assertEquals(fields.get(6).getName(), "stringField");
    Assert.assertEquals(fields.get(6).getSchema().type(), AvroType.STRING);
    Assert.assertEquals(fields.get(7).getPosition(), 7);
    Assert.assertEquals(fields.get(7).getName(), "enumField");
    Assert.assertEquals(fields.get(7).getSchema().type(), AvroType.ENUM);
    AvroEnumSchema simpleEnumSchema = (AvroEnumSchema) fields.get(7).getSchema();
    Assert.assertEquals(simpleEnumSchema.getFullName(), "innerNamespace.SimpleEnum");
    Assert.assertEquals(simpleEnumSchema.getSymbols(), Arrays.asList("A", "B", "C"));
    Assert.assertEquals(fields.get(8).getPosition(), 8);
    Assert.assertEquals(fields.get(8).getName(), "fixedField");
    Assert.assertEquals(fields.get(8).getSchema().type(), AvroType.FIXED);
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getFullName(), "com.acme.SimpleFixed");
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getSize(), 7);
    Assert.assertEquals(fields.get(9).getPosition(), 9);
    Assert.assertEquals(fields.get(9).getName(), "strArrayField");
    Assert.assertEquals(fields.get(9).getSchema().type(), AvroType.ARRAY);
    Assert.assertEquals(((AvroArraySchema) fields.get(9).getSchema()).getValueSchema().type(), AvroType.NULL);
    Assert.assertEquals(fields.get(10).getPosition(), 10);
    Assert.assertEquals(fields.get(10).getName(), "enumMapField");
    Assert.assertEquals(fields.get(10).getSchema().type(), AvroType.MAP);
    AvroSchema mapValueSchema = ((AvroMapSchema) fields.get(10).getSchema()).getValueSchema();
    Assert.assertSame(mapValueSchema, simpleEnumSchema);
}
Also used : AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 3 with AvroSchemaField

use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.

the class AvscParserTest method testParsingDefaultValues.

@Test
public void testParsingDefaultValues() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithDefaultValues.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    for (AvroSchemaField field : schema.getFields()) {
        Assert.assertNotNull(field.getDefaultValue(), "field " + field.getName() + " has a null default");
    }
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 4 with AvroSchemaField

use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.

the class AvscParserTest method testParsingHorribleDefaultValues.

@Test
public void testParsingHorribleDefaultValues() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithHorribleDefaultValues.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    for (AvroSchemaField field : schema.getFields()) {
        List<AvscIssue> issuesWithField = result.getIssues(field);
        Assert.assertFalse(issuesWithField.isEmpty(), "field " + field.getName() + " has no issues?!");
        Assert.assertFalse(issuesWithField.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
        Assert.assertNull(field.getDefaultValue());
    }
}
Also used : Arrays(java.util.Arrays) TestUtil(com.linkedin.avroutil1.testcommon.TestUtil) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) Test(org.testng.annotations.Test) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) GenericData(org.apache.avro.generic.GenericData) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) Assert(org.testng.Assert) JsonPropertiesContainer(com.linkedin.avroutil1.model.JsonPropertiesContainer) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) Schema(org.apache.avro.Schema) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) IOException(java.io.IOException) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) Collectors(java.util.stream.Collectors) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation) List(java.util.List) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) AvroType(com.linkedin.avroutil1.model.AvroType) Collections(java.util.Collections) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 5 with AvroSchemaField

use of com.linkedin.avroutil1.model.AvroSchemaField in project avro-util by linkedin.

the class AvscParserTest method testParsingLogicalTypes.

@Test
public void testParsingLogicalTypes() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithLogicalTypes.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    for (AvroSchemaField field : schema.getFields()) {
        switch(field.getName()) {
            case "bytesDecimalField":
                Assert.assertEquals(field.getSchema().type(), AvroType.BYTES);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
                Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getScale(), 2);
                Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getPrecision(), 4);
                break;
            case "fixedDecimalField":
                Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
                break;
            case "fixedDurationField":
                Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DURATION);
                break;
            case "stringUUIDField":
                Assert.assertEquals(field.getSchema().type(), AvroType.STRING);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.UUID);
                break;
            case "intDateField":
                Assert.assertEquals(field.getSchema().type(), AvroType.INT);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DATE);
                break;
            case "intTimeMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.INT);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MILLIS);
                break;
            case "longTimeMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MICROS);
                break;
            case "longTimestampMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MILLIS);
                break;
            case "longTimestampMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MICROS);
                break;
            case "longLocalTimestampMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MILLIS);
                break;
            case "longLocalTimestampMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MICROS);
                break;
            default:
                break;
        }
    }
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Aggregations

AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)11 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)10 Test (org.testng.annotations.Test)8 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)4 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)3 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)2 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)2 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)2 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)2 AvroType (com.linkedin.avroutil1.model.AvroType)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)2 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonValue (javax.json.JsonValue)2 AvroJavaStringRepresentation (com.linkedin.avroutil1.model.AvroJavaStringRepresentation)1