简体   繁体   English

AWS Dynamodb 扫描 | 扫描是否存在列值

[英]AWS Dynamodb Scan | Scan if exists column values

I have a table that has a column called "items", but not all rows have it, so I want to scan all rows that have "items".我有一个表,其中有一列名为“items”,但并非所有行都有它,所以我想扫描所有包含“items”的行。

Something like:就像是:

$resposta = $clientDB->scan(array(
    'TableName' => 'tableName',
    'Key' => [
        'items' => ['S' => 'exists']
    ]
));

But I can't figure out how to do it...但是我不知道该怎么做...

The table has 10000 rows, but only 10 of them have "items", and I want to get only these 10 rows.该表有 10000 行,但其中只有 10 行有“项目”,我只想得到这 10 行。

Edit:编辑:

As Seth Geoghegan pointed below, it was necessary create a global secondary indexes on the attribute i wanted to filter.正如 Seth Geoghegan 在下面指出的,有必要在我想要过滤的属性上创建一个全局二级索引。

I ended up here:我最终来到了这里:

    $params = [
        'TableName' => 'tableName',
        'FilterExpression' => "attribute_exists(items)"
    ];

OR或者

    $params = [
        'TableName' => 'tableName',
        'FilterExpression' => 'items != :null',
        'ExpressionAttributeValues' =>  [
            ':null' => null,
        ],
    ];

But both didnt worked... First one seens necessary ExpressionAttributeValues to be setup and the second the php stop working with no error logs.但两者都没有工作......第一个看到需要设置的 ExpressionAttributeValues 和第二个 php 停止工作,没有错误日志。

This is a perfect use case for global secondary indexes (GSI)!这是全局二级索引(GSI)的完美用例!

You can create a GSI on the items attribute.您可以在items属性上创建 GSI。 Items with the items attribute defined will get projected into the GSI.定义了items属性的项目将被投影到 GSI 中。 Importantly, items that do not contain this attribute will not be in the index.重要的是,包含此属性的项目不会出现在索引中。 You could then query the GSI and retrieve the items you're after.然后,您可以查询 GSI 并检索您想要的项目。

Well, after some effort, i found a way though:好吧,经过一番努力,我找到了一种方法:

    $resposta = $clientDB->scan(array(
        'TableName' => 'tableName',
        'FilterExpression' => "attribute_exists(items)"
    ));

After i created another global secondary index (GSI) for "items" (pointed by Seth Geoghegan), i just needed to add in the scan function the FilterExpression the "attribute_exists(items") and it worked.在我为“items”(由 Seth Geoghegan 指向)创建另一个全局二级索引 (GSI) 之后,我只需要在扫描 function 中添加 FilterExpression 和“attribute_exists(items”) 即可。

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

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