Search in sources :

Example 6 with SpannerOptions

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

the class SpannerSnippets method getDatabaseAdminClient.

DatabaseAdminClient getDatabaseAdminClient() {
    // [START get_dbadmin_client]
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    return dbAdminClient;
}
Also used : DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 7 with SpannerOptions

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

the class SpannerSnippets method getDatabaseClient.

DatabaseClient getDatabaseClient() {
    // [START get_db_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);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    return dbClient;
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) DatabaseId(com.google.cloud.spanner.DatabaseId) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 8 with SpannerOptions

use of com.google.cloud.spanner.SpannerOptions in project spring-cloud-gcp by spring-cloud.

the class GcpSpannerEmulatorAutoConfigurationTests method testEmulatorAutoConfigurationEnabled.

@Test
public void testEmulatorAutoConfigurationEnabled() {
    this.contextRunner.withPropertyValues("spring.cloud.gcp.spanner.emulator.enabled=true").run(context -> {
        SpannerOptions spannerOptions = context.getBean(SpannerOptions.class);
        assertThat(spannerOptions.getEndpoint()).isEqualTo("localhost:9010");
    });
}
Also used : SpannerOptions(com.google.cloud.spanner.SpannerOptions) Test(org.junit.Test)

Example 9 with SpannerOptions

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

the class RemoteSpannerHelper method create.

/**
   * Creates a {@code RemoteSpannerHelper} bound to the given instance ID. All databases created
   * using this will be created in the given instance.
   */
public static RemoteSpannerHelper create(InstanceId instanceId) throws Throwable {
    SpannerOptions options = SpannerOptions.newBuilder().setProjectId(instanceId.getProject()).build();
    Spanner client = options.getService();
    return new RemoteSpannerHelper(options, instanceId, client);
}
Also used : SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 10 with SpannerOptions

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

the class QuickstartSample 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
        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)

Aggregations

SpannerOptions (com.google.cloud.spanner.SpannerOptions)19 Spanner (com.google.cloud.spanner.Spanner)13 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 ResultSet (com.google.cloud.spanner.ResultSet)4 Test (org.junit.Test)4 BatchClient (com.google.cloud.spanner.BatchClient)3 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)3 DatabaseId (com.google.cloud.spanner.DatabaseId)3 Builder (com.google.cloud.spanner.SpannerOptions.Builder)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 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 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)1 InstanceConfig (com.google.cloud.spanner.InstanceConfig)1 Partition (com.google.cloud.spanner.Partition)1 CommitRequest (com.google.spanner.v1.CommitRequest)1 CommitResponse (com.google.spanner.v1.CommitResponse)1 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)1