Search in sources :

Example 11 with Spanner

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

the class DatabaseSelect method main.

public static void main(String... args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: QuickStartSample <instance_id> <database_id>");
        return;
    }
    // Instantiates a client
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    // Name of your instance & database.
    String instanceId = args[0];
    String databaseId = args[1];
    try {
        // Creates a database client
        DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
        // Queries the database
        try (ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"))) {
            System.out.println("\n\nResults:");
            // Prints the results
            while (resultSet.next()) {
                System.out.printf("%d\n\n", resultSet.getLong(0));
            }
        }
    } finally {
        // Closes the client which will free up the resources used
        spanner.close();
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ResultSet(com.google.cloud.spanner.ResultSet) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 12 with Spanner

use of com.google.cloud.spanner.Spanner 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 13 with Spanner

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

the class SpannerSnippets method getInstanceAdminClient.

InstanceAdminClient getInstanceAdminClient() {
    // [START get_instance_admin_client]
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    return instanceAdminClient;
}
Also used : InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 14 with Spanner

use of com.google.cloud.spanner.Spanner 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 15 with Spanner

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

the class InstanceConfigIT method testEuropeWestSingleNodeConfig.

@Test
public void testEuropeWestSingleNodeConfig() {
    String credentialsPath = "cloudspanner-emulator-key.json";
    String projectId = "test-project";
    GoogleCredentials credentials = null;
    try {
        credentials = CloudSpannerConnection.getCredentialsFromFile(credentialsPath);
    } catch (IOException e) {
        throw new RuntimeException("Could not read key file " + credentialsPath, e);
    }
    Builder builder = SpannerOptions.newBuilder();
    builder.setProjectId(projectId);
    builder.setCredentials(credentials);
    builder.setHost(CloudSpannerIT.getHost());
    SpannerOptions options = builder.build();
    Spanner spanner = options.getService();
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    InstanceConfig config = instanceAdminClient.getInstanceConfig("regional-europe-west1");
    assertEquals("regional-europe-west1", config.getId().getInstanceConfig());
    spanner.close();
}
Also used : InstanceConfig(com.google.cloud.spanner.InstanceConfig) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) Builder(com.google.cloud.spanner.SpannerOptions.Builder) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) IOException(java.io.IOException) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner) IntegrationTest(nl.topicus.jdbc.test.category.IntegrationTest) Test(org.junit.Test)

Aggregations

Spanner (com.google.cloud.spanner.Spanner)15 SpannerOptions (com.google.cloud.spanner.SpannerOptions)13 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 ResultSet (com.google.cloud.spanner.ResultSet)4 BatchClient (com.google.cloud.spanner.BatchClient)3 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)3 DatabaseId (com.google.cloud.spanner.DatabaseId)3 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)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 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)1 InstanceConfig (com.google.cloud.spanner.InstanceConfig)1 Partition (com.google.cloud.spanner.Partition)1 Builder (com.google.cloud.spanner.SpannerOptions.Builder)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