Search in sources :

Example 51 with FieldType

use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.

the class RowCoderTest method testArrayOfRow.

@Test
public void testArrayOfRow() throws Exception {
    Schema nestedSchema = Schema.builder().addInt32Field("f1_int").addStringField("f1_str").build();
    FieldType collectionElementType = FieldType.row(nestedSchema);
    Schema schema = Schema.builder().addArrayField("f_array", collectionElementType).build();
    Row row = Row.withSchema(schema).addArray(Row.withSchema(nestedSchema).addValues(1, "one").build(), Row.withSchema(nestedSchema).addValues(2, "two").build(), Row.withSchema(nestedSchema).addValues(3, "three").build()).build();
    CoderProperties.coderDecodeEncodeEqual(RowCoder.of(schema), row);
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 52 with FieldType

use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.

the class RowCoderTest method testIterableOfRow.

@Test
public void testIterableOfRow() throws Exception {
    Schema nestedSchema = Schema.builder().addInt32Field("f1_int").addStringField("f1_str").build();
    FieldType collectionElementType = FieldType.row(nestedSchema);
    Schema schema = Schema.builder().addIterableField("f_iter", collectionElementType).build();
    Row row = Row.withSchema(schema).addIterable(ImmutableList.of(Row.withSchema(nestedSchema).addValues(1, "one").build(), Row.withSchema(nestedSchema).addValues(2, "two").build(), Row.withSchema(nestedSchema).addValues(3, "three").build())).build();
    CoderProperties.coderDecodeEncodeEqual(RowCoder.of(schema), row);
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 53 with FieldType

use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.

the class RowCoderTest method testConsistentWithEqualsArrayOfBytes.

@Test
public void testConsistentWithEqualsArrayOfBytes() throws Exception {
    FieldType fieldType = FieldType.array(FieldType.BYTES);
    Schema schema = Schema.of(Schema.Field.of("f1", fieldType));
    RowCoder coder = RowCoder.of(schema);
    List<byte[]> list1 = Collections.singletonList(new byte[] { 1, 2, 3, 4 });
    Row row1 = Row.withSchema(schema).addValue(list1).build();
    List<byte[]> list2 = Collections.singletonList(new byte[] { 1, 2, 3, 4 });
    Row row2 = Row.withSchema(schema).addValue(list2).build();
    Assume.assumeTrue(coder.consistentWithEquals());
    CoderProperties.coderConsistentWithEquals(coder, row1, row2);
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 54 with FieldType

use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.

the class RowCoderTest method testIterableOfIterable.

@Test
public void testIterableOfIterable() throws Exception {
    FieldType iterableType = FieldType.iterable(FieldType.array(FieldType.INT32));
    Schema schema = Schema.builder().addField("f_iter", iterableType).build();
    Row row = Row.withSchema(schema).addIterable(ImmutableList.of(Lists.newArrayList(1, 2, 3, 4), Lists.newArrayList(5, 6, 7, 8), Lists.newArrayList(9, 10, 11, 12))).build();
    CoderProperties.coderDecodeEncodeEqual(RowCoder.of(schema), row);
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Test(org.junit.Test)

Example 55 with FieldType

use of org.apache.beam.sdk.schemas.Schema.FieldType in project beam by apache.

the class Row method getBaseValue.

/**
 * Returns the base type for this field. If this is a logical type, we convert to the base value.
 * Otherwise the field itself is returned.
 */
@Nullable
public <T> T getBaseValue(int idx, Class<T> clazz) {
    Object value = getValue(idx);
    FieldType fieldType = getSchema().getField(idx).getType();
    if (fieldType.getTypeName().isLogicalType() && value != null) {
        while (fieldType.getTypeName().isLogicalType()) {
            value = fieldType.getLogicalType().toBaseType(value);
            fieldType = fieldType.getLogicalType().getBaseType();
        }
    }
    return (T) value;
}
Also used : FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

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