gor*_*dyr 2 gremlin titan tinkerpop tinkerpop3
我正在努力与格雷姆林交手.彻底阅读了文档后,我似乎仍然在概念上苦苦思索.
我正在创建一个基本的新闻源,遵循Neo4j文档中的模型:
http://neo4j.com/docs/snapshot/cypher-cookbook-newsfeed.html
我实际上正在使用titandb,但遵循上面显示的相同类型的原则/架构.
到目前为止,我已经创建了一个user顶点图,它通过friend边连接.
我可以添加一个新的post顶点并通过posted边连接到user顶点,如下所示:
def activity = graph.addVertex(T.label, "post");
activity.property("post_id", post_id);
activity.property("time", time);
activity.property("body", body);
def g = graph.traversal();
def user = g.V().hasLabel("user").has("userid", userid).next();
user.addEdge("posted", activity, "time", time);
Run Code Online (Sandbox Code Playgroud)
但是,我需要能够在一个Gremlin脚本中执行以下操作:
post如上所述创建新顶点.posted之间的旧边.但只有当一个帖子存在.userpostpost顶点附加到新顶点.userpostedpost顶点,则post通过next边将其附加到新添加的顶点.最终为每个用户创建长流量的帖子.我一直在玩,使用反复试验,现在好像几个小时,似乎无法理解它.
任何帮助将不胜感激.
另一种方式(使用单次遍历):
使用单个用户创建初始图表:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("user").property("userid", 123)
==>v[0]
Run Code Online (Sandbox Code Playgroud)
添加第一篇文章:
gremlin> g.V().has("user", "userid", 123).as("user"). /* find the user */
addV("post").as("p").property("post_id", 1). /* add a new post */
property("time", System.currentTimeMillis()).
property("body", "bla bla").
addE("posted").from("user").as("e"). /* connect user and post */
property("time", System.currentTimeMillis()).
outV(). /* traverse to user */
outE("posted").where(neq("e")).as("o"). /* traverse to any pre-existing posted edge */
inV(). /* traverse to pre-existing post */
addE("next").to("p"). /* connect it with the new post */
select("o").drop() /* drop the old posted edge */
gremlin> // check
gremlin> g.V().not(inE()).repeat(union(outE("posted").inV(), inE("next").outV())).until(__.not(union(outE("posted"), inE("next")))).path().by(label)
==>[user, posted, post]
Run Code Online (Sandbox Code Playgroud)
添加其他帖子(同一查询):
gremlin> g.V().has("user", "userid", 123).as("user").
addV("post").as("p").property("post_id", 1).
property("time", System.currentTimeMillis()).
property("body", "bla bla").
addE("posted").from("user").as("e").
property("time", System.currentTimeMillis()).
outV().
outE("posted").where(neq("e")).as("o").
inV().
addE("next").to("p").
select("o").drop()
gremlin> // check
gremlin> g.V().not(inE()).repeat(union(outE("posted").inV(), inE("next").outV())).until(__.not(union(outE("posted"), inE("next")))).path().by(label)
==>[user, posted, post, next, post]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |