Search in sources :

Example 1 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project java-docs-samples by GoogleCloudPlatform.

the class BatchSample method main.

/**
 * 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]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchClient(com.google.cloud.spanner.BatchClient) SpannerOptions(com.google.cloud.spanner.SpannerOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ExecutorService(java.util.concurrent.ExecutorService) ResultSet(com.google.cloud.spanner.ResultSet) Spanner(com.google.cloud.spanner.Spanner)

Example 2 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project beam by apache.

the class SpannerIOReadTest method readAllPipeline.

@Test
public void readAllPipeline() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    PCollectionView<Transaction> tx = pipeline.apply("tx", SpannerIO.createTransaction().withSpannerConfig(spannerConfig).withTimestampBound(timestampBound));
    PCollection<ReadOperation> reads = pipeline.apply(Create.of(ReadOperation.create().withQuery("SELECT * FROM users"), ReadOperation.create().withTable("users").withColumns("id", "name")));
    PCollection<Struct> one = reads.apply("read all", SpannerIO.readAll().withSpannerConfig(spannerConfig).withTransaction(tx));
    BatchTransactionId txId = new FakeBatchTransactionId("tx");
    when(mockBatchTx.getBatchTransactionId()).thenReturn(txId);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound)).thenReturn(mockBatchTx);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class))).thenReturn(mockBatchTx);
    Partition fakePartition = FakePartitionFactory.createFakeReadPartition(ByteString.copyFromUtf8("partition"));
    when(mockBatchTx.partitionQuery(any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users")), any(ReadQueryUpdateTransactionOption.class))).thenReturn(Arrays.asList(fakePartition, fakePartition));
    when(mockBatchTx.partitionRead(any(PartitionOptions.class), eq("users"), eq(KeySet.all()), eq(Arrays.asList("id", "name")), any(ReadQueryUpdateTransactionOption.class))).thenReturn(Arrays.asList(fakePartition));
    when(mockBatchTx.execute(any(Partition.class))).thenReturn(ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 4)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(4, 6)));
    PAssert.that(one).containsInAnyOrder(FAKE_ROWS);
    pipeline.run();
}
Also used : Partition(com.google.cloud.spanner.Partition) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) PartitionOptions(com.google.cloud.spanner.PartitionOptions) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) TimestampBound(com.google.cloud.spanner.TimestampBound) Test(org.junit.Test)

Example 3 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionRead.

void partitionRead() {
    // [START partition_read]
    final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong());
    List<Partition> partitions = txn.partitionRead(PartitionOptions.getDefaultInstance(), "Singers", KeySet.all(), Arrays.asList("SingerId", "FirstName", "LastName"));
    for (final Partition p : partitions) {
        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);
            }
        }
    }
// [END partition_read]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 4 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionReadUsingIndex.

void partitionReadUsingIndex() {
    // [START partition_read_using_index]
    final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong());
    List<Partition> partitions = txn.partitionReadUsingIndex(PartitionOptions.getDefaultInstance(), "Singers", "SingerId", KeySet.all(), Arrays.asList("SingerId", "FirstName", "LastName"));
    for (Partition p : partitions) {
        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);
            }
        }
    }
// [END partition_read_using_index]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 5 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionQuery.

void partitionQuery() {
    // [START partition_query]
    final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong());
    List<Partition> partitions = txn.partitionQuery(PartitionOptions.getDefaultInstance(), Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"));
    for (final Partition p : partitions) {
        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);
            }
        }
    }
// [END partition_query]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Aggregations

BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)8 Partition (com.google.cloud.spanner.Partition)7 ResultSet (com.google.cloud.spanner.ResultSet)5 BatchClient (com.google.cloud.spanner.BatchClient)2 Test (org.junit.Test)2 Timestamp (com.google.cloud.Timestamp)1 BatchTransactionId (com.google.cloud.spanner.BatchTransactionId)1 FakeBatchTransactionId (com.google.cloud.spanner.FakeBatchTransactionId)1 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)1 PartitionOptions (com.google.cloud.spanner.PartitionOptions)1 Spanner (com.google.cloud.spanner.Spanner)1 SpannerOptions (com.google.cloud.spanner.SpannerOptions)1 Struct (com.google.cloud.spanner.Struct)1 TimestampBound (com.google.cloud.spanner.TimestampBound)1 Field (java.lang.reflect.Field)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1