使用 IN 子句查询 DynamoDB

2023-06-26Java开发问题
15

本文介绍了使用 IN 子句查询 DynamoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是 DynamoDB 的新手,想知道如何使用 Java 在带有 IN 子句的 DynamoDB 中的表上进行查询.

I am new to DynamoDB and wanted to know how can we query on a table in DynamoDB with IN clause using Java.

我有一个名为 items 的表.它的架构是

I have a table named items. It`s schema is

1. Product (Partition Key of type String)
2. ID (Sort Key of type int)
3. Date ( attribute of type String)

我想查询类似

SELECT ID FROM items WHERE Product IN ("apple","orange"). 

SELECT Product FROM items WHERE ID IN (100,200). 

推荐答案

由于要求是获取没有分区键的产品,有两种选择.

As the requirement is to get the products without the partition key, there are two options available.

1) 在排序键属性 Id 上创建 GSI(全局二级索引)以使用查询 API

1) Create GSI (Global Secondary Index) on sort key attribute Id to use the Query API

2) 扫描整个表格以获取产品 - 因为它扫描整个表格效率不高

2) Scan the entire table to get the products - not very efficient as it scans the full table

这两个选项都使用 DynamoDB 上可用的 IN 运算符.

Both the options use the IN operator available on DynamoDB.

使用 ID 进行扫描的示例代码:-

Map<String, AttributeValue> attributeValues = new HashMap<>();
attributeValues.put(":id1", new AttributeValue().withN("100"));
attributeValues.put(":id2", new AttributeValue().withN("200"));

DynamoDBScanExpression dynamoDBScanExpression = new DynamoDBScanExpression()
                                                    .withFilterExpression("ID IN (:id1, :id2)")
                                                    .withExpressionAttributeValues(attributeValues);


PaginatedScanList<yourModelClass> scanList = dynamoDBMapper.scan(yourModelClass.class, dynamoDBScanExpression,
        new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT));

Iterator<yourModelClass> iterator = scanList.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next().getTitle());
}

按产品查询:-

1) 不能使用 IN 运算符 作为分区键

1) Cannot use IN operator for partition key

2) 在不知道排序键的情况下无法使用 batchLoad API

2) Cannot use batchLoad API without knowing the sort key

结论:-

有效的解决方案是在属性 Id 上创建 GSI(不带排序键)并使用 batchLoad API.

The efficient solution is to create GSI (without sort key) on attribute Id and use batchLoad API.

注意: GSI 的分区键不必唯一

Note: The partition key of GSI need not be unique

这篇关于使用 IN 子句查询 DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13