use of org.apache.beam.sdk.io.gcp.spanner.ReadOperation in project java-docs-samples by GoogleCloudPlatform.
the class SpannerReadAll method main.
public static void main(String[] args) {
Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
Pipeline p = Pipeline.create(options);
SpannerConfig spannerConfig = SpannerConfig.create().withInstanceId(options.getInstanceId()).withDatabaseId(options.getDatabaseId());
// [START spanner_dataflow_readall]
PCollection<Struct> allRecords = p.apply(SpannerIO.read().withSpannerConfig(spannerConfig).withQuery("SELECT t.table_name FROM information_schema.tables AS t WHERE t" + ".table_catalog = '' AND t.table_schema = ''")).apply(MapElements.into(TypeDescriptor.of(ReadOperation.class)).via((SerializableFunction<Struct, ReadOperation>) input -> {
String tableName = input.getString(0);
return ReadOperation.create().withQuery("SELECT * FROM " + tableName);
})).apply(SpannerIO.readAll().withSpannerConfig(spannerConfig));
// [END spanner_dataflow_readall]
PCollection<Long> dbEstimatedSize = allRecords.apply(EstimateSize.create()).apply(Sum.longsGlobally());
dbEstimatedSize.apply(ToString.elements()).apply(TextIO.write().to(options.getOutput()).withoutSharding());
p.run().waitUntilFinish();
}
Aggregations