简体   繁体   English

具有本地二级索引(PHP)的AWS DynamoDB查询

[英]AWS DynamoDB Query with Local Secondary Index (PHP)

Trying to do a query with my local secondary index.... If I don't specify the HashRangeKey, I get an error saying it's required. 尝试使用本地二级索引进行查询。...如果未指定HashRangeKey,则会收到一条错误消息,指出它是必需的。 If I add it, then it ignores the range condition. 如果添加它,则它会忽略范围条件。

The query seems to work fine in AWS "Table Explorer", so I think it is just a simple error or something with the library. 该查询似乎在AWS“表资源管理器”中运行良好,因此我认为这只是一个简单的错误或该库中的内容。

Validation errors: [HashKeyValue] is a required object: Attribute value of the hash component of the composite primary key. 验证错误:[HashKeyValue]是必需的对象:复合主键的哈希组件的属性值。

array(
      'TableName'=>self::$_tableName,
      'IndexName'=>'vote-index',
      'KeyConditions' => array(
        'itemId' => array(
          'ComparisonOperator' => 'EQ',
          'AttributeValueList' => array(
            array('N'=>$itemId),
          ),
        ),
        'vote' => array(
          'ComparisonOperator' => 'EQ',
          'AttributeValueList' => array(
            array('N'=>$vote),
          ),
        ),
      ),
      'Count' => true,
    )

*According to this code here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelPHPQuerying.html I shouldn't need a HashKeyValue. *根据此处的代码: http ://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelPHPQuerying.html我不需要HashKeyValue。

This runs but it only gives me the results filtered by the HashKeyValue 这会运行,但只给我提供了由HashKeyValue过滤的结果

array(
      'TableName'=>self::$_tableName,
      'IndexName'=>'vote-index',

      'HashKeyValue'=>array(
        'N'=>$itemId,
      ),
      'RangeKeyValue'=>array(
        'N'=>$vote,
      ),
)

Error: The provided key element does not match the schema 错误:提供的关键元素与架构不匹配

array(
      'TableName'=>self::$_tableName,
      'IndexName'=>'vote-index',

      'HashKeyValue'=>array(
        'N'=>$itemId,
      ),
      'RangeKeyCondition'=>array(
        'ComparisonOperator' => 'EQ',
        'AttributeValueList' => array(
          array('N'=>$vote),
        ),
      ),
)

This answer may be helpful. 这个答案可能会有所帮助。

A local secondary index functions like an alternate range key...whether querying the table's main hash key/range key combo or the hash key/local secondary index, you must specify a hash key (think of DynamoDB as a big hash table...impossible to look things up without a hash key). 本地二级索引的功能类似于备用范围键...无论查询表的主哈希键/范围键组合还是哈希键/本地二级索引,您都必须指定一个哈希键(将DynamoDB视为一个大型哈希表。) 。无法在没有哈希键的情况下查找内容)。

From the DynamoDB Secondary Index Documentation (emphasis mine): DynamoDB二级索引文档 (重点是我的):

Local secondary index — an index that has the same hash key as the table, but a different range key . 本地二级索引— 具有与表相同的哈希键,但具有不同的范围键的索引 A local secondary index is "local" in the sense that every partition of a local secondary index is scoped to a table partition that has the same hash key. 本地二级索引在本地二级索引的每个分区都作用于具有相同哈希键的表分区的意义上是“本地”。

If you want to query by some other attribute in the table that is not the table's main hash key, you'll need to make a global secondary index, which is more like an alternate table hash key. 如果要通过表中的某些其他属性(而不是表的主哈希键)进行查询,则需要创建一个全局二级索引,该索引更像是备用表哈希键。 Unfortunately, you can specify indexes only at creation time, and cannot modify them, so you may need to migrate your tables. 不幸的是,您只能在创建时指定索引,而不能修改它们,因此您可能需要迁移表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM