use of com.google.cloud.spanner.DatabaseClient in project beam by apache.
the class SpannerWriteIT method testWrite.
@Test
public void testWrite() throws Exception {
p.apply(GenerateSequence.from(0).to(100)).apply(ParDo.of(new GenerateMutations(options.getTable()))).apply(SpannerIO.write().withProjectId(options.getProjectId()).withInstanceId(options.getInstanceId()).withDatabaseId(options.getDatabaseId()));
p.run();
DatabaseClient databaseClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), options.getInstanceId(), options.getDatabaseId()));
ResultSet resultSet = databaseClient.singleUse().executeQuery(Statement.of("SELECT COUNT(*) FROM " + options.getTable()));
assertThat(resultSet.next(), is(true));
assertThat(resultSet.getLong(0), equalTo(100L));
assertThat(resultSet.next(), is(false));
}
use of com.google.cloud.spanner.DatabaseClient 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.DatabaseClient in project google-cloud-java by GoogleCloudPlatform.
the class ITReadTest method invalidDatabase.
@Test
public void invalidDatabase() {
RemoteSpannerHelper helper = env.getTestHelper();
DatabaseClient invalidClient = helper.getClient().getDatabaseClient(DatabaseId.of(helper.getInstanceId(), "invalid"));
expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
invalidClient.singleUse(TimestampBound.strong()).readRow(TABLE_NAME, Key.of("k99"), ALL_COLUMNS);
}
use of com.google.cloud.spanner.DatabaseClient in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method queryRealTable.
@Test
public void queryRealTable() {
Database populatedDb = env.getTestHelper().createTestDatabase("CREATE TABLE T ( K STRING(MAX) NOT NULL, V STRING(MAX) ) PRIMARY KEY (K)");
DatabaseClient client = env.getTestHelper().getDatabaseClient(populatedDb);
client.writeAtLeastOnce(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to("k1").set("V").to("v1").build(), Mutation.newInsertBuilder("T").set("K").to("k2").set("V").to("v2").build(), Mutation.newInsertBuilder("T").set("K").to("k3").set("V").to("v3").build(), Mutation.newInsertBuilder("T").set("K").to("k4").set("V").to("v4").build()));
Statement statement = Statement.newBuilder("SELECT K, V FROM T WHERE K >= @min AND K < @max ORDER BY K ASC").bind("min").to("k13").bind("max").to("k32").build();
ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong()));
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("K", Type.string()), StructField.of("V", Type.string())));
assertThat(resultSet.getString(0)).isEqualTo("k2");
assertThat(resultSet.getString(1)).isEqualTo("v2");
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getString("K")).isEqualTo("k3");
assertThat(resultSet.getString("V")).isEqualTo("v3");
assertThat(resultSet.next()).isFalse();
}
Aggregations