/** * Creates a {@code DatabaseId} from the name of the database. * * @param name the database name of the form {@code * projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID} * @throws IllegalArgumentException if {@code name} does not conform to the expected pattern */ static DatabaseId of(String name) { Map<String, String> parts = NAME_TEMPLATE.match(name); Preconditions.checkArgument( parts != null, "Name should conform to pattern %s: %s", NAME_TEMPLATE, name); return of(parts.get("project"), parts.get("instance"), parts.get("database")); }
@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); }
private String getDatabaseName(String instanceId, String databaseId) { return new DatabaseId(new InstanceId(projectId, instanceId), databaseId).getName(); } }
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]
try { String command = args[0]; DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]); if (!db.getInstanceId().getProject().equals(clientProject)) { System.err.println( "Invalid project specified. Project in the database id should match"
private String database() { return getId().getDatabase(); }
private String instance() { return getId().getInstanceId().getInstance(); }
@Override public String toString() { return getName(); }
/** Creates a {@code DatabaseId} given the instance identity and database id. */ public static DatabaseId of(InstanceId instanceId, String database) { return new DatabaseId(instanceId, database); } }
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]
@Test public void listPagination() throws Exception { List<String> dbIds = ImmutableList.of( testHelper.getUniqueDatabaseId(), testHelper.getUniqueDatabaseId(), testHelper.getUniqueDatabaseId()); String instanceId = testHelper.getInstanceId().getInstance(); for (String dbId : dbIds) { dbs.add(dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.<String>of()).get()); } Page<Database> page = dbAdminClient.listDatabases(instanceId, Options.pageSize(1)); List<String> dbIdsGot = new ArrayList<>(); while (page != null) { Database db = Iterables.getOnlyElement(page.getValues()); dbIdsGot.add(db.getId().getDatabase()); page = page.getNextPage(); } assertThat(dbIdsGot).containsAllIn(dbIds); } }
/** * Get the instance ID used to perform database operations. * @return the instance ID string. */ public String getInstanceId() { return this.databaseId.getInstanceId().getInstance(); }
@Override public String toString() { return String.format("Database[%s, %s]", id.getName(), state); } }
/** Creates a {@code DatabaseId} given project, instance and database IDs. */ public static DatabaseId of(String project, String instance, String database) { return new DatabaseId(new InstanceId(project, instance), database); }
project = spanner.getOptions().getProjectId(); dbClient = spanner.getDatabaseClient(DatabaseId.of(project, instance, database)); } catch (Exception e) { LOGGER.log(Level.SEVERE, "init()", e);
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]
@Test public void databaseOperationsViaEntity() throws Exception { String dbId = testHelper.getUniqueDatabaseId(); String instanceId = testHelper.getInstanceId().getInstance(); String statement1 = "CREATE TABLE T (\n" + " K STRING(MAX),\n" + ") PRIMARY KEY(K)"; OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1)); Database db = op.get(); dbs.add(db); assertThat(db.getId().getDatabase()).isEqualTo(dbId); db = db.reload(); assertThat(db.getId().getDatabase()).isEqualTo(dbId); String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)"; OperationFuture<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null); op2.get(); Iterable<String> statementsInDb = db.getDdl(); assertThat(statementsInDb).containsExactly(statement1, statement2); db.drop(); dbs.clear(); expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); db.reload(); }
/** * Get the instance ID used to perform database operations. * @return the instance ID string. */ public String getInstanceId() { return this.databaseId.getInstanceId().getInstance(); }
@Override public com.google.spanner.v1.Session call() throws Exception { return gapicRpc.createSession( db.getName(), getOptions().getSessionLabels(), options); } });
private String getDatabaseName(String instanceId, String databaseId) { return new DatabaseId(new InstanceId(projectId, instanceId), databaseId).getName(); } }