Retrieve all items from DynamoDB using query?

五迷三道 提交于 2019-12-29 07:24:12

问题


I am trying to retrieve all items in a dynamodb table using a query. 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. 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?


回答1:


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.




回答2:


I'm on groovy but it's gonna drop you a hint. 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. 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. 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)



回答3:


I ran into this error when I was misusing KeyConditionExpression instead of FilterExpression when querying a dynamodb table.

KeyConditionExpression should only be used with partition key or sort key values. FilterExpression should be used when you want filter your results even more.

However do note, using FilterExpression uses the same reads as it would without, because it performs the query based on the keyConditionExpression. It then removes items from the results based on your FilterExpression.

Source Working with Queries




回答4:


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


来源:https://stackoverflow.com/questions/21515765/retrieve-all-items-from-dynamodb-using-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!