0

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": "a@gmail.com", "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": "a@gmail.com", "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": "c@gmail.com", "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":"a@gmail.com",
"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  
  • for get_item to work you need to have primary key or composite key [ref](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html) . What you want can be accomplished with [scan](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html) or changing sort key. Read [this](https://stackoverflow.com/questions/44589967/how-to-fetch-scan-all-items-from-aws-dynamodb-using-node-js) for more info. – Manav Chhibber Jan 22 '21 at 08:03
  • @ManavChhibber update the code –  Jan 22 '21 at 08:11

1 Answers1

4

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.
Jens
  • 20,533
  • 11
  • 60
  • 86
  • 1
    I know you are asking about Python - but there is a Java V2 example that uses a Secondary index here: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/EnhancedScanRecordsWithExpression.java – smac2020 Jan 22 '21 at 17:59