如何在 Java 中对社交图进行建模
非常简单的场景
N 个用户,每个用户可以有 0 .. N - 1 个朋友(他们也是用户)
我如何在 AppEngine 数据存储的 Java 中对此进行建模
考虑
- 用户 x 和用户 y 的 场景朋友(所以双方都需要在交易中更新自己的状态
Very simple scenario
N users, each user can have 0 .. N - 1 friends (who are also users)
How can I model this in Java for AppEngine data store
Scenario to consider
- user x and user y become friends (so both need update their own status, in a transaction
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们将用户关系建模为一个简单的 UserRelation 实体:
其中 RelationState 是一个枚举,描述状态(通常,不仅仅是友谊)
实际上,我们还使用此枚举进行授权,例如在用户配置文件上。
最简单的方法是为每个(对称)关系(例如,在 facebook 上使用的友谊)设置两个对象,为非对称关系(例如,在 twitter 上使用的关注者或阻止的用户)设置一个对象。虽然这首先看起来像是开销,但使用两个对象肯定会简化查询。
我认为 AppEngine 部分本身应该非常简单。
We've modeled user relations as a simple UserRelation entity:
Where RelationState is an enum, describing states (normally, there is more than friendship)
Actually, we also use this enum for authorizaton, e.g. on user profiles.
It's easiest to have two objects for each (symmetric) relationship (e.g. friendship as used on facebook) and only a single object for non-symmetric relationships (e.g. followers as used on twitter or blocked users). While this might look like overhead in the first place, using two objects certainly simplifies querying.
I think the AppEngine part itself should then be pretty straight forward.
考虑使用只有两个外键的
Friendship
表:user1
和user2
。该表中的条目模拟了两个用户之间的社交联系。您甚至可以添加更多列来描述此社交关系的类型
。(或考虑 sfussenegger 的答案;)相同的想法但更好的演示)
Consider using a
Friendship
table with just two foreign keys,user1
anduser2
. An entry in this table models an social connection between two users. You could even add more columns to describe thetype
of this social relation.(or consider sfussenegger's answer ;) same idea but better presentation)