Search in sources :

Example 1 with ResultSet

use of com.google.cloud.spanner.ResultSet in project YCSB by brianfrankcooper.

the class CloudSpannerClient method scan.

@Override
public Status scan(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    if (queriesForReads) {
        return scanUsingQuery(table, startKey, recordCount, fields, result);
    }
    Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
    KeySet keySet = KeySet.newBuilder().addRange(KeyRange.closedClosed(Key.of(startKey), Key.of())).build();
    try (ResultSet resultSet = dbClient.singleUse(timestampBound).read(table, keySet, columns, Options.limit(recordCount))) {
        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, "scan()", e);
        return Status.ERROR;
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

Example 2 with ResultSet

use of com.google.cloud.spanner.ResultSet 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 3 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method rowsAreSnapshots.

@Test
public void rowsAreSnapshots() {
    List<Struct> rows = new ArrayList<>();
    ResultSet resultSet = client.singleUse(TimestampBound.strong()).read(TABLE_NAME, KeySet.newBuilder().addKey(Key.of("k2")).addKey(Key.of("k3")).addKey(Key.of("k4")).build(), ALL_COLUMNS);
    while (resultSet.next()) {
        rows.add(resultSet.getCurrentRowAsStruct());
    }
    assertThat(rows.size()).isEqualTo(3);
    assertThat(rows.get(0).getString(0)).isEqualTo("k2");
    assertThat(rows.get(0).getString(1)).isEqualTo("v2");
    assertThat(rows.get(1).getString(0)).isEqualTo("k3");
    assertThat(rows.get(1).getString(1)).isEqualTo("v3");
    assertThat(rows.get(2).getString(0)).isEqualTo("k4");
    assertThat(rows.get(2).getString(1)).isEqualTo("v4");
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 4 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerObjectMapperImplTests method mapToListTest.

@Test
public void mapToListTest() {
    List<Double> doubleList = new ArrayList<>();
    doubleList.add(3.33);
    List<String> stringList = new ArrayList<>();
    stringList.add("string");
    Struct struct1 = Struct.newBuilder().add("id", Value.string("key1")).add("custom_col", Value.string("string1")).add("booleanField", Value.bool(true)).add("intField", Value.int64(123L)).add("longField", Value.int64(3L)).add("doubleField", Value.float64(3.33)).add("doubleArray", Value.float64Array(new double[] { 3.33, 3.33, 3.33 })).add("doubleList", Value.float64Array(doubleList)).add("stringList", Value.stringArray(stringList)).add("booleanList", Value.boolArray(new boolean[] {})).add("longList", Value.int64Array(new long[] {})).add("timestampList", Value.timestampArray(new ArrayList<>())).add("dateList", Value.dateArray(new ArrayList<>())).add("bytesList", Value.bytesArray(new ArrayList<>())).add("dateField", Value.date(Date.fromYearMonthDay(2018, 11, 22))).add("timestampField", Value.timestamp(Timestamp.ofTimeMicroseconds(333))).add("bytes", Value.bytes(ByteArray.copyFrom("string1"))).build();
    Struct struct2 = Struct.newBuilder().add("id", Value.string("key2")).add("custom_col", Value.string("string2")).add("booleanField", Value.bool(true)).add("intField", Value.int64(222L)).add("longField", Value.int64(5L)).add("doubleField", Value.float64(5.55)).add("doubleArray", Value.float64Array(new double[] { 5.55, 5.55 })).add("doubleList", Value.float64Array(doubleList)).add("stringList", Value.stringArray(stringList)).add("booleanList", Value.boolArray(new boolean[] {})).add("longList", Value.int64Array(new long[] {})).add("timestampList", Value.timestampArray(new ArrayList<>())).add("dateList", Value.dateArray(new ArrayList<>())).add("bytesList", Value.bytesArray(new ArrayList<>())).add("dateField", Value.date(Date.fromYearMonthDay(2019, 11, 22))).add("timestampField", Value.timestamp(Timestamp.ofTimeMicroseconds(555))).add("bytes", Value.bytes(ByteArray.copyFrom("string2"))).build();
    MockResults mockResults = new MockResults();
    mockResults.structs = Arrays.asList(struct1, struct2);
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenAnswer(invocation -> mockResults.next());
    when(results.getCurrentRowAsStruct()).thenAnswer(invocation -> mockResults.getCurrent());
    List<TestEntity> entities = this.objectMapper.mapToList(results, TestEntity.class);
    verify(results, times(1)).close();
    assertEquals(2, entities.size());
    TestEntity t1 = entities.get(0);
    TestEntity t2 = entities.get(1);
    assertEquals("key1", t1.id);
    assertEquals("string1", t1.stringField);
    assertEquals(true, t1.booleanField);
    assertEquals(123, t1.intField);
    assertEquals(3L, t1.longField);
    assertEquals(3.33, t1.doubleField, 0.00001);
    assertEquals(3, t1.doubleArray.length);
    assertEquals(2018, t1.dateField.getYear());
    assertEquals(ByteArray.copyFrom("string1"), t1.bytes);
    assertEquals("key2", t2.id);
    assertEquals("string2", t2.stringField);
    assertEquals(true, t2.booleanField);
    assertEquals(222, t2.intField);
    assertEquals(5L, t2.longField);
    assertEquals(5.55, t2.doubleField, 0.00001);
    assertEquals(2, t2.doubleArray.length);
    assertEquals(2019, t2.dateField.getYear());
    assertEquals(1, t2.doubleList.size());
    assertEquals(3.33, t2.doubleList.get(0), 0.000001);
    assertThat(t2.stringList, containsInAnyOrder("string"));
    assertEquals(ByteArray.copyFrom("string2"), t2.bytes);
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.google.cloud.spanner.ResultSet) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 5 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method findMultipleKeysWithIndexTest.

@Test
public void findMultipleKeysWithIndexTest() {
    ResultSet results = mock(ResultSet.class);
    ReadOption readOption = mock(ReadOption.class);
    SpannerReadOptions options = new SpannerReadOptions().addReadOption(readOption).setIndex("index");
    KeySet keySet = KeySet.singleKey(Key.of("key"));
    when(this.readContext.readUsingIndex(any(), any(), any(), any(), any())).thenReturn(results);
    this.spannerTemplate.find(TestEntity.class, keySet, options);
    verify(this.objectMapper, times(1)).mapToList(same(results), eq(TestEntity.class));
    verify(this.readContext, times(1)).readUsingIndex(eq("custom_test_table"), eq("index"), same(keySet), any(), same(readOption));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) ReadOption(com.google.cloud.spanner.Options.ReadOption) Test(org.junit.Test)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)58 Test (org.junit.Test)19 Statement (com.google.cloud.spanner.Statement)12 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ArrayList (java.util.ArrayList)8 DatabaseClient (com.google.cloud.spanner.DatabaseClient)7 Struct (com.google.cloud.spanner.Struct)7 KeySet (com.google.cloud.spanner.KeySet)6 ReadContext (com.google.cloud.spanner.ReadContext)6 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Partition (com.google.cloud.spanner.Partition)5 HashMap (java.util.HashMap)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 Joiner (com.google.common.base.Joiner)4 Mutation (com.google.cloud.spanner.Mutation)3 ResultSetStats (com.google.spanner.v1.ResultSetStats)3 DBException (com.yahoo.ycsb.DBException)3 DBException (site.ycsb.DBException)3