use of com.google.cloud.spanner.Statement in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method largeErrorText.
@Test
public void largeErrorText() {
String veryLongString = Joiner.on("").join(Iterables.limit(Iterables.cycle("x"), 8000));
Statement statement = Statement.newBuilder("SELECT REGEXP_CONTAINS(@value, @regexp)").bind("value").to("").bind("regexp").to("(" + veryLongString).build();
ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong()));
expectedException.expect(isSpannerException(ErrorCode.OUT_OF_RANGE));
expectedException.expectMessage("Cannot parse regular expression");
resultSet.next();
}
use of com.google.cloud.spanner.Statement in project YCSB by brianfrankcooper.
the class CloudSpannerClient method readUsingQuery.
private Status readUsingQuery(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
Statement query;
Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
if (fields == null || fields.size() == fieldCount) {
query = Statement.newBuilder(standardQuery).bind("key").to(key).build();
} else {
Joiner joiner = Joiner.on(',');
query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id=@key").bind("key").to(key).build();
}
try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
resultSet.next();
decodeStruct(columns, resultSet, result);
if (resultSet.next()) {
throw new Exception("Expected exactly one row for each read.");
}
return Status.OK;
} catch (Exception e) {
LOGGER.log(Level.INFO, "readUsingQuery()", e);
return Status.ERROR;
}
}
use of com.google.cloud.spanner.Statement in project YCSB by brianfrankcooper.
the class CloudSpannerClient method scanUsingQuery.
private Status scanUsingQuery(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
Statement query;
if (fields == null || fields.size() == fieldCount) {
query = Statement.newBuilder(standardScan).bind("startKey").to(startKey).bind("count").to(recordCount).build();
} else {
Joiner joiner = Joiner.on(',');
query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id>=@startKey LIMIT @count").bind("startKey").to(startKey).bind("count").to(recordCount).build();
}
try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
while (resultSet.next()) {
HashMap<String, ByteIterator> row = new HashMap<>();
decodeStruct(columns, resultSet, row);
result.add(row);
}
return Status.OK;
} catch (Exception e) {
LOGGER.log(Level.INFO, "scanUsingQuery()", e);
return Status.ERROR;
}
}
use of com.google.cloud.spanner.Statement 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.Statement 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();
}
Aggregations