.withDatabaseId(databaseId); PCollectionView<Transaction> tx = p.apply( SpannerIO.createTransaction() .withSpannerConfig(spannerConfig) .withTimestampBound(TimestampBound.strong()));
@Override public PCollection<Struct> expand(PCollection<ReadOperation> input) { PCollectionView<Transaction> txView = getTxView(); if (txView == null) { Pipeline begin = input.getPipeline(); SpannerIO.CreateTransaction createTx = SpannerIO.createTransaction() .withSpannerConfig(getSpannerConfig()) .withTimestampBound(getTimestampBound()); txView = begin.apply(createTx); } return input.apply( "Naive read from Cloud Spanner", ParDo.of(new NaiveSpannerReadFn(getSpannerConfig(), txView)).withSideInputs(txView)); }
pipeline.apply( "tx", SpannerIO.createTransaction() .withSpannerConfig(spannerConfig) .withTimestampBound(timestampBound));
@Override public PCollection<Struct> expand(PCollection<ReadOperation> input) { PCollectionView<Transaction> txView = getTxView(); if (txView == null) { Pipeline begin = input.getPipeline(); SpannerIO.CreateTransaction createTx = SpannerIO.createTransaction() .withSpannerConfig(getSpannerConfig()) .withTimestampBound(getTimestampBound()); txView = begin.apply(createTx); } return input .apply( "Generate Partitions", ParDo.of(new GeneratePartitionsFn(getSpannerConfig(), txView)).withSideInputs(txView)) .apply("Shuffle partitions", Reshuffle.<Partition>viaRandomKey()) .apply( "Read from Partitions", ParDo.of(new ReadFromPartitionFn(getSpannerConfig(), txView)).withSideInputs(txView)); }
@Test public void testQuery() throws Exception { SpannerConfig spannerConfig = createSpannerConfig(); PCollectionView<Transaction> tx = p.apply( SpannerIO.createTransaction() .withSpannerConfig(spannerConfig) .withTimestampBound(TimestampBound.strong())); PCollection<Struct> output = p.apply( SpannerIO.read() .withSpannerConfig(spannerConfig) .withQuery("SELECT * FROM " + options.getTable()) .withTransaction(tx)); PAssert.thatSingleton(output.apply("Count rows", Count.globally())).isEqualTo(5L); p.run(); }
@Test public void testRead() throws Exception { SpannerConfig spannerConfig = createSpannerConfig(); PCollectionView<Transaction> tx = p.apply( SpannerIO.createTransaction() .withSpannerConfig(spannerConfig) .withTimestampBound(TimestampBound.strong())); PCollection<Struct> output = p.apply( SpannerIO.read() .withSpannerConfig(spannerConfig) .withTable(options.getTable()) .withColumns("Key", "Value") .withTransaction(tx)); PAssert.thatSingleton(output.apply("Count rows", Count.<Struct>globally())).isEqualTo(5L); p.run(); }
@Test public void testReadAllRecordsInDb() throws Exception { SpannerConfig spannerConfig = createSpannerConfig(); PCollectionView<Transaction> tx = p.apply( SpannerIO.createTransaction() .withSpannerConfig(spannerConfig) .withTimestampBound(TimestampBound.strong())); PCollection<Struct> allRecords = p.apply( SpannerIO.read() .withSpannerConfig(spannerConfig) .withBatching(false) .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().withTransaction(tx).withSpannerConfig(spannerConfig)); PAssert.thatSingleton(allRecords.apply("Count rows", Count.globally())).isEqualTo(5L); p.run(); }
begin.apply(SpannerIO.createTransaction().withSpannerConfig(spannerConfig));
Pipeline p = begin.getPipeline(); PCollectionView<Transaction> tx = p.apply(SpannerIO.createTransaction().withSpannerConfig(spannerConfig)); PCollection<Ddl> ddl = p.apply("Read Information Schema", new ReadInformationSchema(spannerConfig, tx));