user14257643
user14257643

Reputation:

How to get the item from dynamo db if its not primary or sort_key

My dynamodb table has user as partition_key and sort_key as Time

I am searching for the type which is in 'Product Team' from the dynamodb in prescribed format

Below i have added 3 items from the dynamo db table

{ "user": "[email protected]", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested":"Requested for the 192.168.1.1 access" "Time": "2021-01-08 12:09:54.986542" },

{ "user": "[email protected]", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested":"Requested for the 192.168.1.2 access" "Time": "2021-01-09 12:10:54.986542" },

{ "user": "[email protected]", "type": "Ops Team", "message": "Tester", "employeeId": "102", "message_requested":"Requested for the 192.168.1.1 access" "Time": "2021-01-08 11:09:54.986542" }

Expected out is

{"user":"[email protected]",
"params":[{"message_requested":"Requested for the 192.168.1.1 access"
  "Time": "2021-01-08 12:09:54.986542"},
{"message_requested":"Requested for the 192.168.1.2 access"
  "Time": "2021-01-09 12:10:54.986542"}]
}

Below is the code to extract the items from the dynamodb table

def details(type):
    dynamodb = boto3.resource('dynamodb')
    client = boto3.client('dynamodb')
    table = dynamodb.Table("my_db_table_name")
    #res = client.get_item(
    #            TableName=table,
    #            Key={
    #                'type': {'S': 'Product Team'
    #                       }})
    res = table.scan(FilterExpression=Attr("type").eq("Product Team"))
    return res  

Upvotes: 0

Views: 898

Answers (1)

Nikolay Bonev
Nikolay Bonev

Reputation: 151

There are two options:

  1. Use scan to perform search in the table. In this case the search time will be significant compared with Primary/Composite key.
  2. Make a Global Secondary Index and make Primary/Composite key the fields that you need.

Upvotes: 4

Related Questions