简体   繁体   English

AWS Boto3 Dynamodb查询问题

[英]AWS Boto3 Dynamodb Query Issue

I am having issues defining a Query expression for AWS Dynamodb. 我在定义AWS Dynamodb的查询表达式时遇到问题。 The goal is to get the unique dynamodb record that has the field timeStamp that contains the string "1475413345". 目的是获得具有包含字符串“ 1475413345”的字段timeStamp的唯一dynamodb记录。 I have confirmed that I can retrieve this via the AWS web console using the same string. 我已经确认可以使用相同的字符串通过AWS Web控制台检索此字符串。

I suspect I have problem with the syntax for .contains incorrect. 我怀疑.contains的语法有问题。

Any assistance is greatly appreciated! 非常感谢您的协助!

Here is the Code: 这是代码:

import boto3
from boto3.dynamodb.conditions import Key, Attr

from time import sleep

benchtemp = boto3.resource('dynamodb')  # if set in ~/.boto
table = benchtemp.Table('benchtemp')

temps = table.query(
    KeyConditionExpression=Key('timeStamp').contains('1475413345')
)


for temp in temps:
    print temp;
    time.sleep(1)

Here is the error message I'm getting: 这是我收到的错误消息:

Traceback (most recent call last):
  File "/Users/neal/PycharmProjects/Read-NMT-DB/Read-NMT-DB.py", line 15, in <module>
    KeyConditionExpression=Key('timeStamp').contains('1475413345')
AttributeError: 'Key' object has no attribute 'contains'

Process finished with exit code 1

KeyConditionExpressions may only constrain the primary (partition and sort) keys of the table/index. KeyConditionExpressions只能约束表/索引的主键(分区和排序)。 If timeStamp is not a primary key, you can use a FilterExpression to filter out the keys that do not match the timestamp prefix you are looking for. 如果timeStamp不是主键,则可以使用FilterExpression筛选出与要查找的时间戳前缀不匹配的键。

Try writing your query as such: 尝试这样编写查询:

temps = table.query(
            TableName=table,
            ExpressionAttributeNames='#tS = timeStamp'
            KeyConditionExpression='#tS = :recordTimeStamp',
            ExpressionAttributeValues={":recordTimeStamp": Decimal(1475413345)}
        )

Note that ExpressionAttributeNames is needed because "timeStamp" is a reserved word. 请注意,需要ExpressionAttributeNames,因为“ timeStamp”是保留字。 For information on that, see here . 有关此信息,请参见此处

Use Decimal if you record timeStamp as a number in your database. 如果将timeStamp作为数字记录在数据库中,请使用Decimal。 To use Decimal, import: 要使用十进制,请导入:

from decimal import *

If you record timeStamp as a string, use str(1475413345) instead of Decimal(1475413345). 如果将timeStamp记录为字符串,请使用str(1475413345)而不是Decimal(1475413345)。

As Alexander said, make sure that timeStamp is your partition key. 正如亚历山大所说,确保timeStamp是您的分区键。 If it is not, either change the table structure so that it is or use a filter expression. 如果不是,则更改表结构使其为原来的状态,或使用过滤器表达式。 You can only query using either partition and sort OR just the partition key. 您只能使用任一分区进行查询排序,或者仅使用分区键进行查询。

For example, if userId is your partition key and timeStamp is your sort key, you can perform the following read actions: 例如,如果userId是您的分区键,timeStamp是您的排序键,则可以执行以下读取操作:

Get Item: Find item with userId = ABC and timeStamp = 123
Query by Partition Key: Find all items with userId = ABC
Query by Partition Key and Sort Condition:  Find all items with userId = ABC and timeStamp is < 100

(Source: AWSMobileHub auto-generated examples) (来源:AWSMobileHub自动生成的示例)

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

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