Search in sources :

Example 1 with DatabaseId

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

the class SpannerSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        printUsageAndExit();
    }
    // [START init_client]
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    try {
        String command = args[0];
        DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
        // [END init_client]
        // This will return the default project id based on the environment.
        String clientProject = spanner.getOptions().getProjectId();
        if (!db.getInstanceId().getProject().equals(clientProject)) {
            System.err.println("Invalid project specified. Project in the database id should match" + "the project name set in the environment variable GCLOUD_PROJECT. Expected: " + clientProject);
            printUsageAndExit();
        }
        // [START init_client]
        DatabaseClient dbClient = spanner.getDatabaseClient(db);
        DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
        // [END init_client]
        run(dbClient, dbAdminClient, command, db);
    } finally {
        spanner.close();
    }
    System.out.println("Closed client");
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) DatabaseId(com.google.cloud.spanner.DatabaseId) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 2 with DatabaseId

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

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

Aggregations

DatabaseId (com.google.cloud.spanner.DatabaseId)3 Spanner (com.google.cloud.spanner.Spanner)3 SpannerOptions (com.google.cloud.spanner.SpannerOptions)3 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 BatchClient (com.google.cloud.spanner.BatchClient)1 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)1