문제

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. 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.

Validation errors: [HashKeyValue] is a required object: Attribute value of the hash component of the composite primary key.

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.

This runs but it only gives me the results filtered by the 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).

From the DynamoDB Secondary Index Documentation (emphasis mine):

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top