Java Code Examples for com.google.cloud.spanner.BatchClient#batchReadOnlyTransaction()

The following examples show how to use com.google.cloud.spanner.BatchClient#batchReadOnlyTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadInformationSchema.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  Transaction transaction = c.sideInput(tx);
  BatchTransactionId transactionId = transaction.transactionId();

  BatchClient batchClient = spannerAccessor.getBatchClient();

  BatchReadOnlyTransaction context = batchClient.batchReadOnlyTransaction(transactionId);

  InformationSchemaScanner scanner = new InformationSchemaScanner(context);
  Ddl ddl = scanner.scan();
  c.output(ddl);
}
 
Example 2
Source File: InformationSchemaScannerTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private Ddl getDatabaseDdl() {
  BatchClient batchClient = spannerServer.getBatchClient(dbId);
  BatchReadOnlyTransaction batchTx =
      batchClient.batchReadOnlyTransaction(TimestampBound.strong());
  InformationSchemaScanner scanner = new InformationSchemaScanner(batchTx);
  return scanner.scan();
}
 
Example 3
Source File: BatchSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/**
 * This example showcases how to create a batch client, partition a query, and concurrently read
 * from multiple partitions.
 */
public static void main(String[] args) throws InterruptedException {
  if (args.length != 2) {
    System.err.println("Usage: BatchSample <instance_id> <database_id>");
    return;
  }

  /*
   * CREATE TABLE Singers (
   *   SingerId   INT64 NOT NULL,
   *   FirstName  STRING(1024),
   *   LastName   STRING(1024),
   *   SingerInfo BYTES(MAX),
   * ) PRIMARY KEY (SingerId);
   */

  String instanceId = args[0];
  String databaseId = args[1];

  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();

  // [START spanner_batch_client]
  int numThreads = Runtime.getRuntime().availableProcessors();
  ExecutorService executor = Executors.newFixedThreadPool(numThreads);

  // Statistics
  int totalPartitions;
  AtomicInteger totalRecords = new AtomicInteger(0);

  try {
    BatchClient batchClient =
        spanner.getBatchClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));

    final BatchReadOnlyTransaction txn =
        batchClient.batchReadOnlyTransaction(TimestampBound.strong());

    // A Partition object is serializable and can be used from a different process.
    List<Partition> partitions =
        txn.partitionQuery(
            PartitionOptions.getDefaultInstance(),
            Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"));

    totalPartitions = partitions.size();

    for (final Partition p : partitions) {
      executor.execute(
          () -> {
            try (ResultSet results = txn.execute(p)) {
              while (results.next()) {
                long singerId = results.getLong(0);
                String firstName = results.getString(1);
                String lastName = results.getString(2);
                System.out.println("[" + singerId + "] " + firstName + " " + lastName);
                totalRecords.getAndIncrement();
              }
            }
          });
    }
  } finally {
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.HOURS);
    spanner.close();
  }

  double avgRecordsPerPartition = 0.0;
  if (totalPartitions != 0) {
    avgRecordsPerPartition = (double) totalRecords.get() / totalPartitions;
  }
  System.out.println("totalPartitions=" + totalPartitions);
  System.out.println("totalRecords=" + totalRecords);
  System.out.println("avgRecordsPerPartition=" + avgRecordsPerPartition);
  // [END spanner_batch_client]
}