java–Firebase数据库 – “扇出”技术

java–Firebase数据库 – “扇出”技术,第1张

概述我正在调查适用于Android的Firebase数据库sample并意识到它以下列方式存储其数据:我不太熟悉NoSQL技术,并试图理解为什么我们必须坚持每个帖子实体两次–相应地在posts和user_posts.文档说这种方法被称为“扇出”,我完全同意通过像databaseReference.child(“user-posts”).chi

我正在调查适用于Android的Firebase数据库sample
并意识到它以下列方式存储其数据:

我不太熟悉Nosql技术,并试图理解为什么我们必须坚持每个帖子实体两次 – 相应地在posts和user_posts.文档说这种方法被称为“扇出”,我完全同意通过像databaseReference.child(“user-posts”).child(“< user_uID>”)之类的简单构造来访问用户的帖子可能是有用的.但是为什么我们需要post节点呢?如果我们需要更新一些帖子怎么办?我们必须做两次吗?

// [START write_fan_out]private voID writeNewPost(String userID, String username, String Title, String body) {    // Create new post at /user-posts/$userID/$postID and at    // /posts/$postID simultaneously    String key = mDatabase.child("posts").push().getKey();    Post post = new Post(userID, username, Title, body);    Map<String, Object> postValues = post.toMap();    Map<String, Object> childUpdates = new HashMap<>();    childUpdates.put("/posts/" + key, postValues);    childUpdates.put("/user-posts/" + userID + "/" + key, postValues);    mDatabase.updateChildren(childUpdates);}// [END write_fan_out]

所以我想…当这种方法可能有用时,何时不是? Firebase SDK是否提供了在更新或删除数据时保持所有重复项同步的任何工具?

更新:以下是Firebase团队的解释received:

the reason the posts are duplicated is because we want to be able to
quickly get all the posts belonging to a user (as you suggested) and
filtering from the List of all posts ever to get the posts by one user
can get pretty expensive as the number of posts expands.

This does mean that we have to update the post in two locations
whenever we update it. It makes the code a little uglIEr but since
querIEs are more common than writes it’s better to optimize for
reading the data.

我怀疑这种方法可能看起来不太优雅,但它可能是大型数据集的最快选择,只要您执行SELECT比UPDATE更频繁.但是,在某些情况下,我宁愿坚持使用此处推荐的其他解决方案.

解决方法:

Data Fan Out是管理大量数据的绝佳技术.如果不使用此模式,将来可能会出现严重的扩展问题.

我从您的数据库结构中看到的是,您将整个帖子信息存储两次,这不是一个好习惯.您希望仅存储对另一个节点下的帖子的引用.因此,您将拥有一个名为users-posts的节点,其中包含用户密钥,每个密钥都有一组值为true的post键.为了更清楚:

这样,您就可以跟踪用户在users-posts节点下编写的帖子;以及在帖子节点下编写每个帖子的用户.现在,您可能需要获取所有用户帖子的列表.您需要做的是在users-posts / USER_KEY / node上同步以获取用户编写的所有帖子的密钥,然后使用您刚刚获得的post密钥获取更多帖子信息.

为什么建议使用此数据库设计?因为每次同步所获得的信息要少得多(使用Firebase,我们不会发出请求,因此我将读取称为同步).在您的示例中,如果您将一个侦听器附加到user-posts / USER_KEY /以获取所有帖子的列表,您还将询问他们编写的每个和每个帖子的所有信息.使用数据扇出方法,您可以只询问所需的帖子信息,因为您已经拥有帖子的密钥.

总结

以上是内存溢出为你收集整理的java – Firebase数据库 – “扇出”技术全部内容,希望文章能够帮你解决java – Firebase数据库 – “扇出”技术所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存