4

I have somewhat of a specific question regarding how to model my DynamoDB tables so that I can handle the necessary queries.

My application is centered on the idea of "events." Every event has attributes (name, location, time, number of attendees, etc). Events are associated with the cities in which they are located. I am trying to figure out how to perform a get / query request (probably a series of get / query requests) to obtain the top 25 events with the most attendees for a specific city.

I come from a background of relational databases, and this would be a really simple query (select * from events where city = x order by attendees limit 25). But I am having a hard time figuring out how to do the same with a non-relational database. I know I will have to create additional tables to store mappings of hashes, but I can't seem to figure it out.

One way I have thought of implementing it is to somehow let the "attendees" (of Number type) be the range key, and let the city be the hash key. But this will not necessarily be a unique key because multiple events in the same city could have the same number of attendees. Also, is it even possible to "update / atomically increment" a range key?

Thanks for all your help!

4

1 回答 1

1

注意:我仍然相信 RDBMS 更适合这些查询,但这里有:

首先,您只能以原子方式递增一个属性。
现在对于您的情况,我建议以下内容:

Table: Events
hk: eventId
attributes

Table: Top_Attendees_Per_City
hk: city
rk: eventId

Table: Event_Id_Generator
hk: event_counter
running_counter

Table: Minimum_Attendees_Per_City
hk: city
min_attendees_number, max_attendees_number, events_number

一旦一个事件被触发到您的后端,您将需要为其分配一个正在运行的 id。这不是强制性的,它在扩展方面存在问题,但它将确保如果活动有相同数量的参与者,较新的活动将在您的“top25”中优先。
您需要检查参加人数是否在最小和最大之间,同时计算事件直到“25”。这使您的 mimimum_attendees_per_city 可以决定这个新事件是否将出现在 top25 中。如果是,则将其添加到 top_attendees_per_city。
最后,您使用 setScanIndexForward(false) 和 setLimit(25) 查询该表
结果是参加人数最多的 25 个活动。最后说明:结果项目不是由他们的与会者订购的,您可以在返回之前在应用程序级别订购它们。

于 2012-06-06T08:10:42.683 回答