[英]Using both AttributesToGet and KeyConditionExpression with boto3 and dynamodb
我有興趣返回具有給定分區鍵值的所有記錄(即 u_type = "prospect")。 但我只想從每條記錄中返回特定屬性。 我從 boto docs & Stack answers 中收集了以下片段:
resp = table.query(
Select='SPECIFIC_ATTRIBUTES',
AttributesToGet=[
'u_type','ID','action','status','first_name','last_name'
],
KeyConditionExpression=Key('u_type').eq("prospect")
)
運行此程序時,我收到以下錯誤:
An error occurred (ValidationException) when calling the Query operation:
Can not use both expression and non-expression parameters in the same request:
Non-expression parameters: {AttributesToGet}
Expression parameters: {KeyConditionExpression}
此外,我已經嘗試使用具有以下實現的 ProjectionExpression:
resp = table.query(
KeyConditionExpression= 'u_type = :hkey',
ExpressionAttributeValues= {
':hkey': "prospect",
},
Limit= 200,
ProjectionExpression= ['u_type','ID','action','status','first_name','last_name']
)
請注意,這是根據為節點編寫的另一個堆棧溢出答案調整的。
使用此 ProjectionExpression 實現時,我遇到以下錯誤:
Invalid type for parameter ProjectionExpression, value:
['u_type', 'ID', 'action', 'status', 'first_name', 'last_name'], type: <class 'list'>,
valid types: <class 'str'>
我不確定我的方法或描述是否不清楚,但本質上我想使用 boto3 和 dynamo 執行以下 SQL 等效項:
SELECT u_type,ID,action,status,first_name,last_name
FROM table
WHERE u_type LIKE 'prospect';
注意:分區鍵:u_type,排序鍵:ID
AttributesToGet
是一個遺留參數, 文檔建議改用較新的ProjectionExpression
。 該文檔還說ProjectionExpression
是一個字符串,而不是一個列表。 在您鏈接到的答案中,它可能是 NodeJS SDK 中的一個列表,但在 Python 中,文檔說它必須是一個字符串。 所以我會試試這個:
resp = table.query(
KeyConditionExpression= 'u_type = :hkey',
ExpressionAttributeValues= {
':hkey': "prospect",
},
Limit= 200,
ProjectionExpression='u_type,ID,action,status,first_name,last_name'
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.