Ends in
00
days
00
hrs
00
mins
00
secs
SHOP NOW

🎆NEW YEAR SALE EXTENSION - Practice exams as low as $9.74 USD & eBooks as low as $2.99 USD

Guided Lab: Querying a Local Secondary Index

Description

A Local Secondary Index (LSI) in DynamoDB is an index that has the same partition key as the base table, but a different sort key. It’s “local” in the sense that every partition of a local secondary index is scoped to a base table partition that has the same partition key value.

One important thing to note about LSIs is that they must be specified at the time of table creation. You cannot add a local secondary index to an existing table. This is a key difference from Global Secondary Index.

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 the Local Secondary Index in DynamoDB.
  • Learn how to create a Local Secondary Index.
  • Learn how to query a Local 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 Table: First, you need to have a DynamoDB table. Let’s assume you have a Movies table with Year (Number) as the partition key and Title (String) as the sort key.

3. Create an LSI: In the same screen, under the “Table settings” section, choose Customize settings.

4. Look for the “Secondary indexes” section and click on “Create local index”. Enter “RatingIndex” as the index name and “Rating” as the sort key. DO NOT forget to change the Data type.

  • Create index.
  • Create table.
Creating Item and Querying the LSI

5. Insert Data into the Table: Once the table is created, and in “Active” Status, you can start inserting data. Click the DynamoDB table you just created, then, click on the “Explore items” on the left panel or the “ Explore table items” on the upper right side of the window, and then, click “Create item”.

6. Add Sample Data: Now, let’s add sample data to the Movies table. Here’s a sample data set:

{
  "Year": {
    "N": "2020"
  },
  "Title": {
    "S": "Movie A"
  },
  "Rating": {
    "N": "8.1"
  }
}
{
  "Year": {
    "N": "2020"
  },
  "Title": {
    "S": "Movie B"
  },
  "Rating": {
    "N": "7.5"
  }
}
{
  "Year": {
    "N": "2021"
  },
  "Title": {
    "S": "Movie C"
  },
  "Rating": {
    "N": "8.3"
  }
}
{
  "Year": {
    "N": "2020"
  },
  "Title": {
    "S": "Movie D"
  },
  "Rating": {
    "N": "7.9"
  }
}

To do this, After you click the” create item”, Enter the Value for the “Year” and “Title” as given above. To add the Rating attribute, click the “Add new attribute” dropdown, and select, “Number”. Then, Enter the Attribute name, “Rating“, and the value, “8.1”. Do the same thing for the rest, by clicking the checkbox beside the first item created, and next, click the “Actions” dropdown, then, “Duplicate item”. Lastly, edit the Values accordingly.

Output:

7. Query the LSI: To query the LSI, click on the “Select a table or index” tab and select “RatingIndex”. Then, enter the “Partition key” value (e.g., 2020) and a condition for the “Sort key” (e.g., Rating Greater than 7.0). Do not forget to select “Query”. Lastly, Click “Run” to initiate the query.

8. Review the Query Results: The results of the query will be displayed in the console. Review the results to ensure they meet your expectations.

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

Skip to content