简体   繁体   English

在不使用主分区键的情况下查询 DynamoDB 中的所有数据

[英]Query all data in DynamoDB without using primary partition key

I am very new to DynamoDB.我对 DynamoDB 很陌生。 I want to query all data within certain time range.我想查询一定时间范围内的所有数据。

column = "timerange" is primary sort key column = "timerange" 是主排序键
column = "name" is primary partition key. column = "name" 是主分区键。

I want to get all data within 2 timerange.我想在 2 个时间范围内获取所有数据。

This is my query.这是我的查询。

from decimal import *
from boto3.dynamodb.conditions import Key, Attr
import boto3
import time
from datetime import datetime

dynamo = boto3.resource('dynamodb')
table = dynamo.Table('tablename')
a = time.mktime(datetime.strptime('2020-03-26 14:29:10','%Y-%m-%d %H:%M:%S').timetuple())
b = time.mktime(datetime.strptime('2020-03-26 14:30:10','%Y-%m-%d %H:%M:%S').timetuple())

response = table.query(
    KeyConditionExpression =
        Key('timerange').between(Decimal(a), Decimal(b)))

Which gives me an error ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: After searching the internet I found out that you need to have primary partition key inside your query so I tried the Contains method from https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html这给了我一个错误ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element:搜索互联网后我发现您需要在查询中包含主分区键,所以我尝试了包含方法来自https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html

response = table.query(
    KeyConditionExpression =
        Key('name').contains('a', 'b') &
        Key('timerange').between(Decimal(a), Decimal(b)))

Which I clearly do not understand fully.我显然不完全理解。

How can I get all data within given timerange [a,b]?如何获取给定时间范围 [a,b] 内的所有数据?

You cannot solve this kind of problem easily in DynamoDB, at least not in the general case that would allow you to make one query, and only one query, to get all records within an arbitrary date range, regardless of name (the partition key).您无法在 DynamoDB 中轻松解决此类问题,至少在允许您进行一次查询且仅一次查询以获取任意日期范围内的所有记录的一般情况下,无论name如何(分区键) .

DynamoDB is a key/value database. DynamoDB 是一个键/值数据库。 Your queries are typically against individual keys, optionally with a range of values for a sort key.您的查询通常针对单个键,可以选择使用排序键的一系列值。 A query for name=A and timestamp between X and Y is perfect and can be queried very efficiently.查询 name=A 和 X 和 Y 之间的时间戳是完美的,可以非常有效地查询。

To do what you want, you would most typically create a Global Secondary Index whose primary key was a composite of:要执行您想要的操作,您通常会创建一个全局二级索引,其主键是以下各项的组合:

  • YYMMDD of the timestamp时间戳的 YYMMDD
  • the timestamp时间戳

Now, you can query for items with timestamp in a certain range, regardless of name , but they must be on the same date .现在,您可以查询时间戳在一定范围内的项目,无论name如何,但它们必须在同一日期 If you needed this query to work more broadly, say with ranges up to a month, then your GSI would have a primary key that was a composite of:如果您需要此查询更广泛地工作,例如最多一个月的范围,那么您的 GSI 将有一个主键,它由以下各项组成:

  • YYMM of the timestamp时间戳的 YYMM
  • the timestamp时间戳

And now you can query on all items within a given range of dates/times in the same month.现在您可以查询同一个月内给定日期/时间范围内的所有项目。

Here are a couple of useful resources:以下是一些有用的资源:

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

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