Guided Lab: Querying a Global Secondary Index

Description

A Global Secondary Index (GSI) in DynamoDB is an index with a partition key and a sort key that can be different from those on the base table. It allows for efficient querying of data based on different attributes. A global secondary index is considered ‘global’ because queries on the index can span all of the data in the base table, across all partitions. GSIs provide flexibility in querying data in various ways without impacting the performance of the base table. Additionally, a global secondary index has no size limitations and has its own provisioned throughput settings for read and write activity that is separate from those of the table, allowing for independent scaling and optimization.

Prerequisites

This lab assumes you have experience creating an Amazon DynamoDB table and are familiar with its basic components

If you find any gaps in your knowledge, consider taking the following labs:

  • Creating an Amazon DynamoDB table

Objectives

In this lab, you will learn how to:

  • Understand the concept of Global Secondary Indexes in DynamoDB.
  • Learn how to create a Global Secondary Index.
  • Learn how to query a Global Secondary Index.

Lab Steps

Creating the DynamoDB Table

1. Navigate to DynamoDB.

  • In the AWS Management Console, navigate to the DynamoDB service.

2. Create a DynamoDB table with the following configurations.

  • Table name: Movies
  • Partition key: MovieTitle (String)
  • Sort key: ReleaseYear (Number)

Scroll down, and click, “Create table”.

Creating and Querying a Global Secondary Index

3. Create a GSI:

  • You can create a GSI using the AWS Management Console by navigating to your Movies table, selecting the Indexes tab, and clicking on Create index.
  • Enter the following Index details for this lab:
    • Partition key: Genre (String)
    • Sort key: Rating (Number)
    • Index name: Genre-Rating-index

Scroll down, and click, “Create index”

  • It will take a few minutes to create the GSI.

4. Once done, select the created GSI, then click “Explore table items”.

5. Once you’re there, click the “Create Item” button.

6. Add Sample Data: Now, let’s add sample data to the ‘Movies’ table. You can do this one-by-one for each item.
To do this, after clicking the “Create item”, look for the “JSON view”, and click it. Then, Copy and Paste the sample data set below, and click, “Create item”.

{
  "MovieTitle": {
    "S": "The Dark Knight"
  },
  "ReleaseYear": {
    "N": "2008"
  },
  "Genre": {
    "S": "Action"
  },
  "Rating": {
    "N": "9.0"
  },
  "Director": {
    "S": "Christopher Nolan"
  }
}
{
  "MovieTitle": {
    "S": "Inception"
  },
  "ReleaseYear": {
    "N": "2010"
  },
  "Genre": {
    "S": "Sci-Fi"
  },
  "Rating": {
    "N": "8.8"
  },
  "Director": {
    "S": "Christopher Nolan"
  }
}
{
  "MovieTitle": {
    "S": "The Godfather"
  },
  "ReleaseYear": {
    "N": "1972"
  },
  "Genre": {
    "S": "Crime"
  },
  "Rating": {
    "N": "9.2"
  },
  "Director": {
    "S": "Francis Ford Coppola"
  }
}
{
  "MovieTitle": {
    "S": "The Shawshank Redemption"
  },
  "ReleaseYear": {
    "N": "1994"
  },
  "Genre": {
    "S": "Drama"
  },
  "Rating": {
    "N": "9.3"
  },
  "Director": {
    "S": "Frank Darabont"
  }
}
{
  "MovieTitle": {
    "S": "Pulp Fiction"
  },
  "ReleaseYear": {
    "N": "1995"
  },
  "Genre": {
    "S": "Crime"
  },
  "Rating": {
    "N": "8.9"
  },
  "Director": {
    "S": "Quentin Tarantino"
  }
}

Output:

7. Query the GSI: Now you can query the GenreRatingIndex GSI using the AWS Management Console.

To do this, scroll up to see the Scan or query items section. Select Query, and follow the examples below:

  • Example 1: Querying for Action Movies
    • Click on the “Select a table or index” dropdown and select “Genre-Rating-index”.
    • Enter Action into the Partition key field, and click “Run” to initiate the query.

This will return all movies in the Action genre, sorted by Rating in descending order, so the highest-rated action movie is returned first.

  • Example 2: Querying for Drama Movies
    • Enter Drama into the Partition key field, and click “Run” to initiate the query.

This will return all movies in the Drama genre, sorted by Rating in descending order, so the highest-rated drama movie is returned first.

Congratulations! You just successfully learned how to create and query a Global Secondary Index.

Skip to content