【问题标题】:Any solution for a join on app engine?加入应用引擎的任何解决方案?
【发布时间】:2010-08-11 18:10:09
【问题描述】:

我有以下数据模型:

class StadiumOccupant {
  String stadiumname;
  String username;
}

class Friend {
  String username;
  String friendname;
}

我想找到体育场内所有同时也是我朋友的用户。我可以这样做:

List<String> friendsAtStadium;
String stadiumId = 'xyz';

List<Friend> friends = select from Friend where username = 'me';
for (Friend friend : friends) {
  List<StadiumOccupant> friendAtStadium = 
    select from StadiumOccupant where 
    stadiumname = stadiumId and
    username = friend.friendname;
  friendsAtStadium.addAll(friendAtStadium);
}

这适用于非常少量的对象。一旦用户拥有多个朋友,这将不起作用。应用引擎上有解决方案吗?我想不出一个表现好的。

谢谢

--------一些示例数据--------

Friend { john, tim }
Friend { john, mary }
Friend { mary, frank }

StadiumOccupant { abc, tim }
StadiumOccupant { abc, frank }
StadiumOccupant { abc, kim }

所以如果我是用户“john”,我的朋友是:{ tim, mary }。 如果我使用 Stadiumname='abc' 运行体育场查询,我应该返回 { tim }。

【问题讨论】:

  • 您可以更改数据建模吗?
  • username = friend.getUsername(); username = friend.getFriendName();,因为friend.getUserName() 将永远是“我”,因为friendfriends 的一个元素,您之前通过过滤选择了它username = "me"?
  • @Edward:是的,我可以更改建模,我正在尝试找出是否有任何可能的方法,可以是任何方法。
  • @Andrei:你是对的,修正了那个错字。
  • 一个人一次可以在多个体育场内吗? “体育场占用”可能是用户的财产。或者,我猜更准确地说,是人实体的子实体?

标签: java google-app-engine bigtable


【解决方案1】:

对于少量数据,你可以按照你的建议去做。对于大量数据,您无论如何都不会进行连接,即使您使用 SQL 数据库作为后端。

您的问题几乎是典型的社交网络数据问题。最有可能的是,您将希望对数据进行非规范化,并在用户更新其状态时将信息“推送”到所有用户的朋友列表,或者可能像您在上面所做的那样将其拉出。最佳解决方案取决于数据的形状以及您想要优化的查询类型 - 检索或更新。

请参阅我的回答 here 了解更多信息,包括 Facebook 工程师的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-29
    • 2012-03-25
    • 2012-01-13
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多