in java/java-dataflow-samples/read-pubsub-write-bigquery/src/main/java/com/cloudcode/dataflow/example/io/ReadPubsubToRow.java [45:63]
public ReadResult expand(PBegin input) {
// We implement this PTransform in two main steps. First, we read Pub/Sub JSON encoded
// string messages.
PCollection<String> json =
input.apply(
"Read Json Messages", PubsubIO.readStrings().fromSubscription(subscription.getPath()));
// Next, we apply the JsonToRow PTransform to convert JSON strings to Beam Rows.
PCollectionTuple toRowResult = json.apply("To Row", JsonToRow.of(userType));
// Finally, we bundle our errors and expected results into a ReadResult convenience class.
PCollection<String> errors = toRowResult.get(ERROR);
PCollection<Row> rows = toRowResult.get(ROW);
return ReadResult.of(
// ***IMPORTANT: Notice the rows.setRowSchema(rows.getSchema()) semantic. This
// avoids errors at pipeline construction time when it complains that it cannot
// derive your Schema.***
input.getPipeline(), ROW, rows.setRowSchema(rows.getSchema()), ERROR, errors);
}