Search in sources :

Example 51 with ResultSet

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

Example 52 with ResultSet

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();
    }
}
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 53 with ResultSet

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;
}
Also used : ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet)

Example 54 with 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();
}
Also used : ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 55 with ResultSet

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

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