Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - 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  
question from:https://stackoverflow.com/questions/65840979/how-to-get-the-item-from-dynamo-db-if-its-not-primary-or-sort-key

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...