use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class RemoteSpannerHelper method cleanUp.
/** Deletes all the databases created via {@code createTestDatabase}. Shuts down the client. */
public void cleanUp() {
// Drop all the databases we created explicitly.
int numDropped = 0;
for (Database db : dbs) {
try {
logger.log(Level.FINE, "Dropping test database {0}", db.getId());
db.drop();
++numDropped;
} catch (SpannerException e) {
logger.log(Level.SEVERE, "Failed to drop test database " + db.getId(), e);
}
}
logger.log(Level.INFO, "Dropped {0} test database(s)", numDropped);
client.close();
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITDatabaseAdminTest method databaseOperations.
@Test
public void databaseOperations() 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 = dbAdminClient.getDatabase(instanceId, dbId);
assertThat(db.getId().getDatabase()).isEqualTo(dbId);
boolean foundDb = false;
for (Database dbInList : Iterators.toArray(dbAdminClient.listDatabases(instanceId).iterateAll().iterator(), Database.class)) {
if (dbInList.getId().getDatabase().equals(dbId)) {
foundDb = true;
break;
}
}
assertThat(foundDb).isTrue();
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
Operation<?, ?> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), null);
op2.waitFor();
List<String> statementsInDb = dbAdminClient.getDatabaseDdl(instanceId, dbId);
assertThat(statementsInDb).containsExactly(statement1, statement2);
dbAdminClient.dropDatabase(instanceId, dbId);
dbs.clear();
expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
db = dbAdminClient.getDatabase(testHelper.getInstanceId().getInstance(), dbId);
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITReadOnlyTxnTest method newTestDatabase.
private static DatabaseClient newTestDatabase(ImmutableList.Builder<History> historyBuilder) {
Database newDb = env.getTestHelper().createTestDatabase("CREATE TABLE TestTable ( StringValue STRING(MAX) ) PRIMARY KEY ()");
DatabaseClient newClient = env.getTestHelper().getDatabaseClient(newDb);
for (int i = 0; i < 5; ++i) {
writeNewValue(newClient, i, historyBuilder);
}
return newClient;
}
use of com.google.cloud.spanner.Database in project java-docs-samples by GoogleCloudPlatform.
the class SpannerWriteIT method setUp.
@Before
public void setUp() throws Exception {
instanceId = System.getProperty("spanner.test.instance");
databaseId = "df-spanner-write-it";
spannerOptions = SpannerOptions.getDefaultInstance();
spanner = spannerOptions.getService();
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
try {
adminClient.dropDatabase(instanceId, databaseId);
} catch (SpannerException e) {
// Does not exist, ignore.
}
Operation<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers " + "(singerId INT64 NOT NULL, " + "firstName STRING(MAX) NOT NULL, lastName STRING(MAX) NOT NULL,) " + "PRIMARY KEY (singerId)", "CREATE TABLE Albums (singerId INT64 NOT NULL, " + "albumId INT64 NOT NULL, albumTitle STRING(MAX) NOT NULL,) " + "PRIMARY KEY (singerId, albumId)"));
op.waitFor();
String singers = Stream.of("1\tJohn\tLennon", "2\tPaul\tMccartney", "3\tGeorge\tHarrison", "4\tRingo\tStarr").collect(Collectors.joining("\n"));
singersPath = Files.createTempFile("singers", "txt");
Files.write(singersPath, singers.getBytes());
String albums = Stream.of("1\t1\tImagine", "2\t1\tPipes of Peace", "3\t1\tDark Horse").collect(Collectors.joining("\n"));
albumsPath = Files.createTempFile("albums", "txt");
Files.write(albumsPath, albums.getBytes());
}
use of com.google.cloud.spanner.Database in project java-docs-samples by GoogleCloudPlatform.
the class SpannerReadIT method setUp.
@Before
public void setUp() {
instanceId = System.getProperty("spanner.test.instance");
databaseId = "df-spanner-read-it";
spannerOptions = SpannerOptions.getDefaultInstance();
spanner = spannerOptions.getService();
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
try {
adminClient.dropDatabase(instanceId, databaseId);
} catch (SpannerException e) {
// Does not exist, ignore.
}
Operation<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers " + "(singerId INT64 NOT NULL, firstName STRING(MAX) NOT NULL, " + "lastName STRING(MAX) NOT NULL,) PRIMARY KEY (singerId)", "CREATE TABLE Albums (singerId INT64 NOT NULL, albumId INT64 NOT NULL, " + "albumTitle STRING(MAX) NOT NULL,) PRIMARY KEY (singerId, albumId)"));
op.waitFor();
List<Mutation> mutations = Arrays.asList(Mutation.newInsertBuilder("singers").set("singerId").to(1L).set("firstName").to("John").set("lastName").to("Lennon").build(), Mutation.newInsertBuilder("singers").set("singerId").to(2L).set("firstName").to("Paul").set("lastName").to("Mccartney").build(), Mutation.newInsertBuilder("singers").set("singerId").to(3L).set("firstName").to("George").set("lastName").to("Harrison").build(), Mutation.newInsertBuilder("singers").set("singerId").to(4L).set("firstName").to("Ringo").set("lastName").to("Starr").build(), Mutation.newInsertBuilder("albums").set("singerId").to(1L).set("albumId").to(1L).set("albumTitle").to("Imagine").build(), Mutation.newInsertBuilder("albums").set("singerId").to(2L).set("albumId").to(1L).set("albumTitle").to("Pipes of Peace").build());
DatabaseClient dbClient = getDbClient();
TransactionRunner runner = dbClient.readWriteTransaction();
runner.run(new TransactionRunner.TransactionCallable<Void>() {
@Nullable
@Override
public Void run(TransactionContext tx) {
tx.buffer(mutations);
return null;
}
});
}
Aggregations