Search in sources :

Example 21 with FieldType

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());
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 22 with FieldType

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());
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 23 with FieldType

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());
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Schema.toSchema(org.apache.beam.sdk.schemas.Schema.toSchema) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 24 with FieldType

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());
    });
}
Also used : Type(com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type) Descriptor(com.google.protobuf.Descriptors.Descriptor) DynamicMessage(com.google.protobuf.DynamicMessage) LocalDateTime(java.time.LocalDateTime) RunWith(org.junit.runner.RunWith) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) BigDecimal(java.math.BigDecimal) SqlTypes(org.apache.beam.sdk.schemas.logicaltypes.SqlTypes) Map(java.util.Map) LocalTime(java.time.LocalTime) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) Row(org.apache.beam.sdk.values.Row) Field(org.apache.beam.sdk.schemas.Schema.Field) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) JUnit4(org.junit.runners.JUnit4) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) StandardCharsets(java.nio.charset.StandardCharsets) ByteString(com.google.protobuf.ByteString) LocalDate(java.time.LocalDate) Instant(org.joda.time.Instant) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Label(com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label) Functions(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Functions) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Assert.assertEquals(org.junit.Assert.assertEquals) Type(com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Label(com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) ByteString(com.google.protobuf.ByteString) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 25 with FieldType

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);
    }
}
Also used : SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) InvocationTargetException(java.lang.reflect.InvocationTargetException) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Type(java.lang.reflect.Type) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper)

Aggregations

FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)58 Schema (org.apache.beam.sdk.schemas.Schema)24 Field (org.apache.beam.sdk.schemas.Schema.Field)20 Row (org.apache.beam.sdk.values.Row)15 Test (org.junit.Test)15 Map (java.util.Map)10 List (java.util.List)9 ArrayList (java.util.ArrayList)7 Nullable (org.checkerframework.checker.nullness.qual.Nullable)7 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)6 BigDecimal (java.math.BigDecimal)6 Schema.toSchema (org.apache.beam.sdk.schemas.Schema.toSchema)6 ImmutableMap (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)6 Collectors (java.util.stream.Collectors)5 EnumerationType (org.apache.beam.sdk.schemas.logicaltypes.EnumerationType)5 LocalDateTime (java.time.LocalDateTime)4 LocalTime (java.time.LocalTime)4 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)3 TableSchema (com.google.api.services.bigquery.model.TableSchema)3 AutoValue (com.google.auto.value.AutoValue)3