Java Code Examples for org.apache.beam.sdk.values.Row#getArray()

The following examples show how to use org.apache.beam.sdk.values.Row#getArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaBeanSchemaTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecursiveArrayGetters() throws NoSuchSchemaException {
  SchemaRegistry registry = SchemaRegistry.createDefault();
  SchemaTestUtils.assertSchemaEquivalent(
      NESTED_ARRAY_BEAN_SCHEMA, registry.getSchema(NestedArrayBean.class));

  SimpleBean simple1 = createSimple("string1");
  SimpleBean simple2 = createSimple("string2");
  SimpleBean simple3 = createSimple("string3");

  NestedArrayBean bean = new NestedArrayBean(simple1, simple2, simple3);
  Row row = registry.getToRowFunction(NestedArrayBean.class).apply(bean);
  List<Row> rows = (List) row.getArray("beans");
  assertSame(simple1, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(0)));
  assertSame(simple2, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(1)));
  assertSame(simple3, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(2)));
}
 
Example 2
Source File: JavaFieldSchemaTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecursiveArrayGetters() throws NoSuchSchemaException {
  SchemaRegistry registry = SchemaRegistry.createDefault();
  SchemaTestUtils.assertSchemaEquivalent(
      NESTED_ARRAY_POJO_SCHEMA, registry.getSchema(NestedArrayPOJO.class));

  SimplePOJO simple1 = createSimple("string1");
  SimplePOJO simple2 = createSimple("string2");
  SimplePOJO simple3 = createSimple("string3");

  NestedArrayPOJO pojo = new NestedArrayPOJO(simple1, simple2, simple3);
  Row row = registry.getToRowFunction(NestedArrayPOJO.class).apply(pojo);
  List<Row> rows = (List) row.getArray("pojos");
  assertSame(simple1, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(0)));
  assertSame(simple2, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(1)));
  assertSame(simple3, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(2)));
}
 
Example 3
Source File: BeamRowToBigtableFn.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Method generates a set of Bigtable mutations from the field specified in the input. Example: a
 * collection named ‘mycolumn’ with three values would be expanded into three column qualifiers in
 * inside Cloud Bigtable called, ‘mycolumn[0]’, ‘mycolumn[1]’ ,‘mycolumn[2]’. Cell values will be
 * serialized with the {@link BeamRowToBigtableFn#primitiveFieldToBytes(TypeName, Object)}
 * primitiveFieldToBytes} method.
 *
 * @param row The row to get the collection from.
 * @param field The field pointing to a collection in the row.
 * @return A set of mutation on the format specified above.
 */
private List<Mutation> createCollectionMutations(Row row, Field field) {
  List<Mutation> mutations = new ArrayList<>();
  List<Object> list = new ArrayList<Object>(row.getArray(field.getName()));
  TypeName collectionElementType = field.getType().getCollectionElementType().getTypeName();
  for (int i = 0; i < list.size(); i++) {
    String fieldName = field.getName() + "[" + i + "]";
    ByteString value = primitiveFieldToBytes(collectionElementType, list.get(i));
    SetCell cell = createCell(defaultColumnFamily.get(), fieldName, value);
    mutations.add(Mutation.newBuilder().setSetCell(cell).build());
  }
  return mutations;
}
 
Example 4
Source File: BeamUncollectRel.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void process(@Element Row inputRow, OutputReceiver<Row> output) {
  for (Object element : inputRow.getArray(0)) {
    if (element instanceof Row) {
      Row nestedRow = (Row) element;
      output.output(Row.withSchema(schema).addValues(nestedRow.getBaseValues()).build());
    } else {
      output.output(Row.withSchema(schema).addValue(element).build());
    }
  }
}
 
Example 5
Source File: BeamUnnestRel.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void process(@Element Row row, OutputReceiver<Row> out) {

  @Nullable Collection<Object> rawValues = row.getArray(unnestIndex);

  if (rawValues == null) {
    return;
  }
  Schema.TypeName typeName =
      outputSchema.getField(unnestIndex).getType().getCollectionElementType().getTypeName();

  for (Object uncollectedValue : rawValues) {
    if (typeName.equals(Schema.TypeName.ROW)) {
      Row nestedRow = (Row) uncollectedValue;
      out.output(
          Row.withSchema(outputSchema)
              .addValues(row.getBaseValues())
              .addValues(nestedRow.getBaseValues())
              .build());
    } else {
      out.output(
          Row.withSchema(outputSchema)
              .addValues(row.getBaseValues())
              .addValue(uncollectedValue)
              .build());
    }
  }
}