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();
}
}
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));
}
}
}
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));
}
}
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);
}
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));
}
Aggregations