按日期查询 DynamoDB [英] Querying DynamoDB by date

查看:40
本文介绍了按日期查询 DynamoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关系数据库背景,正在尝试使用亚马逊的 DynamoDB

I'm coming from a relational database background and trying to work with amazon's DynamoDB

我有一个表,其中包含一个散列键DataID"和一个范围CreatedAt"以及其中的一堆项目.

I have a table with a hash key "DataID" and a range "CreatedAt" and a bunch of items in it.

我正在尝试获取在特定日期之后创建并按日期排序的所有项目.这在关系数据库中非常简单.

I'm trying to get all the items that were created after a specific date and sorted by date. Which is pretty straightforward in a relational database.

在 DynamoDB 中,我能找到的最接近的是查询并使用大于过滤器的范围键.唯一的问题是要执行查询,我需要一个无法达到目的的哈希键.

In DynamoDB the closest thing i could find is a query and using the range key greater than filter. The only issue is that to perform a query i need a hash key which defeats the purpose.

那我做错了什么?我的表架构错了吗,散列键不应该是唯一的吗?或者有其他查询方式吗?

So what am I doing wrong? Is my table schema wrong, shouldn't the hash key be unique? or is there another way to query?

推荐答案

更新答案:

DynamoDB 允许指定二级索引以帮助进行此类查询.二级索引可以是全局的,这意味着索引跨越整个表的哈希键,也可以是本地的,意味着索引将存在于每个哈希键分区中,因此要求在进行查询时也指定哈希键.

DynamoDB allows for specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.

对于此问题中的用例,您可能希望在CreatedAt"字段上使用全局二级索引.

For the use case in this question, you would want to use a global secondary index on the "CreatedAt" field.

有关 DynamoDB 二级索引的更多信息请参阅二级索引文档

For more on DynamoDB secondary indexes see the secondary index documentation

原答案:

DynamoDB 不允许仅对范围键进行索引查找.需要哈希键,以便服务知道要查找哪个分区才能找到数据.

DynamoDB does not allow indexed lookups on the range key only. The hash key is required such that the service knows which partition to look in to find the data.

当然可以执行扫描操作来按日期值过滤,但这需要全表扫描,所以不太理想.

You can of course perform a scan operation to filter by the date value, however this would require a full table scan, so it is not ideal.

如果您需要按时间跨多个主键对记录进行索引查找,DynamoDB 可能不是您使用的理想服务,或者您可能需要使用单独的表(在 DynamoDB 或关系存储中)存储您可以对其执行索引查找的项目元数据.

If you need to perform an indexed lookup of records by time across multiple primary keys, DynamoDB might not be the ideal service for you to use, or you might need to utilize a separate table (either in DynamoDB or a relational store) to store item metadata that you can perform an indexed lookup against.

这篇关于按日期查询 DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆