Search in sources :

Example 1 with Transaction

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();
}
Also used : SpannerConfig(org.apache.beam.sdk.io.gcp.spanner.SpannerConfig) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Transaction(org.apache.beam.sdk.io.gcp.spanner.Transaction) Pipeline(org.apache.beam.sdk.Pipeline) Struct(com.google.cloud.spanner.Struct)

Aggregations

Struct (com.google.cloud.spanner.Struct)1 Pipeline (org.apache.beam.sdk.Pipeline)1 SpannerConfig (org.apache.beam.sdk.io.gcp.spanner.SpannerConfig)1 Transaction (org.apache.beam.sdk.io.gcp.spanner.Transaction)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1