python - 如何在 appengine 中模拟追随者流?

标签 python database-design google-app-engine bigtable

我正在尝试设计表格以建立关注者关系。

假设我有一个 140 字符的记录流,其中包含用户、主题标签和其他文本。

用户关注其他用户,也可以关注话题标签。

我在下面概述了我的设计方式,但我的设计有两个限制。我想知道其他人是否有更聪明的方法来实现相同的目标。

问题是

  1. 为每条记录复制关注者列表
  2. 如果添加或删除一个新的关注者,“所有” 必须更新记录。

代码

class HashtagFollowers(db.Model):
    """
    This table contains the followers for each hashtag
    """
    hashtag = db.StringProperty()
    followers = db.StringListProperty()

class UserFollowers(db.Model):
    """
    This table contains the followers for each user
    """
    username = db.StringProperty()
    followers = db.StringListProperty()

class stream(db.Model):
    """
    This table contains the data stream
    """
    username = db.StringProperty()
    hashtag = db.StringProperty()
    text = db.TextProperty()

    def save(self):
        """
        On each save all the followers for each hashtag and user
        are added into a another table with this record as the parent
        """
        super(stream, self).save()
        hfs = HashtagFollowers.all().filter("hashtag =", self.hashtag).fetch(10)
        for hf in hfs:
            sh = streamHashtags(parent=self, followers=hf.followers)
            sh.save()
        ufs = UserFollowers.all().filter("username =", self.username).fetch(10)
        for uf in ufs:
            uh = streamUsers(parent=self, followers=uf.followers)
            uh.save()



class streamHashtags(db.Model):
    """
    The stream record is the parent of this record
    """
    followers = db.StringListProperty() 

class streamUsers(db.Model):
    """
    The stream record is the parent of this record
    """
    followers = db.StringListProperty()

Now, to get the stream of followed hastags 

    indexes = db.GqlQuery("""SELECT __key__ from streamHashtags where followers = 'myusername'""")
    keys = [k,parent() for k in indexes[offset:numresults]]
    return db.get(keys)

有没有更聪明的方法来做到这一点?

最佳答案

您要解决的问题称为扇出问题。

来自 Google App Engine 团队的 Brett Slatkin 就 App Engine 上的扇出问题提出了一个高效/可扩展的解决方案。您可以在此处找到演讲视频:

http://code.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html

关于python - 如何在 appengine 中模拟追随者流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898298/

相关文章:

python - 不可散列类型 : 'list' error in python

database - 如何有效地存储大量的 n 克?

mysql - 在 MySQL 中的表之间创建一对一和一对多关系

python - 在哪里可以找到 Google App Engine 中 webapp2 session.get 的详细语法?

python - 如何修改python复数

Python解析大文本文件并抓取多层次数据

java - JPA 多对多关系与额外的枚举列

java - 通过谷歌云端点通过 Android 客户端将文件上传到谷歌云存储

Python - 删除多维数组中的项目

sql - 无法在小数列 SQL 中插入 int 值