use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class ITWriteTest method writeBytesRandom.
@Test
public void writeBytesRandom() {
// Pseudo-random test for byte encoding. We explicitly set a random seed so that multiple
// test runs cover more data, but any failing test run can be reproduced easily.
Random rnd = new Random();
long seed = rnd.nextLong();
rnd.setSeed(seed);
Map<String, ByteArray> expected = new HashMap<>();
boolean pass = false;
try {
for (int length : new int[] { 1, 2, 5, 11 }) {
byte[] data = new byte[length];
for (int i = 0; i < 3; ++i) {
rnd.nextBytes(data);
String key = uniqueString();
ByteArray value = ByteArray.copyFrom(data);
expected.put(key, value);
write(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).set("BytesValue").to(value).build());
}
}
KeySet.Builder keys = KeySet.newBuilder();
for (String key : expected.keySet()) {
keys.addKey(Key.of(key));
}
ResultSet resultSet = client.singleUse(TimestampBound.strong()).read("T", keys.build(), Arrays.asList("K", "BytesValue"));
while (resultSet.next()) {
String key = resultSet.getString(0);
ByteArray value = resultSet.getBytes(1);
assertThat(expected).containsKey(key);
ByteArray expectedValue = expected.remove(key);
assertThat(value).isEqualTo(expectedValue);
}
assertThat(expected).isEmpty();
pass = true;
} finally {
if (!pass) {
System.out.println("To reproduce failure, use seed " + seed);
}
}
}
use of com.google.cloud.spanner.ResultSet 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();
}
use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method analyzePlan.
@Test
public void analyzePlan() {
Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2");
ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PLAN);
assertThat(resultSet.next()).isFalse();
assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64())));
ResultSetStats receivedStats = resultSet.getStats();
assertThat(receivedStats).isNotNull();
assertThat(receivedStats.hasQueryPlan()).isTrue();
assertThat(receivedStats.hasQueryStats()).isFalse();
}
use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method analyzeProfile.
@Test
public void analyzeProfile() {
Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2 AS column ORDER BY column");
ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PROFILE);
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64())));
assertThat(resultSet.getLong(0)).isEqualTo(1);
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getLong(0)).isEqualTo(2);
assertThat(resultSet.next()).isFalse();
ResultSetStats receivedStats = resultSet.getStats();
assertThat(receivedStats).isNotNull();
assertThat(receivedStats.hasQueryPlan()).isTrue();
assertThat(receivedStats.hasQueryStats()).isTrue();
}
use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method unboundParameter.
@Test
public void unboundParameter() {
ResultSet resultSet = Statement.of("SELECT @v").executeQuery(client.singleUse(TimestampBound.strong()));
expectedException.expect(isSpannerException(ErrorCode.INVALID_ARGUMENT));
expectedException.expectMessage("No parameter found for binding: v");
resultSet.next();
}
Aggregations