我是DynamoDB的新手,想知道我们如何使用Java使用IN子句查询DynamoDB中的表。
我有一个名为items.
的表,它的架构是
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).
答案 0 :(得分:2)
由于要求使用没有分区键的产品,有两种选择。
1)在排序键属性 from scipy import integrate
import numpy as np
t = np.linspace(1, 10, 0.5)
X0 = [0.1]
def func(X,t)
a = 0.5 * t
b = 1/X[0]
return [a * b * X[0]**2]
X, infodict = integrate.odeint(func, X0, t, full_output=True, mxstep=5000000)
上创建GSI(全局二级索引)以使用查询API
2)扫描整个表格以获得产品 - 扫描整个表格时效率不高
这两个选项都使用DynamoDB上提供的 IN运算符。
使用ID扫描的示例代码:
Id
按产品查询: -
1)无法使用 IN运算符作为分区键
2)在不知道排序键的情况下无法使用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());
}
API
<强>结论: - 强>
有效的解决方案是在属性batchLoad
上创建GSI(没有排序键)并使用Id
API。
注意: GSI的分区键不必是唯一的