use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITDatabaseAdminTest method databaseOperationsViaEntity.
@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)";
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
op = op.waitFor();
Database db = op.getResult();
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)";
Operation<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
op2.waitFor();
Iterable<String> statementsInDb = db.getDdl();
assertThat(statementsInDb).containsExactly(statement1, statement2);
db.drop();
dbs.clear();
expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
db.reload();
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITDatabaseAdminTest method updateDdlRetry.
@Test
public void updateDdlRetry() throws Exception {
String dbId = testHelper.getUniqueDatabaseId();
String instanceId = testHelper.getInstanceId().getInstance();
String statement1 = "CREATE TABLE T (\n" + " K STRING(MAX),\n" + ") PRIMARY KEY(K)";
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
op = op.waitFor();
Database db = op.getResult();
dbs.add(db);
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
Operation<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
Operation<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
op1 = op1.waitFor();
op2 = op2.waitFor();
assertThat(op1.getMetadata()).isEqualTo(op2.getMetadata());
}
use of com.google.cloud.spanner.Database in project java-docs-samples by GoogleCloudPlatform.
the class SpannerTasks method createDatabase.
private static void createDatabase(PrintWriter pw) {
Iterable<String> statements = 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 = SpannerClient.getDatabaseAdminClient().createDatabase(SpannerClient.getInstanceId(), SpannerClient.getDatabaseId(), statements).waitFor().getResult();
pw.println("Created database [" + db.getId() + "]");
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class DatabaseAdminClientSnippets method createDatabase.
/**
* Example to create database.
*/
// [TARGET createDatabase(String, String, Iterable)]
// [VARIABLE my_instance_id]
// [VARIABLE my_database_id]
public Database createDatabase(String instanceId, String databaseId) {
// [START createDatabase]
OperationFuture<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;
try {
db = op.get();
} catch (ExecutionException e) {
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
// [END createDatabase]
return db;
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class DatabaseAdminClientSnippets method listDatabases.
/**
* Example to get the list of Cloud Spanner database in the given instance.
*/
// [TARGET listDatabases(String, ListOption...)]
// [VARIABLE my_instance_id]
public List<Database> listDatabases(String instanceId) {
// [START listDatabases]
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();
}
// [END listDatabases]
return dbs;
}
Aggregations