使用多个嵌套 JSON 键 (PHP) 进行 DynamoDB 扫描

我的数据存储在 DynamoDB 中的单个表events"中,采用以下架构,_id"作为哈希键,microtime"作为范围键(以下示例数据):

My data is stored in a single table "events" in DynamoDB in the following schema with "_id" as hash key and "microtime" as range key (example data below):

{
  "_id": {
    "S": "ae3761b5-b73b-4fb9-ae5a-5cc230b8fa11"
  },
  "data": {
    "M": {
      "name": {
        "S": "Jeff"
      },
      "purchase_value": {
        "N": "25"
      },
      "user_id": {
        "N": "1201"
      }
    }
  },
  "microtime": {
    "N": "14147568808639"
  }
}

我正在使用 AWS PHP SDK 2.7.0 根据我的过滤器(在本例中为主哈希键_id")扫描数据库.我下面的代码完美运行(返回正确的条目):

I am using AWS PHP SDK 2.7.0 to scan the database based on my filters (in this example, primary hash key "_id"). My code below that works perfectly (returns correct entry):

$client = DynamoDbClient::factory(array(
                'key'    => 'key',
                'secret' => 'secret',
                'region' => 'eu-west-1'
));

$iterator = $client->getIterator('Scan', array(
    'TableName' => 'events',
    'ScanFilter' => array(
        '_id' => array(
            'AttributeValueList' => array(
                array('S' => "ae3761b5-b73b-4fb9-ae5a-5cc230b8fa11")
            ),
            'ComparisonOperator' => 'EQ'
        ),
    )
));

foreach ($iterator as $item) {
  print_r($item);
}

现在,当我尝试根据数据"元素内部的数据进行扫描时,我无法获得任何匹配的条目.下面的示例代码(我正在尝试使用 data.name=Jeff 提取所有内容):

now when I try to scan based on data INSIDE of the "data" element, I can't get any matching entries. Example code below (I am trying to extract all with data.name=Jeff):

$iterator = $client->getIterator('Scan', array(
    'TableName' => 'events',
    'ScanFilter' => array(
        'data.name' => array(
            'AttributeValueList' => array(
                array('S' => "Jeff")
            ),
            'ComparisonOperator' => 'EQ'
        ),
    )
));

foreach ($iterator as $item) {
  print_r($item);
}

任何人都可以看到我的代码有任何问题,或者实际上无法通过比较 JSON 文档内部的值来提取数据?提前感谢您的任何建议!

Anyone can see any issues with my code, or is it in fact not possible to extract data by comparing values INSIDE of a JSON document? Thanks in advance for any suggestions!

编辑

我已尝试使用 DynamoDB PHP SDK 文档中的以下代码 (http://docs.aws.amazon.com/aws-sdk-php/v3/api/Aws/DynamoDb/dynamodb-2012-08-10.html#scan) - 仍然没有成功.我只能在使用主哈希/范围键和任何其他(一级")键进行扫描时检索,但不能像我的示例中那样使用来自文档(地图/列表)内部的键.见下文:

I've tried the following code from DynamoDB PHP SDK Documentation (http://docs.aws.amazon.com/aws-sdk-php/v3/api/Aws/DynamoDb/dynamodb-2012-08-10.html#scan) - still no success. I can only retrieve when scanning using primary hash/range key, and any other ("first-level") key, but not using a key from INSIDE of a document (map/list) like in my example. See below:

$iterator = $client->getIterator('Scan', array(
    'TableName' => 'events',
    'ScanFilter' => array(
        'data' => array(
            'AttributeValueList' => array(
                array(
                    'M' => array(
                        'name' => array("S"=>"Jeff"),
                    ),
                ),
            ),
            'ComparisonOperator' => 'EQ',
        ),
    ),
));

推荐答案

解决我的问题:

$iterator = $client->getIterator('Scan', array(
    'TableName' => 'event_test2',
    'FilterExpression' => 'event.customer_name = :filter',
    "ExpressionAttributeValues" => array(":filter"=>array("S"=>"Jeff")),
));

更多信息在这里:https://forums.aws.amazon.com/thread.jspa?threadID=164470

相关文章