|
45 | 45 | import com.google.common.io.BaseEncoding;
|
46 | 46 | import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
|
47 | 47 | import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
|
| 48 | +import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions; |
48 | 49 | import java.sql.Timestamp;
|
49 | 50 | import java.time.Instant;
|
50 | 51 | import java.util.ArrayList;
|
@@ -1433,6 +1434,45 @@ static void queryWithTimestampParameter(DatabaseClient dbClient) {
|
1433 | 1434 | }
|
1434 | 1435 | // [END spanner_query_with_timestamp_parameter]
|
1435 | 1436 |
|
| 1437 | + // [START spanner_create_client_with_query_options] |
| 1438 | + static void clientWithQueryOptions(DatabaseId db) { |
| 1439 | + SpannerOptions options = |
| 1440 | + SpannerOptions.newBuilder() |
| 1441 | + .setDefaultQueryOptions( |
| 1442 | + db, QueryOptions.newBuilder().setOptimizerVersion("latest").build()) |
| 1443 | + .build(); |
| 1444 | + Spanner spanner = options.getService(); |
| 1445 | + DatabaseClient dbClient = spanner.getDatabaseClient(db); |
| 1446 | + try (ResultSet resultSet = |
| 1447 | + dbClient |
| 1448 | + .singleUse() |
| 1449 | + .executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) { |
| 1450 | + while (resultSet.next()) { |
| 1451 | + System.out.printf( |
| 1452 | + "%d %d %s\n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2)); |
| 1453 | + } |
| 1454 | + } |
| 1455 | + } |
| 1456 | + // [END spanner_create_client_with_query_options] |
| 1457 | + |
| 1458 | + // [START spanner_query_with_query_options] |
| 1459 | + static void queryWithQueryOptions(DatabaseClient dbClient) { |
| 1460 | + try (ResultSet resultSet = |
| 1461 | + dbClient |
| 1462 | + .singleUse() |
| 1463 | + .executeQuery( |
| 1464 | + Statement |
| 1465 | + .newBuilder("SELECT SingerId, AlbumId, AlbumTitle FROM Albums") |
| 1466 | + .withQueryOptions(QueryOptions.newBuilder().setOptimizerVersion("latest").build()) |
| 1467 | + .build())) { |
| 1468 | + while (resultSet.next()) { |
| 1469 | + System.out.printf( |
| 1470 | + "%d %d %s\n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2)); |
| 1471 | + } |
| 1472 | + } |
| 1473 | + } |
| 1474 | + // [END spanner_query_with_query_options] |
| 1475 | + |
1436 | 1476 | static void run(
|
1437 | 1477 | DatabaseClient dbClient,
|
1438 | 1478 | DatabaseAdminClient dbAdminClient,
|
@@ -1589,6 +1629,12 @@ static void run(
|
1589 | 1629 | case "querywithtimestampparameter":
|
1590 | 1630 | queryWithTimestampParameter(dbClient);
|
1591 | 1631 | break;
|
| 1632 | + case "clientwithqueryoptions": |
| 1633 | + clientWithQueryOptions(database); |
| 1634 | + break; |
| 1635 | + case "querywithqueryoptions": |
| 1636 | + queryWithQueryOptions(dbClient); |
| 1637 | + break; |
1592 | 1638 | default:
|
1593 | 1639 | printUsageAndExit();
|
1594 | 1640 | }
|
@@ -1649,6 +1695,8 @@ static void printUsageAndExit() {
|
1649 | 1695 | System.err.println(" SpannerExample querywithint my-instance example-db");
|
1650 | 1696 | System.err.println(" SpannerExample querywithstring my-instance example-db");
|
1651 | 1697 | System.err.println(" SpannerExample querywithtimestampparameter my-instance example-db");
|
| 1698 | + System.err.println(" SpannerExample clientwithqueryoptions my-instance example-db"); |
| 1699 | + System.err.println(" SpannerExample querywithqueryoptions my-instance example-db"); |
1652 | 1700 | System.exit(1);
|
1653 | 1701 | }
|
1654 | 1702 |
|
|
0 commit comments