Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we query on a secondary index of dynamodb using boto3?

Is there a way at all to query on the global secondary index of dynamodb using boto3. I dont find any online tutorials or resources.

like image 354
ZZzzZZzz Avatar asked Mar 02 '16 21:03

ZZzzZZzz


People also ask

How do you Query DynamoDB with Boto3?

Query Tables in DynamoDB using Boto3 To query items in DynamoDB, you can use the query() method to fetch items based on primary key values. In addition, you can use the KeyConditionExpression to specify the value of the partition key and return all items from the table with that partition key.

How do secondary indexes work DynamoDB?

DynamoDB supports two types of secondary indexes: Global secondary index — An index with a partition key and a sort key that can be different from those on the base table. 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.

What is a secondary index in DynamoDB?

DynamoDB doesn’t have any query optimizer, so a secondary index is used while you Query it or Scan it. DynamoDB supports two types of indexes which have been discussed below in detail: A Global Secondary Index (GSI) is an index with a partition key and a sort key that can be different from keys in the base table.

How can I create a secondary sort key in DynamoDB?

Some applications only need to query data using the base table’s primary key. However, there might be situations where an alternative sort key would be helpful. To give your application a choice of sort keys, you can create one or more local secondary indexes on an Amazon DynamoDB table and issue Query or Scan requests against these indexes.

What are the examples of DynamoDB boto3 query?

List of DynamoDB Boto3 Query Examples 1 Connecting Boto3 to DynamoDB 2 Create Table 3 Get All Items / Scan 4 Get Item 5 Batch Get Item 6 Put Item 7 Query Set of Items 8 Update Item 9 Conditionally Update Item 10 Increment Item Attribute 11 Delete Item 12 Delete All Items 13 Query with Sorting 14 Query Pagination 15 Run DynamoDB Local More ...

How do I get all the data from a DynamoDB index?

Within this game, DynamoDB uses the index to access all of the user IDs and top scores for this game. The results are returned, sorted in descending order because the ScanIndexForward parameter is set to false. You can use the Scan operation to retrieve all of the data from a global secondary index.


2 Answers

You need to provide an IndexName parameter for the query function.

This is the name of the index, which is usually different from the name of the index attribute (the name of the index has an -index suffix by default, although you can change it during table creation). For example, if your index attribute is called video_id, your index name is probably video_id-index.

import boto3 from boto3.dynamodb.conditions import Key dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('videos') video_id = 25 response = table.query(     IndexName='video_id-index',     KeyConditionExpression=Key('video_id').eq(video_id) ) 

To check the index name, go to the Indexes tab of the table on the web interface of AWS. You'll need a value from the Name column.

like image 126
Attila Tanyi Avatar answered Sep 29 '22 19:09

Attila Tanyi


For anyone using the boto3 client, below example should work:

import boto3      # for production client = boto3.client('dynamodb')  # for local development if running local dynamodb server client = boto3.client(    'dynamodb',    region_name='localhost',    endpoint_url='http://localhost:8000' )  resp = client.query(    TableName='UsersTabe',    IndexName='MySecondaryIndexName',    ExpressionAttributeValues={        ':v1': {            'S': '[email protected]',        },    },    KeyConditionExpression='emailField = :v1', )  # will always return list items = resp.get('Items')  first_item = items[0] 
like image 31
GWed Avatar answered Sep 29 '22 18:09

GWed