Search in sources :

Example 6 with Spanner

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

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

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

the class CloudSpannerDriver method getSpanner.

/**
 * Get a {@link Spanner} instance from the pool or create a new one if needed.
 *
 * @param projectId The projectId to connect to
 * @param credentials The credentials to use for the connection
 * @param host The host to connect to. Normally this is https://spanner.googleapis.com, but you
 *        could also use a (local) emulator. If null, no host will be set and the default host of
 *        Google Cloud Spanner will be used.
 * @return The {@link Spanner} instance to use
 */
synchronized Spanner getSpanner(String projectId, Credentials credentials, String host) {
    SpannerKey key = SpannerKey.of(host, projectId, credentials);
    Spanner spanner = spanners.get(key);
    if (spanner == null) {
        spanner = createSpanner(key);
        spanners.put(key, spanner);
    }
    return spanner;
}
Also used : Spanner(com.google.cloud.spanner.Spanner)

Example 9 with Spanner

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

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

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