Search in sources :

Example 1 with SchemaFieldInfo

use of com.hortonworks.registries.schemaregistry.SchemaFieldInfo in project registry by hortonworks.

the class StreamsSchemaProvider method generateFields.

@Override
public List<SchemaFieldInfo> generateFields(String schemaText) {
    // schema should be in json form.
    List<Schema.Field> fields;
    try {
        fields = new ObjectMapper().readValue(schemaText, new TypeReference<List<Schema.Field>>() {
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    List<SchemaFieldInfo> fieldInfos = new ArrayList<>(fields.size());
    for (Schema.Field field : fields) {
        // currently internal schema implementation does not have namespace.
        fieldInfos.add(new SchemaFieldInfo("__universal", field.getName(), field.getType().toString()));
    }
    return fieldInfos;
}
Also used : SchemaFieldInfo(com.hortonworks.registries.schemaregistry.SchemaFieldInfo) Schema(com.hortonworks.registries.common.Schema) ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with SchemaFieldInfo

use of com.hortonworks.registries.schemaregistry.SchemaFieldInfo in project registry by hortonworks.

the class AvroFieldsGenerator method parseField.

private void parseField(Schema.Field field, List<SchemaFieldInfo> schemaFieldInfos, Set<String> visitedRecords) {
    Schema schema = field.schema();
    Schema.Type type = schema.getType();
    String name = field.name();
    LOG.debug("Visiting field: [{}]", field);
    String namespace = null;
    try {
        namespace = schema.getNamespace();
    } catch (Exception e) {
    // ignore.
    }
    schemaFieldInfos.add(new SchemaFieldInfo(namespace, name, type.name()));
    // todo check whether fields should be mapped to the root schema.
    parseSchema(schema, schemaFieldInfos, visitedRecords);
}
Also used : SchemaFieldInfo(com.hortonworks.registries.schemaregistry.SchemaFieldInfo) Schema(org.apache.avro.Schema)

Example 3 with SchemaFieldInfo

use of com.hortonworks.registries.schemaregistry.SchemaFieldInfo in project registry by hortonworks.

the class AvroSchemaFieldsGeneratorTest method testComplexSchemaRead.

@Test
public void testComplexSchemaRead() throws Exception {
    AvroFieldsGenerator avroFieldsGenerator = new AvroFieldsGenerator();
    try (InputStream schemaStream = this.getClass().getResourceAsStream("/schema-1.avsc")) {
        Schema.Parser parser = new Schema.Parser();
        Schema schema = parser.parse(schemaStream);
        List<SchemaFieldInfo> schemaFieldInfos = avroFieldsGenerator.generateFields(schema);
        Assert.assertEquals(schemaFieldInfos.size(), 12);
    }
}
Also used : SchemaFieldInfo(com.hortonworks.registries.schemaregistry.SchemaFieldInfo) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Example 4 with SchemaFieldInfo

use of com.hortonworks.registries.schemaregistry.SchemaFieldInfo in project registry by hortonworks.

the class AvroSchemaProviderTest method testAvroSchemas.

@Test
public void testAvroSchemas() throws Exception {
    AvroSchemaProvider avroSchemaProvider = new AvroSchemaProvider();
    try (InputStream schemaStream = AvroSchemaProviderTest.class.getResourceAsStream("/avro/trucks.avsc")) {
        List<SchemaFieldInfo> schemaFieldInfos = avroSchemaProvider.generateFields(IOUtils.toString(schemaStream));
        LOG.info("generated schema fields [{}]", schemaFieldInfos);
        Assert.assertEquals(11, schemaFieldInfos.size());
    }
}
Also used : SchemaFieldInfo(com.hortonworks.registries.schemaregistry.SchemaFieldInfo) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 5 with SchemaFieldInfo

use of com.hortonworks.registries.schemaregistry.SchemaFieldInfo in project registry by hortonworks.

the class StreamsSchemaProviderTest method testStreamsSchema.

@Test
public void testStreamsSchema() throws Exception {
    StreamsSchemaProvider streamsSchemaProvider = new StreamsSchemaProvider();
    try (InputStream trucksSchemaStream = StreamsSchemaProviderTest.class.getResourceAsStream("/streams/trucks.stsc")) {
        List<SchemaFieldInfo> schemaFieldInfos = streamsSchemaProvider.generateFields(IOUtils.toString(trucksSchemaStream));
        LOG.info("schemaFieldInfos = " + schemaFieldInfos);
        Assert.assertEquals(11, schemaFieldInfos.size());
    }
}
Also used : SchemaFieldInfo(com.hortonworks.registries.schemaregistry.SchemaFieldInfo) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

SchemaFieldInfo (com.hortonworks.registries.schemaregistry.SchemaFieldInfo)5 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 Schema (org.apache.avro.Schema)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Schema (com.hortonworks.registries.common.Schema)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1