Aru*_* SS 10 bigdata amazon-web-services amazon-dynamodb
我试图查询我的dynamodb表以获取feed_guid和status_id = 1.但它返回查询键条件不支持错误.请找到我的表架构和查询.
$result =$dynamodbClient->createTable(array(
'TableName' => 'feed',
'AttributeDefinitions' => array(
array('AttributeName' => 'user_id', 'AttributeType' => 'S'),
array('AttributeName' => 'feed_guid', 'AttributeType' => 'S'),
array('AttributeName' => 'status_id', 'AttributeType' => 'N'),
),
'KeySchema' => array(
array('AttributeName' => 'feed_guid', 'KeyType' => 'HASH'),
),
'GlobalSecondaryIndexes' => array(
array(
'IndexName' => 'StatusIndex',
'ProvisionedThroughput' => array (
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5
),
'KeySchema' => array(
array(
'AttributeName' => 'status_id',
'KeyType' => 'HASH'
),
),
'Projection' => array(
'ProjectionType' => 'ALL'
)
),
array(
'IndexName' => 'UserIdIndex',
'ProvisionedThroughput' => array (
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5
),
'KeySchema' => array(
array(
'AttributeName' => 'user_id',
'KeyType' => 'HASH'
),
),
'Projection' => array(
'ProjectionType' => 'ALL'
)
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
)
));
Run Code Online (Sandbox Code Playgroud)
以下是我更新该表的查询.
$result = $dynamodbClient->query(array(
'TableName' => 'feed',
'KeyConditionExpression' => 'feed_guid = :v_fid AND status_id = :v_sid ',
'ExpressionAttributeValues' => array(
':v_fid' => array('S' => '71a27f0547cd5456d9ee7c181b6cb2f8'),
':v_sid' => array('N' => 1)
),
'ConsistentRead' => false
));
Run Code Online (Sandbox Code Playgroud)
小智 9
如上所述,"KeyConditionExpression"中包含的属性应该只是您的哈希键,与您的基表架构匹配(在本例中为'feed_guid').如果要同时查询'feed_guid'和'status_id',则需要使用hash和range键创建表,并将'status_id'指定为范围键.
全局二级索引与基表完全分离,因此在这种情况下,您可以单独查询索引(在查询StatusIndex时在键条件下使用'status_id',在查询UserIdIndex时在键条件下使用'user_id').
请在此处查找有关查询全局二级索引的更多详细信息
小智 5
另一种选择是除了 KeyConditionExpression 之外还使用 FilterExpression。正如 Daniela 提到的,KeyConditionExpression 应仅包含哈希键中的列。但任何未索引的列都可以包含在 FilterExpression 中。
您需要在 KeyConditionExpression 中提供 hash_key(分区键)和 range_key(排序键)。hash_key 必须完全匹配 (=),但 range_key 可以使用其他运算符,例如 'begins_with'
| 归档时间: |
|
| 查看次数: |
24119 次 |
| 最近记录: |