use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.
the class SchemaTest method testIterableSchema.
@Test
public void testIterableSchema() {
FieldType iterableType = FieldType.iterable(FieldType.STRING);
Schema schema = Schema.of(Field.of("f_iter", iterableType));
Field field = schema.getField("f_iter");
assertEquals("f_iter", field.getName());
assertEquals(iterableType, field.getType());
}
use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.
the class SchemaTest method testArrayOfRowSchema.
@Test
public void testArrayOfRowSchema() {
Schema nestedSchema = Schema.of(Field.of("f1_str", FieldType.STRING));
FieldType arrayType = FieldType.array(FieldType.row(nestedSchema));
Schema schema = Schema.of(Field.of("f_array", arrayType));
Field field = schema.getField("f_array");
assertEquals("f_array", field.getName());
assertEquals(arrayType, field.getType());
}
use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.
the class SchemaTest method testNestedIterableSchema.
@Test
public void testNestedIterableSchema() {
FieldType iterableType = FieldType.iterable(FieldType.iterable(FieldType.STRING));
Schema schema = Schema.of(Field.of("f_iter", iterableType));
Field field = schema.getField("f_iter");
assertEquals("f_iter", field.getName());
assertEquals(iterableType, field.getType());
}
use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.
the class BeamRowToStorageApiProtoTest method testDescriptorFromSchema.
@Test
public void testDescriptorFromSchema() {
DescriptorProto descriptor = BeamRowToStorageApiProto.descriptorSchemaFromBeamSchema(BASE_SCHEMA);
Map<String, Type> types = descriptor.getFieldList().stream().collect(Collectors.toMap(FieldDescriptorProto::getName, FieldDescriptorProto::getType));
Map<String, Type> expectedTypes = BASE_SCHEMA_PROTO.getFieldList().stream().collect(Collectors.toMap(FieldDescriptorProto::getName, FieldDescriptorProto::getType));
assertEquals(expectedTypes, types);
Map<String, String> nameMapping = BASE_SCHEMA.getFields().stream().collect(Collectors.toMap(f -> f.getName().toLowerCase(), Field::getName));
descriptor.getFieldList().forEach(p -> {
FieldType schemaFieldType = BASE_SCHEMA.getField(nameMapping.get(p.getName())).getType();
Label label = schemaFieldType.getTypeName().isCollectionType() ? Label.LABEL_REPEATED : schemaFieldType.getNullable() ? Label.LABEL_OPTIONAL : Label.LABEL_REQUIRED;
assertEquals(label, p.getLabel());
});
}
use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.
the class ConvertHelpers method getConvertPrimitive.
/**
* Returns a function to convert a Row into a primitive type. This only works when the row schema
* contains a single field, and that field is convertible to the primitive type.
*/
@SuppressWarnings("unchecked")
public static <OutputT> SerializableFunction<?, OutputT> getConvertPrimitive(FieldType fieldType, TypeDescriptor<?> outputTypeDescriptor, TypeConversionsFactory typeConversionsFactory) {
FieldType expectedFieldType = StaticSchemaInference.fieldFromType(outputTypeDescriptor, JavaFieldTypeSupplier.INSTANCE);
if (!expectedFieldType.equals(fieldType)) {
throw new IllegalArgumentException("Element argument type " + outputTypeDescriptor + " does not work with expected schema field type " + fieldType);
}
Type expectedInputType = typeConversionsFactory.createTypeConversion(false).convert(outputTypeDescriptor);
TypeDescriptor<?> outputType = outputTypeDescriptor;
if (outputType.getRawType().isPrimitive()) {
// A SerializableFunction can only return an Object type, so if the DoFn parameter is a
// primitive type, then box it for the return. The return type will be unboxed before being
// forwarded to the DoFn parameter.
outputType = TypeDescriptor.of(Primitives.wrap(outputType.getRawType()));
}
TypeDescription.Generic genericType = TypeDescription.Generic.Builder.parameterizedType(SerializableFunction.class, expectedInputType, outputType.getType()).build();
DynamicType.Builder<SerializableFunction> builder = (DynamicType.Builder<SerializableFunction>) new ByteBuddy().subclass(genericType);
try {
return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).method(ElementMatchers.named("apply")).intercept(new ConvertPrimitiveInstruction(outputType, typeConversionsFactory)).make().load(ReflectHelpers.findClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
Aggregations