use of org.apache.beam.sdk.io.gcp.spanner.Transaction in project java-docs-samples by GoogleCloudPlatform.
the class TransactionalRead method main.
public static void main(String[] args) {
Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
Pipeline p = Pipeline.create(options);
String instanceId = options.getInstanceId();
String databaseId = options.getDatabaseId();
// [START spanner_dataflow_txread]
SpannerConfig spannerConfig = SpannerConfig.create().withInstanceId(instanceId).withDatabaseId(databaseId);
PCollectionView<Transaction> tx = p.apply(SpannerIO.createTransaction().withSpannerConfig(spannerConfig).withTimestampBound(TimestampBound.strong()));
PCollection<Struct> singers = p.apply(SpannerIO.read().withSpannerConfig(spannerConfig).withQuery("SELECT SingerID, FirstName, LastName FROM Singers").withTransaction(tx));
PCollection<Struct> albums = p.apply(SpannerIO.read().withSpannerConfig(spannerConfig).withQuery("SELECT SingerId, AlbumId, AlbumTitle FROM Albums").withTransaction(tx));
// [END spanner_dataflow_txread]
singers.apply(MapElements.via(new SimpleFunction<Struct, String>() {
@Override
public String apply(Struct input) {
return Joiner.on(DELIMITER).join(input.getLong(0), input.getString(1), input.getString(2));
}
})).apply(TextIO.write().to(options.getSingersFilename()).withoutSharding());
albums.apply(MapElements.via(new SimpleFunction<Struct, String>() {
@Override
public String apply(Struct input) {
return Joiner.on(DELIMITER).join(input.getLong(0), input.getLong(1), input.getString(2));
}
})).apply(TextIO.write().to(options.getAlbumsFilename()).withoutSharding());
p.run().waitUntilFinish();
}
Aggregations