private String instance() { return getId().getInstanceId().getInstance(); }
@Test public void basics() { String name = "projects/test-project/instances/test-instance/databases/database-1"; DatabaseId db = DatabaseId.of(name); assertThat(db.getName()).isEqualTo(name); assertThat(db.getInstanceId().getInstance()).isEqualTo("test-instance"); assertThat(db.getDatabase()).isEqualTo("database-1"); assertThat(DatabaseId.of("test-project", "test-instance", "database-1")).isEqualTo(db); assertThat(DatabaseId.of(name)).isEqualTo(db); assertThat(DatabaseId.of(name).hashCode()).isEqualTo(db.hashCode()); assertThat(db.toString()).isEqualTo(name); }
static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_index]
static void addCommitTimestamp(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP " + "OPTIONS (allow_commit_timestamp=true)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_timestamp_column]
static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) " + "STORING (MarketingBudget)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle2 index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_storing_index]
static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added MarketingBudget column"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_column]
static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, DatabaseId id) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = dbAdminClient.updateDatabaseDdl( id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList( "CREATE TABLE Performances (\n" + " SingerId INT64 NOT NULL,\n" + " VenueId INT64 NOT NULL,\n" + " EventDate Date,\n" + " Revenue INT64, \n" + " LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)\n" + ") PRIMARY KEY (SingerId, VenueId, EventDate),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Created Performances table in database: [" + id + "]"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_table_with_timestamp_column]
static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) { OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase( id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList(
if (!db.getInstanceId().getProject().equals(clientProject)) { System.err.println( "Invalid project specified. Project in the database id should match"
/** * Get the instance ID used to perform database operations. * @return the instance ID string. */ public String getInstanceId() { return this.databaseId.getInstanceId().getInstance(); }
/** * Get the instance ID used to perform database operations. * @return the instance ID string. */ public String getInstanceId() { return this.databaseId.getInstanceId().getInstance(); }
private String instance() { return getId().getInstanceId().getInstance(); }