public interface DatabaseAdminClient
Modifier and Type | Method and Description |
---|---|
OperationFuture<Database,com.google.spanner.admin.database.v1.CreateDatabaseMetadata> |
createDatabase(String instanceId,
String databaseId,
Iterable<String> statements)
Creates a new database in a Cloud Spanner instance.
|
void |
dropDatabase(String instanceId,
String databaseId)
Drops a Cloud Spanner database.
|
Database |
getDatabase(String instanceId,
String databaseId)
Gets the current state of a Cloud Spanner database.
|
List<String> |
getDatabaseDdl(String instanceId,
String databaseId)
Returns the schema of a Cloud Spanner database as a list of formatted DDL statements.
|
Page<Database> |
listDatabases(String instanceId,
Options.ListOption... options)
Returns the list of Cloud Spanner database in the given instance.
|
OperationFuture<Void,com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata> |
updateDatabaseDdl(String instanceId,
String databaseId,
Iterable<String> statements,
String operationId)
Enqueues the given DDL statements to be applied, in order but not necessarily all at once, to
the database schema at some point (or points) in the future.
|
OperationFuture<Database,com.google.spanner.admin.database.v1.CreateDatabaseMetadata> createDatabase(String instanceId, String databaseId, Iterable<String> statements) throws SpannerException
Example to create database.
String instanceId = my_instance_id;
String databaseId = my_database_id;
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient
.createDatabase(
instanceId,
databaseId,
Arrays.asList(
"CREATE TABLE Singers (\n"
+ " SingerId INT64 NOT NULL,\n"
+ " FirstName STRING(1024),\n"
+ " LastName STRING(1024),\n"
+ " SingerInfo BYTES(MAX)\n"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums (\n"
+ " SingerId INT64 NOT NULL,\n"
+ " AlbumId INT64 NOT NULL,\n"
+ " AlbumTitle STRING(MAX)\n"
+ ") PRIMARY KEY (SingerId, AlbumId),\n"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));
Database db = op.waitFor().getResult();
instanceId
- the id of the instance in which to create the database.databaseId
- the id of the database which will be created. It must conform to the regular
expression [a-z][a-z0-9_\-]*[a-z0-9] and be between 2 and 30 characters in lengthstatements
- DDL statements to run while creating the database, for example CREATE
TABLE MyTable ( ... )
. This should not include CREATE DATABASE
statement.SpannerException
Database getDatabase(String instanceId, String databaseId) throws SpannerException
Example to getDatabase.
String instanceId = my_instance_id;
String databaseId = my_database_id;
Database db = dbAdminClient.getDatabase(instanceId, databaseId);
SpannerException
OperationFuture<Void,com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata> updateDatabaseDdl(String instanceId, String databaseId, Iterable<String> statements, @Nullable String operationId) throws SpannerException
Example to update the database DDL.
String instanceId = my_instance_id;
String databaseId = my_database_id;
dbAdminClient.updateDatabaseDdl(instanceId,
databaseId,
Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"),
null).waitFor();
operationId
- Operation id assigned to this operation. If null, system will autogenerate
one. This must be unique within a database abd must be a valid identifier
[a-zA-Z][a-zA-Z0-9_]*.SpannerException
void dropDatabase(String instanceId, String databaseId) throws SpannerException
Example to drop a Cloud Spanner database.
String instanceId = my_instance_id;
String databaseId = my_database_id;
dbAdminClient.dropDatabase(instanceId, databaseId);
SpannerException
List<String> getDatabaseDdl(String instanceId, String databaseId)
Example to get the schema of a Cloud Spanner database.
String instanceId = my_instance_id;
String databaseId = my_database_id;
List<String> statementsInDb = dbAdminClient.getDatabaseDdl(instanceId, databaseId);
Page<Database> listDatabases(String instanceId, Options.ListOption... options)
Example to get the list of Cloud Spanner database in the given instance.
String instanceId = my_instance_id;
Page<Database> page = dbAdminClient.listDatabases(instanceId, Options.pageSize(1));
List<Database> dbs = new ArrayList<>();
while (page != null) {
Database db = Iterables.getOnlyElement(page.getValues());
dbs.add(db);
page = page.getNextPage();
}
Copyright © 2019 Google LLC. All rights reserved.