use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplate method count.
@Override
public long count(Class entityClass) {
SpannerPersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entityClass);
Statement statement = Statement.of(String.format("select count(*) from %s", persistentEntity.tableName()));
try (ResultSet resultSet = executeQuery(statement, null)) {
resultSet.next();
return resultSet.getLong(0);
}
}
use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class DatabaseSelect 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
try (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 google-cloud-java by GoogleCloudPlatform.
the class ReadContextSnippets method executeQuery.
ResultSet executeQuery() {
// [START read_context_execute_query]
// Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
// null.
ReadContext readContext = dbClient.singleUse();
ResultSet resultSet = readContext.executeQuery(Statement.of("SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
// [END read_context_execute_query]
return resultSet;
}
use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.
the class SpannerBenchWrapperImpl method read.
public void read(ReadQuery request, StreamObserver<EmptyResponse> responseObserver) {
System.out.println("read has been called");
try (ReadOnlyTransaction transaction = dbClient.readOnlyTransaction()) {
ResultSet resultSet = transaction.executeQuery(Statement.of(request.getQuery()));
while (resultSet.next()) {
// Do nothing with the data.
}
}
EmptyResponse reply = EmptyResponse.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
use of com.google.cloud.spanner.ResultSet in project beam by apache.
the class SpannerWriteIT method countNumberOfRecords.
private long countNumberOfRecords() {
ResultSet resultSet = spanner.getDatabaseClient(DatabaseId.of(project, options.getInstanceId(), databaseName)).singleUse().executeQuery(Statement.of("SELECT COUNT(*) FROM " + options.getTable()));
assertThat(resultSet.next(), is(true));
long result = resultSet.getLong(0);
assertThat(resultSet.next(), is(false));
return result;
}
Aggregations