| 229 | } |
| 230 | |
| 231 | async function readData(instanceId, databaseId, projectId) { |
| 232 | // [START spanner_read_data] |
| 233 | // Imports the Google Cloud client library |
| 234 | const {Spanner} = require('@google-cloud/spanner'); |
| 235 | |
| 236 | /** |
| 237 | * TODO(developer): Uncomment the following lines before running the sample. |
| 238 | */ |
| 239 | // const projectId = 'my-project-id'; |
| 240 | // const instanceId = 'my-instance'; |
| 241 | // const databaseId = 'my-database'; |
| 242 | |
| 243 | // Creates a client |
| 244 | const spanner = new Spanner({ |
| 245 | projectId: projectId, |
| 246 | }); |
| 247 | |
| 248 | // Gets a reference to a Cloud Spanner instance and database |
| 249 | const instance = spanner.instance(instanceId); |
| 250 | const database = instance.database(databaseId); |
| 251 | |
| 252 | // Reads rows from the Albums table |
| 253 | const albumsTable = database.table('Albums'); |
| 254 | |
| 255 | const query = { |
| 256 | columns: ['SingerId', 'AlbumId', 'AlbumTitle'], |
| 257 | keySet: { |
| 258 | all: true, |
| 259 | }, |
| 260 | }; |
| 261 | |
| 262 | try { |
| 263 | const [rows] = await albumsTable.read(query); |
| 264 | |
| 265 | rows.forEach(row => { |
| 266 | const json = row.toJSON(); |
| 267 | console.log( |
| 268 | `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}` |
| 269 | ); |
| 270 | }); |
| 271 | } catch (err) { |
| 272 | console.error('ERROR:', err); |
| 273 | } finally { |
| 274 | // Close the database when finished. |
| 275 | await database.close(); |
| 276 | } |
| 277 | // [END spanner_read_data] |
| 278 | } |
| 279 | |
| 280 | async function readStaleData(instanceId, databaseId, projectId) { |
| 281 | // [START spanner_read_stale_data] |