Search in sources :

Example 1 with RowWithGetters

use of org.apache.beam.sdk.values.RowWithGetters in project beam by apache.

the class FromRowUsingCreator method fromRow.

@SuppressWarnings("unchecked")
public <ValueT> ValueT fromRow(Row row, Class<ValueT> clazz, Factory<List<FieldValueTypeInformation>> typeFactory) {
    if (row instanceof RowWithGetters) {
        Object target = ((RowWithGetters) row).getGetterTarget();
        if (target.getClass().equals(clazz)) {
            // Efficient path: simply extract the underlying object instead of creating a new one.
            return (ValueT) target;
        }
    }
    Object[] params = new Object[row.getFieldCount()];
    Schema schema = row.getSchema();
    List<FieldValueTypeInformation> typeInformations = typeFactory.create(clazz, schema);
    checkState(typeInformations.size() == row.getFieldCount(), "Did not have a matching number of type informations and fields.");
    for (int i = 0; i < row.getFieldCount(); ++i) {
        FieldType type = schema.getField(i).getType();
        FieldValueTypeInformation typeInformation = checkNotNull(typeInformations.get(i));
        params[i] = fromValue(type, row.getValue(i), typeInformation.getRawType(), typeInformation, typeFactory);
    }
    SchemaUserTypeCreator creator = schemaTypeCreatorFactory.create(clazz, schema);
    return (ValueT) creator.create(params);
}
Also used : RowWithGetters(org.apache.beam.sdk.values.RowWithGetters) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType)

Aggregations

FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)1 RowWithGetters (org.apache.beam.sdk.values.RowWithGetters)1