public static Type convert(Schema schema) { return AvroSchemaVisitor.visit(schema, new SchemaToType(schema)); }
private int getKeyId(Schema schema) { if (schema.getObjectProp(AvroSchemaUtil.KEY_ID_PROP) != null) { return AvroSchemaUtil.getKeyId(schema); } else { return allocateId(); } }
@Override public Type record(Schema record, List<String> names, List<Type> fieldTypes) { List<Schema.Field> fields = record.getFields(); List<Types.NestedField> newFields = Lists.newArrayListWithExpectedSize(fields.size()); if (root == record) { this.nextId = 0; } for (int i = 0; i < fields.size(); i += 1) { Schema.Field field = fields.get(i); Type fieldType = fieldTypes.get(i); int fieldId = getId(field); if (AvroSchemaUtil.isOptionSchema(field.schema())) { newFields.add(Types.NestedField.optional(fieldId, field.name(), fieldType)); } else { newFields.add(Types.NestedField.required(fieldId, field.name(), fieldType)); } } return Types.StructType.of(newFields); }
@Override public Type map(Schema map, Type valueType) { Schema valueSchema = map.getValueType(); int keyId = getKeyId(map); int valueId = getValueId(map); if (AvroSchemaUtil.isOptionSchema(valueSchema)) { return Types.MapType.ofOptional(keyId, valueId, Types.StringType.get(), valueType); } else { return Types.MapType.ofRequired(keyId, valueId, Types.StringType.get(), valueType); } }
@Override public Type array(Schema array, Type elementType) { if (array.getLogicalType() instanceof LogicalMap) { // map stored as an array Schema keyValueSchema = array.getElementType(); Preconditions.checkArgument(AvroSchemaUtil.isKeyValueSchema(keyValueSchema), "Invalid key-value pair schema: {}", keyValueSchema); Types.StructType keyValueType = elementType.asStructType(); Types.NestedField keyField = keyValueType.field("key"); Types.NestedField valueField = keyValueType.field("value"); if (keyValueType.field("value").isOptional()) { return Types.MapType.ofOptional( keyField.fieldId(), valueField.fieldId(), keyField.type(), valueField.type()); } else { return Types.MapType.ofRequired( keyField.fieldId(), valueField.fieldId(), keyField.type(), valueField.type()); } } else { // normal array Schema elementSchema = array.getElementType(); int id = getElementId(array); if (AvroSchemaUtil.isOptionSchema(elementSchema)) { return Types.ListType.ofOptional(id, elementType); } else { return Types.ListType.ofRequired(id, elementType); } } }
private int getElementId(Schema schema) { if (schema.getObjectProp(AvroSchemaUtil.ELEMENT_ID_PROP) != null) { return AvroSchemaUtil.getElementId(schema); } else { return allocateId(); } }
private int getValueId(Schema schema) { if (schema.getObjectProp(AvroSchemaUtil.VALUE_ID_PROP) != null) { return AvroSchemaUtil.getValueId(schema); } else { return allocateId(); } }
private int getId(Schema.Field field) { if (field.getObjectProp(AvroSchemaUtil.FIELD_ID_PROP) != null) { return AvroSchemaUtil.getFieldId(field); } else { return allocateId(); } }