Graph/Gremlin用于社交媒体用例 [英] Graph/Gremlin for social media use case

查看:135
本文介绍了Graph/Gremlin用于社交媒体用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑instagram feed场景.我想让所有追随者都发布"所有帖子.对于这些帖子,我想知道我是否喜欢它,也想知道我关注的其他人中有哪些(如果有的话). 在gremlin中获得最佳解决方案是什么(可能避免重复)?

Consider instagram feed scenario. I want to get all the posts 'posted' by the people I follow. For each of these posts I want to know whether I have liked it or not and also know which of the other people I follow have liked it (if any). What is the best solution to get this in gremlin (possibly avoiding duplication)?

图像清晰起见

以下内容仅是USER 2发布"的帖子.如何在同一查询中获取其他信息?

The following just gives the posts 'posted' by USER 2. How to get other information in the same query?

g.V().has('ID','USER 2').out('posted')

推荐答案

当您询问有关Gremlin的问题,尤其是这种复杂性之一时,始终最好包括提供一些示例数据的Gremlin脚本,如下所示:

When you ask questions about Gremlin, especially one of this complexity, it is always best to include a Gremlin script that provides some sample data, like this:

g.addV('user').property('id',1).as('1').
  addV('user').property('id',2).as('2').
  addV('user').property('id',3).as('3').
  addV('user').property('id',4).as('4').
  addV('post').property('postId','post1').as('p1').
  addV('post').property('postId','post2').as('p2').
  addE('follow').from('1').to('2').
  addE('follow').from('1').to('3').
  addE('follow').from('1').to('4').
  addE('posted').from('2').to('p1').
  addE('posted').from('2').to('p2').
  addE('liked').from('1').to('p2').
  addE('liked').from('3').to('p2').
  addE('liked').from('4').to('p2').iterate()

至于答案,我可能会做这样的事情:

As for the answer, I would probably do something like this:

gremlin> g.V().has('id',1).as('me').
......1>   out('follow').
......2>   aggregate('followers').
......3>   out('posted').
......4>   group().
......5>     by('postId').
......6>     by(project('likedBySelf','likedByFollowing').
......7>          by(__.in('liked').where(eq('me')).count()).
......8>          by(__.in('liked').where(within('followers')).values('id').fold()))
==>[post2:[likedBySelf:1,likedByFollowing:[3,4]],post1:[likedBySelf:0,likedByFollowing:[]]]

您找到用户,并用aggregate()使他们的关注者将他们保持在列表中.然后,您可以使用out('posted')找到他们的帖子.要获得输出的Map结构,可以在这些文章"上使用group().第二个by()调制器使用project()来构建内部的Map并基本上进行两次遍历,其中第一个使用零或一个通过执行count()表示布尔值,第二个返回跟随者" "我们之前汇总的列表以对其进行过滤.请注意fold()在末尾的重要用法,以将内部遍历的结果减少为列表.

You find the user and get their followers holding them in a list with aggregate(). Then you find their posts with out('posted'). To get your Map structure for your output you can group() on those "posts". The second by() modulator uses project() to build your inner Map and basically makes two traversals, where the first uses zero or one to represent your boolean value by doing a count() and the second goes back to the "followers" list we aggregated earlier to filter for those. Note the important use of fold() at the end there to reduce the result of that inner traversal to a list.

这篇关于Graph/Gremlin用于社交媒体用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆