简体   繁体   English

Gremlin 查询将顶点与不相关的顶点 CosmosDB 结合起来

[英]Gremlin query combine vertices with unrelated vertices CosmosDB

I would like to get several vertices eG with the label "user" combined with vertices, they are not related to, yet eG with the label "movie".我想获得几个顶点 eG 与 label“用户”结合的顶点,它们不相关,但 eG 与 label“电影”。

I know, that the strength of Gremlin is traversing the vertex, and combining objects that are not related is not the best use case for the graph.我知道,Gremlin 的优势在于遍历顶点,并且组合不相关的对象并不是图形的最佳用例。 I am using Azure CosmosDB for my application, so if there is any idea how to do this more performant feel free to let me know.我正在为我的应用程序使用 Azure CosmosDB,所以如果有任何想法如何做到这一点,请随时告诉我。 If you can do this with gremlin I need some help with the query.如果你可以用 gremlin 做到这一点,我需要一些查询方面的帮助。 I provide an example here:我在这里提供一个例子:

There are 4 users: bob, jose, frank, peter and 4 movies: movie1, movie2, movie3, movie4有 4 个用户:bob、jose、frank、peter 和 4 部电影:movie1、movie2、movie3、movie4

Between the users and movies there can be an edge "watched"在用户和电影之间可以有一个边缘“观看”

My example data looks as follows:我的示例数据如下所示:

watched:
[bob, [movie1,movie2]]
[jose, [movie3]]
[frank, []]
[peter, [movie]]

The result and format I would like to get is following:我想得到的结果和格式如下:

not watched:
[bob, movie3]
[bob, movie4]
[jose, movie1]
[jose, movie2]
[jose, movie4]
[frank, movie1]
[frank, movie2]
[frank, movie3]
[frank, movie4]
[peter, movie1]
[peter, movie2]
[peter, movie3]

The script to set up the graph (using /partition_key as partition key):设置图形的脚本(使用 /partition_key 作为分区键):

g.addV("user").property("partition_key", 1).property("id", "bob")
g.addV("user").property("partition_key", 1).property("id", "jose")
g.addV("user").property("partition_key", 1).property("id", "frank")
g.addV("user").property("partition_key", 1).property("id", "peter")

g.addV("movie").property("partition_key", 1).property("id", "movie1")
g.addV("movie").property("partition_key", 1).property("id", "movie2")
g.addV("movie").property("partition_key", 1).property("id", "movie3")
g.addV("movie").property("partition_key", 1).property("id", "movie4")

g.V("bob").addE("watched").to(g.V("movie1"))
g.V("bob").addE("watched").to(g.V("movie2"))
g.V("jose").addE("watched").to(g.V("movie3"))
g.V("peter").addE("watched").to(g.V("movie4"))

Please consider, that I cannot use lambdas, because Azure CosmosDB doesn't support them.请考虑,我不能使用 lambda,因为 Azure CosmosDB 不支持它们。

A join in gremlin can be realized by repeating the V() step.可以通过重复 V() 步骤来实现 gremlin 中的连接。 After realizing that, the gremlin query almost reads as an ordinary SQL query, see below.意识到这一点后,gremlin 查询几乎读作一个普通的 SQL 查询,见下文。

g.V().has("id", "bob").addE("watched").to(__.V().has("id", "movie1"))
g.V().has("id", "bob").addE("watched").to(__.V().has("id", "movie2"))
g.V().has("id", "jose").addE("watched").to(__.V().has("id", "movie3"))
g.V().has("id", "peter").addE("watched").to(__.V().has("id", "movie4"))

g.V().hasLabel("user").as("u").
  V().hasLabel("movie").as("m").
  in("watched").where(neq("u")).
  select("u", "m").by("id").
  order().by("u").by("m")

==>[u:bob,m:movie3]
==>[u:bob,m:movie4]
==>[u:frank,m:movie1]
==>[u:frank,m:movie2]
==>[u:frank,m:movie3]
==>[u:frank,m:movie4]
==>[u:jose,m:movie1]
==>[u:jose,m:movie2]
==>[u:jose,m:movie4]
==>[u:peter,m:movie1]
==>[u:peter,m:movie2]
==>[u:peter,m:movie3]

You are right in saying that this query does not perform well in gremlin and I would advise you to use the SQL API of CosmosDb.你说这个查询在 gremlin 中表现不佳是正确的,我建议你使用 CosmosDb 的 SQL API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM