| 73 | } |
| 74 | |
| 75 | async function queryWithQueryOptions(instanceId, databaseId, projectId) { |
| 76 | // [START spanner_query_with_query_options] |
| 77 | // Imports the Google Cloud client library |
| 78 | const {Spanner} = require('@google-cloud/spanner'); |
| 79 | |
| 80 | /** |
| 81 | * TODO(developer): Uncomment the following lines before running the sample. |
| 82 | */ |
| 83 | // const projectId = 'my-project-id'; |
| 84 | // const instanceId = 'my-instance'; |
| 85 | // const databaseId = 'my-database'; |
| 86 | |
| 87 | // Creates a client |
| 88 | const spanner = new Spanner({ |
| 89 | projectId: projectId, |
| 90 | }); |
| 91 | let database; |
| 92 | try { |
| 93 | // Gets a reference to a Cloud Spanner instance and database |
| 94 | const instance = spanner.instance(instanceId); |
| 95 | database = instance.database(databaseId); |
| 96 | |
| 97 | const query = { |
| 98 | sql: `SELECT AlbumId, AlbumTitle, MarketingBudget |
| 99 | FROM Albums |
| 100 | ORDER BY AlbumTitle`, |
| 101 | queryOptions: { |
| 102 | optimizerVersion: 'latest', |
| 103 | // The list of available statistics packages can be found by querying the |
| 104 | // "INFORMATION_SCHEMA.SPANNER_STATISTICS" table. |
| 105 | optimizerStatisticsPackage: 'latest', |
| 106 | }, |
| 107 | }; |
| 108 | |
| 109 | // Queries rows from the Albums table |
| 110 | const [rows] = await database.run(query); |
| 111 | |
| 112 | rows.forEach(row => { |
| 113 | const json = row.toJSON(); |
| 114 | const marketingBudget = json.MarketingBudget |
| 115 | ? json.MarketingBudget |
| 116 | : null; // This value is nullable |
| 117 | console.log( |
| 118 | `AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}, MarketingBudget: ${marketingBudget}` |
| 119 | ); |
| 120 | }); |
| 121 | } catch (err) { |
| 122 | console.error('ERROR:', err); |
| 123 | } finally { |
| 124 | // Close the database when finished. |
| 125 | await database.close(); |
| 126 | } |
| 127 | // [END spanner_query_with_query_options] |
| 128 | } |
| 129 | |
| 130 | require('yargs') |
| 131 | .demandCommand(1) |