private void validate(ResultSet resultSet, BitSet seenRows) { while (resultSet.next()) { assertThat(seenRows.get((int) resultSet.getLong(0))).isFalse(); seenRows.set((int) resultSet.getLong(0)); ByteArray data = resultSet.getBytes(1); assertThat(data.length()).isEqualTo(resultSet.getLong(3)); assertThat(resultSet.getLong(2)).isEqualTo(hasher.hashBytes(data.toByteArray()).asLong()); } } }
assertThat(rs.getCurrentRowAsStruct()).isEqualTo(struct1); assertThat(rs.getString(0)).isEqualTo("x"); assertThat(rs.getLong(1)).isEqualTo(2L); assertThat(rs.getBoolean(2)).isTrue(); assertThat(rs.getBoolean("f3")).isTrue(); assertThat(rs.getCurrentRowAsStruct()).isEqualTo(struct2); assertThat(rs.getString(0)).isEqualTo("y"); assertThat(rs.getLong(1)).isEqualTo(3L); assertThat(rs.isNull(2)).isTrue(); assertThat(rs.next()).isFalse();
private void validate(ResultSet resultSet) { int i = 0; while (resultSet.next()) { assertThat(resultSet.getLong(0)).isEqualTo(i); ByteArray data = resultSet.getBytes(1); assertThat(data.length()).isEqualTo(resultSet.getLong(3)); assertThat(resultSet.getLong(2)).isEqualTo(hasher.hashBytes(data.toByteArray()).asLong()); assertThat(++i).isAtMost(numRows); } assertThat(i).isEqualTo(numRows); } }
void partitionRead() { // [START partition_read] final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); List<Partition> partitions = txn.partitionRead( PartitionOptions.getDefaultInstance(), "Singers", KeySet.all(), Arrays.asList("SingerId", "FirstName", "LastName")); for (final Partition p : partitions) { try (ResultSet results = txn.execute(p)) { while (results.next()) { long singerId = results.getLong(0); String firstName = results.getString(1); String lastName = results.getString(2); System.out.println("[" + singerId + "] " + firstName + " " + lastName); } } } // [END partition_read] }
void partitionReadUsingIndex() { // [START partition_read_using_index] final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); List<Partition> partitions = txn.partitionReadUsingIndex( PartitionOptions.getDefaultInstance(), "Singers", "SingerId", KeySet.all(), Arrays.asList("SingerId", "FirstName", "LastName")); for (Partition p : partitions) { try (ResultSet results = txn.execute(p)) { while (results.next()) { long singerId = results.getLong(0); String firstName = results.getString(1); String lastName = results.getString(2); System.out.println("[" + singerId + "] " + firstName + " " + lastName); } } } // [END partition_read_using_index] } }
System.out.printf("%d\n\n", resultSet.getLong(0));
void partitionQuery() { // [START partition_query] final BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); List<Partition> partitions = txn.partitionQuery( PartitionOptions.getDefaultInstance(), Statement.of("SELECT SingerId, FirstName, LastName FROM Singers")); for (final Partition p : partitions) { try (ResultSet results = txn.execute(p)) { while (results.next()) { long singerId = results.getLong(0); String firstName = results.getString(1); String lastName = results.getString(2); System.out.println("[" + singerId + "] " + firstName + " " + lastName); } } } // [END partition_query] }
@Test public void analyzeProfile() { Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2 AS column ORDER BY column"); ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PROFILE); assertThat(resultSet.next()).isTrue(); assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64()))); assertThat(resultSet.getLong(0)).isEqualTo(1); assertThat(resultSet.next()).isTrue(); assertThat(resultSet.getLong(0)).isEqualTo(2); assertThat(resultSet.next()).isFalse(); ResultSetStats receivedStats = resultSet.getStats(); assertThat(receivedStats).isNotNull(); assertThat(receivedStats.hasQueryPlan()).isTrue(); assertThat(receivedStats.hasQueryStats()).isTrue(); }
static void readStaleData(DatabaseClient dbClient) { ResultSet resultSet = dbClient .singleUse(TimestampBound.ofExactStaleness(15, TimeUnit.SECONDS)) .read("Albums", KeySet.all(), Arrays.asList("SingerId", "AlbumId", "MarketingBudget")); while (resultSet.next()) { System.out.printf( "%d %d %s\n", resultSet.getLong(0), resultSet.getLong(1), resultSet.isNull(2) ? "NULL" : resultSet.getLong("MarketingBudget")); } } // [END spanner_read_stale_data]
static void queryMarketingBudget(DatabaseClient dbClient) { // Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to // null. ResultSet resultSet = dbClient .singleUse() .executeQuery(Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums")); while (resultSet.next()) { System.out.printf( "%d %d %s\n", resultSet.getLong("SingerId"), resultSet.getLong("AlbumId"), // We check that the value is non null. ResultSet getters can only be used to retrieve // non null values. resultSet.isNull("MarketingBudget") ? "NULL" : resultSet.getLong("MarketingBudget")); } } // [END spanner_query_data_with_new_column]
static void query(DatabaseClient dbClient) { // singleUse() can be used to execute a single read or query against Cloud Spanner. ResultSet resultSet = dbClient .singleUse() .executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums")); while (resultSet.next()) { System.out.printf( "%d %d %s\n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2)); } } // [END spanner_query_data]
static void read(DatabaseClient dbClient) { ResultSet resultSet = dbClient .singleUse() .read( "Albums", // KeySet.all() can be used to read all rows in a table. KeySet exposes other // methods to read only a subset of the table. KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle")); while (resultSet.next()) { System.out.printf( "%d %d %s\n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2)); } } // [END spanner_read_data]
static void queryMarketingBudgetWithTimestamp(DatabaseClient dbClient) { // Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to // null. ResultSet resultSet = dbClient .singleUse() .executeQuery( Statement.of( "SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums" + " ORDER BY LastUpdateTime DESC")); while (resultSet.next()) { System.out.printf( "%d %d %s %s\n", resultSet.getLong("SingerId"), resultSet.getLong("AlbumId"), // We check that the value is non null. ResultSet getters can only be used to retrieve // non null values. resultSet.isNull("MarketingBudget") ? "NULL" : resultSet.getLong("MarketingBudget"), resultSet.isNull("LastUpdateTime") ? "NULL" : resultSet.getTimestamp("LastUpdateTime")); } } // [END spanner_query_data_with_timestamp_column]
static void queryPerformancesTable(DatabaseClient dbClient) { // Rows without an explicit value for Revenue will have a Revenue equal to // null. ResultSet resultSet = dbClient .singleUse() .executeQuery( Statement.of( "SELECT SingerId, VenueId, EventDate, Revenue, LastUpdateTime " + "FROM Performances ORDER BY LastUpdateTime DESC")); while (resultSet.next()) { System.out.printf( "%d %d %s %s %s\n", resultSet.getLong("SingerId"), resultSet.getLong("VenueId"), resultSet.getDate("EventDate"), // We check that the value is non null. ResultSet getters can only be used to retrieve // non null values. resultSet.isNull("Revenue") ? "NULL" : resultSet.getLong("Revenue"), resultSet.getTimestamp("LastUpdateTime")); } }
static void readStoringIndex(DatabaseClient dbClient) { // We can read MarketingBudget also from the index since it stores a copy of MarketingBudget. ResultSet resultSet = dbClient .singleUse() .readUsingIndex( "Albums", "AlbumsByAlbumTitle2", KeySet.all(), Arrays.asList("AlbumId", "AlbumTitle", "MarketingBudget")); while (resultSet.next()) { System.out.printf( "%d %s %s\n", resultSet.getLong(0), resultSet.getString(1), resultSet.isNull("MarketingBudget") ? "NULL" : resultSet.getLong("MarketingBudget")); } } // [END spanner_read_data_with_storing_index]
static void readUsingIndex(DatabaseClient dbClient) { ResultSet resultSet = dbClient .singleUse() .readUsingIndex( "Albums", "AlbumsByAlbumTitle", KeySet.all(), Arrays.asList("AlbumId", "AlbumTitle")); while (resultSet.next()) { System.out.printf("%d %s\n", resultSet.getLong(0), resultSet.getString(1)); } } // [END spanner_read_data_with_index]
static void querySingersTable(DatabaseClient dbClient) { ResultSet resultSet = dbClient .singleUse() .executeQuery( Statement.of( "SELECT SingerId, FirstName, LastName FROM Singers")); while (resultSet.next()) { System.out.printf( "%s %s %s\n", resultSet.getLong("SingerId"), resultSet.getString("FirstName"), resultSet.getString("LastName")); } }
static void queryStructField(DatabaseClient dbClient) { Statement s = Statement.newBuilder("SELECT SingerId FROM Singers WHERE FirstName = @name.FirstName") .bind("name") .to( Struct.newBuilder() .set("FirstName") .to("Elena") .set("LastName") .to("Campbell") .build()) .build(); ResultSet resultSet = dbClient.singleUse().executeQuery(s); while (resultSet.next()) { System.out.printf("%d\n", resultSet.getLong("SingerId")); } } // [END spanner_field_access_on_struct_parameters]
static void queryWithStruct(DatabaseClient dbClient) { // [START spanner_create_struct_with_data] Struct name = Struct.newBuilder().set("FirstName").to("Elena").set("LastName").to("Campbell").build(); // [END spanner_create_struct_with_data] // [START spanner_query_data_with_struct] Statement s = Statement.newBuilder( "SELECT SingerId FROM Singers " + "WHERE STRUCT<FirstName STRING, LastName STRING>(FirstName, LastName) " + "= @name") .bind("name") .to(name) .build(); ResultSet resultSet = dbClient.singleUse().executeQuery(s); while (resultSet.next()) { System.out.printf("%d\n", resultSet.getLong("SingerId")); } // [END spanner_query_data_with_struct] }