【问题标题】:Graph/Gremlin query for social media use case用于社交媒体用例的图形/Gremlin 查询
【发布时间】:2018-10-07 04:22:01
【问题描述】:

我的是一个社交网络类型的场景。我想获得我关注的人“发布”的所有帖子。对于这些帖子中的每一个,我想知道我是否喜欢它,以及帖子拥有的喜欢和 cmets 的数量(仅计数)以及具有所有属性的最新 3 cmets 以及评论用户的所有属性,例如他的名字等. 在 gremlin 中获得此问题的最佳解决方案是什么(可能避免重复)?

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').
  addV('comment').property('id','c1').property('text','hi').as('c1'). 
  addV('comment').property('id','c2').property('text','nice').as('c2'). 
  addV('comment').property('id','c3').property('text','hello').as('c3'). 
  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').
  addE('commented').from('1').to('c1'). 
  addE('comments').from('c1').to('p1'). 
  addE('commented').from('2').to('c2'). 
  addE('comments').from('c2').to('p2').iterate()

【问题讨论】:

标签: graph-databases gremlin amazon-neptune


【解决方案1】:

commented 边应该有一个时间戳属性,这就是为什么下面的查询仍然有待办事项,但我想应该很容易自己弄清楚剩下的部分。

g.V().has('user','id',1).as('me').
  out('follow').as('friend').
  out('posted').as('post').                                     /* all the posts 'posted' by the people I follow */
  project('friend','post','liked','likes','comments','latest').
    by(select('friend')).
    by(select('post').by('postId')).
    by(coalesce(__.in('liked').where(eq('me')).constant('yes'),
                constant('no'))).                               /* whether I have liked it or not                */
    by(inE('liked').count()).                                   /* no of likes                                   */
    by(inE('comments').count()).                                /* comments that post have(only count)           */
    by(__.in('comments').as('comment').                         /* todo: order by time desc                      */
       in('commented').as('user').limit(3).                     /* latest 3 comments                             */
       select('comment','user').
         by(valueMap()).                                        /* with all properties                           */
       fold())

示例图的结果:

==>[friend:v[2],post:post1,liked:no,likes:0,comments:1,latest:[[comment:[id:[c1],text:[hi]],user:[id:[1]]]]]
==>[friend:v[2],post:post2,liked:yes,likes:3,comments:1,latest:[[comment:[id:[c2],text:[nice]],user:[id:[2]]]]]

【讨论】:

  • 如果您有数百万用户,您可能能够负担得起在此类查询上表现良好的合理规模的集群。此外,如果您有大量并发请求,缓存将成为一件大事,然后就不再是关于底层存储和查询引擎的问题了。
  • Memcached、Redis 等,当用作缓存层时,并非旨在与特定数据库一起使用,您几乎可以将它们用于任何事情,甚至不需要数据库。
  • 我不知道所有这些大公司都在使用什么,但社交网络几乎是图形数据库最常见的用例。您最终决定什么取决于您的性能、可扩展性和可靠性要求。也就是说,您可以使用任何底层数据库技术构建社交网站 - 所以它应该是最适合您的。
  • 在 TP 3.4.0 中,您将能够执行 valueMap().by(unfold())。在当前版本中,您可以unfold() 值映射和重新group() 它。
猜你喜欢
  • 1970-01-01
  • 2014-02-03
  • 2014-10-09
  • 2019-04-20
  • 2019-09-21
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多