简体   繁体   English

查找列表字段包含dynamodb中值的所有项目

[英]find all items where a list field contains a value in dynamodb

I'm new to DynamoDb and I'm struggling to work out how to do this (using the java sdk). 我是DynamoDb的新手,我正在努力研究如何做到这一点(使用java sdk)。

I currently have a table (in mongo) for notifications. 我目前有一张桌子(在mongo中)用于通知。 The schema is basically as follows (I've simplified it) 该模式基本上如下(我已经简化了)

id: string
notifiedUsers: [123, 345, 456, 567]
message: "this is a message"
created: 12345678000 (epoch millis)

I wanted to migrate to Dynamodb, but I can't work out the best way to select all notifications that went to a particular user after a certain date? 我想迁移到Dynamodb,但是我想不出最佳方法来选择在特定日期之后发给特定用户的所有通知?

I gather I can't have an index on a list like notifiedUsers, therefore I can't use a query in this case - is that correct? 我收集到我无法在列表中有一个像notifyUsers这样的索引,因此在这种情况下我不能使用查询-是这样吗?

I'd prefer not to scan and then filter, there could be a lot of records. 我不希望先扫描然后过滤,否则可能会有很多记录。

Is there a way to do this using a query or another approach? 有没有办法使用查询或其他方法来做到这一点?

EDIT This is what I'm trying now, it's not working and I'm not sure where to take it (if anywhere). 编辑这是我现在正在尝试的方法,它不起作用,我不确定将其带到哪里(如果有的话)。

Condition rangeKeyCondition = new Condition()
    .withComparisonOperator(ComparisonOperator.CONTAINS.toString())
    .withAttributeValueList(new AttributeValue().withS(userId));

if(startTimestamp != null) {
  rangeKeyCondition = rangeKeyCondition.withComparisonOperator(ComparisonOperator.GT.toString())
      .withAttributeValueList(new AttributeValue().withS(startTimestamp));
}

NotificationFeedDynamoRecord replyKey = new NotificationFeedDynamoRecord();
replyKey.setId(partitionKey);

DynamoDBQueryExpression<NotificationFeedDynamoRecord> queryExpression = new DynamoDBQueryExpression<NotificationFeedDynamoRecord>()
    .withHashKeyValues(replyKey)
    .withRangeKeyCondition(NOTIFICATIONS, rangeKeyCondition);

In case anyone else comes across this question, in the end we flattened the schema, so that there is now a record per userId. 万一其他人遇到这个问题,最后我们将模式展平,以便现在每个userId都有一条记录。 This has lead to problems because it's not possible with dynamoDb to atomically batch write records. 这导致了问题,因为dynamoDb无法原子地批量写入记录。 With the original schema we had one record, and could write it atomically ensuring that all users got that notification. 使用原始模式,我们只有一条记录,并且可以原子地记录它,以确保所有用户都收到该通知。 Now we cannot be certain, and this is causing pain. 现在我们不能确定,这正在引起痛苦。

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

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