Step 7: Query the global secondary index - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Step 7: Query the global secondary index

In this step, you query a global secondary index on the Music table using the Amazon DynamoDB console or the Amazon CLI.

For more information about global secondary indexes, see Using Global Secondary Indexes in DynamoDB.

Follow these steps to use the DynamoDB console to query data through the AlbumTitle-index global secondary index.

  1. Open the DynamoDB console at https://console.amazonaws.cn/dynamodb/.

  2. In the navigation pane on the left side of the console, choose Tables.

  3. Choose the Music table from the table list.

  4. Select the View items.

  5. Choose Query.

  6. In the drop-down list under Query, choose AlbumTitle-index.

    For AlbumTitle, enter Somewhat Famous, and then choose Run.

The following Amazon CLI example queries a global secondary index AlbumTitle-index on the Music table. You can do this either through the DynamoDB API or PartiQL, a SQL-compatible query language for DynamoDB.

DynamoDB API

You query the global secondary index through the DynamoDB API by using query and providing the index name.

aws dynamodb query \ --table-name Music \ --index-name AlbumTitle-index \ --key-condition-expression "AlbumTitle = :name" \ --expression-attribute-values '{":name":{"S":"Somewhat Famous"}}'

Using query returns the following sample result.

{ "Items": [ { "AlbumTitle": { "S": "Somewhat Famous" }, "Awards": { "S": "1" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } }, { "AlbumTitle": { "S": "Somewhat Famous" }, "Awards": { "N": "2" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Howdy" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": null }
PartiQL for DynamoDB

You query the global secondary index through PartiQL by using the Select statement and providing the index name.

Note

You'll need to escape the double quotes around Music and AlbumTitle-index since you're doing this through the CLI.

aws dynamodb execute-statement --statement "SELECT * FROM \"Music\".\"AlbumTitle-index\" \ WHERE AlbumTitle='Somewhat Famous'"

Using the Select statement in this way returns the following sample result.

{ "Items": [ { "AlbumTitle": { "S": "Somewhat Famous" }, "Awards": { "S": "1" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } }, { "AlbumTitle": { "S": "Somewhat Famous" }, "Awards": { "S": "2" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Howdy" } } ] }

For more information about querying data with PartiQL, see PartiQL select statements.