| 40 | }); |
| 41 | |
| 42 | async function readDataWithIndex() { |
| 43 | // Gets a reference to a Cloud Spanner instance and database |
| 44 | const instance = spanner.instance(instanceId); |
| 45 | const database = instance.database(databaseId); |
| 46 | |
| 47 | const albumsTable = database.table('Albums'); |
| 48 | |
| 49 | const query = { |
| 50 | columns: ['AlbumId', 'AlbumTitle'], |
| 51 | keySet: { |
| 52 | all: true, |
| 53 | }, |
| 54 | index: 'AlbumsByAlbumTitle', |
| 55 | }; |
| 56 | |
| 57 | // Reads the Albums table using an index |
| 58 | try { |
| 59 | const [rows] = await albumsTable.read(query); |
| 60 | |
| 61 | rows.forEach(row => { |
| 62 | const json = row.toJSON(); |
| 63 | console.log(`AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}`); |
| 64 | }); |
| 65 | } catch (err) { |
| 66 | console.error('ERROR:', err); |
| 67 | } finally { |
| 68 | // Close the database when finished. |
| 69 | await database.close(); |
| 70 | } |
| 71 | } |
| 72 | readDataWithIndex(); |
| 73 | // [END spanner_read_data_with_index] |
| 74 | } |