简体   繁体   English

使用查询从 DynamoDB 中检索所有项目?

[英]Retrieve all items from DynamoDB using query?

I am trying to retrieve all items in a dynamodb table using a query.我正在尝试使用查询检索 dynamodb 表中的所有项目。 Below is my code:下面是我的代码:

import boto.dynamodb2
from boto.dynamodb2.table import Table
from time import sleep

c    = boto.dynamodb2.connect_to_region(aws_access_key_id="XXX",aws_secret_access_key="XXX",region_name="us-west-2")

tab  = Table("rip.irc",connection=c)

x    = tab.query()

for i in x:
    print i
    sleep(1)

However, I recieve the following error:但是,我收到以下错误:

ValidationException: ValidationException: 400 Bad Request
{'message': 'Conditions can be of length 1 or 2 only', '__type': 'com.amazon.coral.validate#ValidationException'}

The code I have is pretty straightforward and out of the boto dynamodb2 docs, so I am not sure why I am getting the above error.我拥有的代码非常简单,并且不在 boto dynamodb2 文档中,所以我不确定为什么会出现上述错误。 Any insights would be appreciated (new to this and a bit lost).任何见解将不胜感激(对此是新的,有点丢失)。 Thanks谢谢

EDIT: I have both an hash key and a range key.编辑:我有一个散列键和一个范围键。 I am able to query by specific hash keys.我可以通过特定的哈希键进行查询。 For example,例如,

x = tab.query(hash__eq="2014-01-20 05:06:29")

How can I retrieve all items though?我怎样才能检索所有项目?

Ahh ok, figured it out.啊好吧,想通了。 If anyone needs:如果有人需要:

You can't use the query method on a table without specifying a specific hash key.您不能在不指定特定哈希键的情况下对表使用查询方法。 The method to use instead is scan.使用的方法是扫描。 So if I replace:所以如果我替换:

x    = tab.query()

with

x    = tab.scan()

I get all the items in my table.我拿到了桌子上的所有物品。

I'm on groovy but it's gonna drop you a hint.我在 groovy 但它会给你一个提示。 Error :错误 :

{'message': 'Conditions can be of length 1 or 2 only'}

is telling you that your key condition can be length 1 -> hashKey only , or length 2 -> hashKey + rangeKey .告诉你你的关键条件可以是 length 1 -> hashKey only ,或 length 2 -> hashKey + rangeKey All what's in a query on a top of keys will provoke this error.键顶部的查询中的所有内容都会引发此错误。 The reason of this error is: you are trying to run search query but using key condition query.此错误的原因是:您正在尝试运行搜索查询,但使用了关键条件查询。 You have to add separate filterCondition to perform your query.您必须添加单独的filterCondition才能执行查询。 My code我的代码

    String keyQuery = " hashKey = :hashKey and rangeKey between :start and :end "
    queryRequest.setKeyConditionExpression(keyQuery)// define key query
    String filterExpression = " yourParam = :yourParam "
    queryRequest.setFilterExpression(filterExpression)// define filter expression
    queryRequest.setExpressionAttributeValues(expressionAttributeValues)
    queryRequest.setSelect('ALL_ATTRIBUTES')
    QueryResult queryResult = client.query(queryRequest)

.scan() does not automatically return all elements of a table due to pagination of the table.由于表格的分页,.scan() 不会自动返回表格的所有元素。 There is a 1Mb max response limit Dynamodb Max response limit有 1Mb 最大响应限制Dynamodb 最大响应限制

Here is a recursive implementation of the boto3 scan:这是 boto3 扫描的递归实现:

import boto3
dynamo = boto3.resource('dynamodb')


def scanRecursive(tableName, **kwargs):
        """
        NOTE: Anytime you are filtering by a specific equivalency attribute such as id, name 
        or date equal to ... etc., you should consider using a query not scan

        kwargs are any parameters you want to pass to the scan operation
        """
        dbTable = dynamo.Table(tableName)
        response = dbTable.scan(**kwargs)
        if kwargs.get('Select')=="COUNT":
            return response.get('Count')
        data = response.get('Items')
        while 'LastEvaluatedKey' in response:
            response = kwargs.get('table').scan(ExclusiveStartKey=response['LastEvaluatedKey'], **kwargs)
            data.extend(response['Items'])
        return data

I ran into this error when I was misusing KeyConditionExpression instead of FilterExpression when querying a dynamodb table.当我在查询 dynamodb 表时误用KeyConditionExpression而不是FilterExpression时,我遇到了这个错误。

KeyConditionExpression should only be used with partition key or sort key values. KeyConditionExpression应该只与分区键或排序键值一起使用。 FilterExpression should be used when you want filter your results even more.当您想进一步过滤结果时,应使用FilterExpression

However do note, using FilterExpression uses the same reads as it would without, because it performs the query based on the keyConditionExpression .但是请注意,使用FilterExpression使用与不使用时相同的读取,因为它根据keyConditionExpression执行查询。 It then removes items from the results based on your FilterExpression .然后根据您的FilterExpression从结果中删除项目。

Source Working with Queries 使用查询的

This is how I do a query if someone still needs a solution:如果有人仍然需要解决方案,我就是这样做的:

def method_name(a, b)
    results = self.query(
      key_condition_expression: '#T = :t',
      filter_expression: 'contains(#S, :s)',
      expression_attribute_names: {
        '#T' => 'your_table_field_name',
        '#S' => 'your_table_field_name'
      },
      expression_attribute_values: {
        ':t' => a,
        ':s' => b
      }
    )
    results
  end

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

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