Search in sources :

Example 1 with Statement

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();
}
Also used : Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 2 with Statement

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;
    }
}
Also used : Joiner(com.google.common.base.Joiner) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

Example 3 with Statement

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;
    }
}
Also used : Joiner(com.google.common.base.Joiner) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

Example 4 with Statement

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();
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Statement(com.google.cloud.spanner.Statement) Database(com.google.cloud.spanner.Database) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 5 with Statement

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();
}
Also used : ResultSetStats(com.google.spanner.v1.ResultSetStats) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)6 Statement (com.google.cloud.spanner.Statement)6 IntegrationTest (com.google.cloud.spanner.IntegrationTest)4 Test (org.junit.Test)4 Joiner (com.google.common.base.Joiner)2 ResultSetStats (com.google.spanner.v1.ResultSetStats)2 DBException (com.yahoo.ycsb.DBException)2 Database (com.google.cloud.spanner.Database)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 StringByteIterator (com.yahoo.ycsb.StringByteIterator)1 HashMap (java.util.HashMap)1