Search in sources :

Example 1 with BatchClient

use of com.google.cloud.spanner.BatchClient 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 BatchClient

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

the class SpannerSnippets method getBatchClient.

BatchClient getBatchClient() {
    // [START get_batch_client]
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    final String project = "test-project";
    final String instance = "test-instance";
    final String database = "example-db";
    DatabaseId db = DatabaseId.of(project, instance, database);
    BatchClient batchClient = spanner.getBatchClient(db);
    return batchClient;
}
Also used : BatchClient(com.google.cloud.spanner.BatchClient) DatabaseId(com.google.cloud.spanner.DatabaseId) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 3 with BatchClient

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

the class SpannerAccessor method createAndConnect.

private static SpannerAccessor createAndConnect(SpannerConfig spannerConfig) {
    SpannerOptions.Builder builder = SpannerOptions.newBuilder();
    // Set retryable codes for all API methods
    if (spannerConfig.getRetryableCodes() != null) {
        builder.getSpannerStubSettingsBuilder().applyToAllUnaryMethods(input -> {
            input.setRetryableCodes(spannerConfig.getRetryableCodes());
            return null;
        });
        builder.getSpannerStubSettingsBuilder().executeStreamingSqlSettings().setRetryableCodes(spannerConfig.getRetryableCodes());
    }
    // Set commit retry settings
    UnaryCallSettings.Builder<CommitRequest, CommitResponse> commitSettings = builder.getSpannerStubSettingsBuilder().commitSettings();
    ValueProvider<Duration> commitDeadline = spannerConfig.getCommitDeadline();
    if (spannerConfig.getCommitRetrySettings() != null) {
        commitSettings.setRetrySettings(spannerConfig.getCommitRetrySettings());
    } else if (commitDeadline != null && commitDeadline.get().getMillis() > 0) {
        // Set the GRPC deadline on the Commit API call.
        RetrySettings.Builder commitRetrySettingsBuilder = commitSettings.getRetrySettings().toBuilder();
        commitSettings.setRetrySettings(commitRetrySettingsBuilder.setTotalTimeout(org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis())).setMaxRpcTimeout(org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis())).setInitialRpcTimeout(org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis())).build());
    }
    // Set execute streaming sql retry settings
    ServerStreamingCallSettings.Builder<ExecuteSqlRequest, PartialResultSet> executeStreamingSqlSettings = builder.getSpannerStubSettingsBuilder().executeStreamingSqlSettings();
    if (spannerConfig.getExecuteStreamingSqlRetrySettings() != null) {
        executeStreamingSqlSettings.setRetrySettings(spannerConfig.getExecuteStreamingSqlRetrySettings());
    } else {
        // Setting the timeout for streaming read to 2 hours. This is 1 hour by default
        // after BEAM 2.20.
        RetrySettings.Builder executeSqlStreamingRetrySettings = executeStreamingSqlSettings.getRetrySettings().toBuilder();
        executeStreamingSqlSettings.setRetrySettings(executeSqlStreamingRetrySettings.setInitialRpcTimeout(org.threeten.bp.Duration.ofMinutes(120)).setMaxRpcTimeout(org.threeten.bp.Duration.ofMinutes(120)).setTotalTimeout(org.threeten.bp.Duration.ofMinutes(120)).build());
    }
    ValueProvider<String> projectId = spannerConfig.getProjectId();
    if (projectId != null) {
        builder.setProjectId(projectId.get());
    }
    ServiceFactory<Spanner, SpannerOptions> serviceFactory = spannerConfig.getServiceFactory();
    if (serviceFactory != null) {
        builder.setServiceFactory(serviceFactory);
    }
    ValueProvider<String> host = spannerConfig.getHost();
    if (host != null) {
        builder.setHost(host.get());
    }
    ValueProvider<String> emulatorHost = spannerConfig.getEmulatorHost();
    if (emulatorHost != null) {
        builder.setEmulatorHost(emulatorHost.get());
        if (spannerConfig.getIsLocalChannelProvider() != null && spannerConfig.getIsLocalChannelProvider().get()) {
            builder.setChannelProvider(LocalChannelProvider.create(emulatorHost.get()));
        }
        builder.setCredentials(NoCredentials.getInstance());
    }
    String userAgentString = USER_AGENT_PREFIX + "/" + ReleaseInfo.getReleaseInfo().getVersion();
    builder.setHeaderProvider(FixedHeaderProvider.create("user-agent", userAgentString));
    SpannerOptions options = builder.build();
    Spanner spanner = options.getService();
    String instanceId = spannerConfig.getInstanceId().get();
    String databaseId = spannerConfig.getDatabaseId().get();
    DatabaseClient databaseClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
    BatchClient batchClient = spanner.getBatchClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
    DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();
    return new SpannerAccessor(spanner, databaseClient, databaseAdminClient, batchClient, spannerConfig);
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) BatchClient(com.google.cloud.spanner.BatchClient) CommitResponse(com.google.spanner.v1.CommitResponse) Duration(org.joda.time.Duration) SpannerOptions(com.google.cloud.spanner.SpannerOptions) ServerStreamingCallSettings(com.google.api.gax.rpc.ServerStreamingCallSettings) RetrySettings(com.google.api.gax.retrying.RetrySettings) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) DatabaseClient(com.google.cloud.spanner.DatabaseClient) UnaryCallSettings(com.google.api.gax.rpc.UnaryCallSettings) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) PartialResultSet(com.google.spanner.v1.PartialResultSet) Spanner(com.google.cloud.spanner.Spanner)

Example 4 with BatchClient

use of com.google.cloud.spanner.BatchClient in project spanner-jdbc by olavloite.

the class BatchReadOnlyTest method testExecuteBatchReadOnly.

@Test
public void testExecuteBatchReadOnly() throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    for (int testRun = 0; testRun < 2; testRun++) {
        final int numberOfPartitions = 6;
        BatchClient batchClient = mock(BatchClient.class);
        BatchReadOnlyTransaction tx = mock(BatchReadOnlyTransaction.class);
        List<Partition> partitions = new ArrayList<>(numberOfPartitions);
        for (int i = 0; i < numberOfPartitions; i++) partitions.add(mock(Partition.class));
        when(tx.partitionQuery(any(), any())).then(new Returns(partitions));
        when(batchClient.batchReadOnlyTransaction(TimestampBound.strong())).then(new Returns(tx));
        Field field = CloudSpannerTransaction.class.getDeclaredField("batchClient");
        field.setAccessible(true);
        field.set(connection.getTransaction(), batchClient);
        connection.setBatchReadOnly(true);
        Statement statement;
        if (testRun % 2 == 0) {
            statement = connection.createStatement();
            assertTrue(statement.execute(SELECT_ALL_FROM_FOO));
        } else {
            PreparedStatement ps = connection.prepareStatement(SELECT_ALL_FROM_FOO);
            assertTrue(ps.execute());
            statement = ps;
        }
        List<ResultSet> resultSets = new ArrayList<>();
        do {
            resultSets.add(statement.getResultSet());
        } while (statement.getMoreResults());
        assertEquals(numberOfPartitions, resultSets.size());
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) Returns(org.mockito.internal.stubbing.answers.Returns) Field(java.lang.reflect.Field) BatchClient(com.google.cloud.spanner.BatchClient) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Aggregations

BatchClient (com.google.cloud.spanner.BatchClient)4 Spanner (com.google.cloud.spanner.Spanner)3 SpannerOptions (com.google.cloud.spanner.SpannerOptions)3 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)2 Partition (com.google.cloud.spanner.Partition)2 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 ServerStreamingCallSettings (com.google.api.gax.rpc.ServerStreamingCallSettings)1 UnaryCallSettings (com.google.api.gax.rpc.UnaryCallSettings)1 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 DatabaseId (com.google.cloud.spanner.DatabaseId)1 ResultSet (com.google.cloud.spanner.ResultSet)1 CommitRequest (com.google.spanner.v1.CommitRequest)1 CommitResponse (com.google.spanner.v1.CommitResponse)1 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)1 PartialResultSet (com.google.spanner.v1.PartialResultSet)1 Field (java.lang.reflect.Field)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1