Search in sources :

Example 46 with ResultSet

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: QuickStartSample <instance_id> <database_id>");
        return;
    }
    // Instantiates a client
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    // Name of your instance & database.
    String instanceId = args[0];
    String databaseId = args[1];
    try {
        // Creates a database client
        DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
        // Queries the database
        ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
        System.out.println("\n\nResults:");
        // Prints the results
        while (resultSet.next()) {
            System.out.printf("%d\n\n", resultSet.getLong(0));
        }
    } finally {
        // Closes the client which will free up the resources used
        spanner.close();
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ResultSet(com.google.cloud.spanner.ResultSet) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 47 with ResultSet

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

the class SpannerSample method readOnlyTransaction.

// [END spanner_read_data_with_storing_index]
// [START spanner_read_only_transaction]
static void readOnlyTransaction(DatabaseClient dbClient) {
    // We use a try-with-resource block to automatically do so.
    try (ReadOnlyTransaction transaction = dbClient.readOnlyTransaction()) {
        ResultSet queryResultSet = transaction.executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"));
        while (queryResultSet.next()) {
            System.out.printf("%d %d %s\n", queryResultSet.getLong(0), queryResultSet.getLong(1), queryResultSet.getString(2));
        }
        ResultSet readResultSet = transaction.read("Albums", KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
        while (readResultSet.next()) {
            System.out.printf("%d %d %s\n", readResultSet.getLong(0), readResultSet.getLong(1), readResultSet.getString(2));
        }
    }
}
Also used : ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 48 with ResultSet

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

the class SpannerWriteIT method testEndToEnd.

@Test
public void testEndToEnd() {
    SpannerWrite.main(new String[] { "--instanceId=" + instanceId, "--databaseId=" + databaseId, "--singersFilename=" + singersPath, "--albumsFilename=" + albumsPath, "--runner=DirectRunner" });
    DatabaseClient dbClient = getDbClient();
    try (ReadContext context = dbClient.singleUse()) {
        ResultSet rs = context.executeQuery(Statement.of("SELECT COUNT(*) FROM singers"));
        assertTrue(rs.next());
        assertEquals(4, rs.getLong(0));
    }
    try (ReadContext context = dbClient.singleUse()) {
        ResultSet rs = context.executeQuery(Statement.of("SELECT COUNT(*) FROM albums"));
        assertTrue(rs.next());
        assertEquals(3, rs.getLong(0));
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) Test(org.junit.Test)

Example 49 with ResultSet

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

the class SpannerObjectMapperImplTests method mapToListPartialColumnsTest.

@Test
public void mapToListPartialColumnsTest() {
    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("doubleList", Value.float64Array(doubleList)).add("stringList", Value.stringArray(stringList)).build();
    Struct struct2 = Struct.newBuilder().add("id", Value.string("key2")).add("custom_col", Value.string("string2")).add("doubleList", Value.float64Array(doubleList)).add("stringList", Value.stringArray(stringList)).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, "id", "custom_col");
    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);
    // This should not have been set
    assertNull(t1.doubleList);
    assertEquals("key2", t2.id);
    assertEquals("string2", t2.stringField);
    // This should not have been set
    assertNull(t2.stringList);
}
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 50 with ResultSet

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

the class SpannerTemplateTests method findByStatementTest.

@Test
public void findByStatementTest() {
    ResultSet results = mock(ResultSet.class);
    QueryOption queryOption = mock(QueryOption.class);
    Statement statement = Statement.of("test");
    SpannerQueryOptions options = new SpannerQueryOptions().addQueryOption(queryOption);
    when(this.readContext.executeQuery(any(), any())).thenReturn(results);
    this.spannerTemplate.find(TestEntity.class, statement, options);
    verify(this.objectMapper, times(1)).mapToList(same(results), eq(TestEntity.class));
    verify(this.readContext, times(1)).executeQuery(same(statement), same(queryOption));
}
Also used : Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) QueryOption(com.google.cloud.spanner.Options.QueryOption) Test(org.junit.Test)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)71 Test (org.junit.Test)25 Statement (com.google.cloud.spanner.Statement)15 Struct (com.google.cloud.spanner.Struct)12 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ReadContext (com.google.cloud.spanner.ReadContext)10 DatabaseClient (com.google.cloud.spanner.DatabaseClient)8 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Mutation (com.google.cloud.spanner.Mutation)5 Partition (com.google.cloud.spanner.Partition)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 HashMap (java.util.HashMap)5 Key (com.google.cloud.spanner.Key)4 ReadOption (com.google.cloud.spanner.Options.ReadOption)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Joiner (com.google.common.base.Joiner)4