如何使用 gremlin API 在 Azure Cosmos DB 中进行左连接
How to do left join in Azure Cosmos DB using gremlin API
我正在研究 Azure Cosmos DB 的 Gremlin API,我正在尝试将一些 SQL 语句转换为 Gremlin 遍历。
以下命令:
//add product vertex
g.addV('product').property('id', 'product1')
g.addV('product').property('id', 'product2')
g.addV('product').property('id', 'product3')
g.addV('product').property('id', 'product4')
g.addV('product').property('id', 'product5')
//add product_category vertex
g.addV('product_category').property('id', 'category1')
g.addV('product_category').property('id', 'category2')
//connect product and product_categories
g.V('product1').addE('belongs_to').to(g.V('category1'))
g.V('product2').addE('belongs_to').to(g.V('category1'))
g.V('product3').addE('belongs_to').to(g.V('category2'))
g.V('product4').addE('belongs_to').to(g.V('category2'))
g.V('product5').addE('belongs_to').to(g.V('category2'))
//add image vertex
g.addV('image').property('public_url', 'url_1').property('id', 'image1')
g.addV('image').property('public_url', 'url_2').property('id', 'image2')
//link products to images
g.V('product1').addE('belongs_to').property('primary', true).to(g.V('image1'))
g.V('product2').addE('belongs_to').property('primary', true).to(g.V('image2'))
现在您可以看到,并非所有产品都有图片。
我希望能够 select 具有类别和图像 属性 的所有产品。
如果产品没有图片,它仍应 select 使用空图片 属性。
我试图通过遵循此 link 来执行类似于左连接的操作:
http://sql2gremlin.com/#_left_join
但不幸的是 Azure Cosmos 的 Gremlin API 尚不支持 match() 步骤。
我的当前查询仅 selects 具有图像输出边缘的产品:
g.V()
.has('label', 'product')
.as('product')
.outE('has_image')
.has('primary', true)
.inV()
.as('primary_image')
.in('has_image')
.out('belongs_to')
.as('category')
.select('product','primary_image','category')
.by(__.valueMap()).by(__.values('public_url')).by(__.values('name'))
所以这里发生了一些事情:
1) 您上面的脚本无法正常将数据插入 TinkerGraph(我没有 Cosmos 帐户)。我在下面添加了更新的脚本。
2) 您正在尝试的不需要左连接。由于这些都是产品的关系,您可以从产品顶点投影结果,如下所示:
g.V().
hasLabel('product').
project('product', 'category', 'image').
by(id()).
by(out('belongs_to').id()).
by(
__.out('has_image').fold().
coalesce(
unfold().id(),
constant('No Image')
)
)
这通过查找所有产品顶点然后 return 图像和类别
来实现
3) 那里的合并语句基于 this Gremlin 配方来检查元素是否存在。如果它没有找到任何东西,因为配方不存在它 return 是一个常量值
4) Gremlin 不允许 returning a null
值所以你需要 return 一些东西
已更新添加脚本
//add product vertex
g.addV('product').property(id, 'product1')
g.addV('product').property(id, 'product2')
g.addV('product').property(id, 'product3')
g.addV('product').property(id, 'product4')
g.addV('product').property(id, 'product5')
//add product_category vertex
g.addV('product_category').property(id, 'category1')
g.addV('product_category').property(id, 'category2')
//connect product and product_categories
g.V('product1').addE('belongs_to').to(g.V('category1'))
g.V('product2').addE('belongs_to').to(g.V('category1'))
g.V('product3').addE('belongs_to').to(g.V('category2'))
g.V('product4').addE('belongs_to').to(g.V('category2'))
g.V('product5').addE('belongs_to').to(g.V('category2'))
//add image vertex
g.addV('image').property(id, 'image1').property('public_url', 'url_1')
g.addV('image').property(id, 'image2').property('public_url', 'url_2')
//link products to images
g.V('product1').addE('has_image').to(V('image1')).property('primary', true)
g.V('product2').addE('has_image').to(V('image2')).property('primary', true)
我正在研究 Azure Cosmos DB 的 Gremlin API,我正在尝试将一些 SQL 语句转换为 Gremlin 遍历。
以下命令:
//add product vertex
g.addV('product').property('id', 'product1')
g.addV('product').property('id', 'product2')
g.addV('product').property('id', 'product3')
g.addV('product').property('id', 'product4')
g.addV('product').property('id', 'product5')
//add product_category vertex
g.addV('product_category').property('id', 'category1')
g.addV('product_category').property('id', 'category2')
//connect product and product_categories
g.V('product1').addE('belongs_to').to(g.V('category1'))
g.V('product2').addE('belongs_to').to(g.V('category1'))
g.V('product3').addE('belongs_to').to(g.V('category2'))
g.V('product4').addE('belongs_to').to(g.V('category2'))
g.V('product5').addE('belongs_to').to(g.V('category2'))
//add image vertex
g.addV('image').property('public_url', 'url_1').property('id', 'image1')
g.addV('image').property('public_url', 'url_2').property('id', 'image2')
//link products to images
g.V('product1').addE('belongs_to').property('primary', true).to(g.V('image1'))
g.V('product2').addE('belongs_to').property('primary', true).to(g.V('image2'))
现在您可以看到,并非所有产品都有图片。
我希望能够 select 具有类别和图像 属性 的所有产品。 如果产品没有图片,它仍应 select 使用空图片 属性。
我试图通过遵循此 link 来执行类似于左连接的操作: http://sql2gremlin.com/#_left_join
但不幸的是 Azure Cosmos 的 Gremlin API 尚不支持 match() 步骤。
我的当前查询仅 selects 具有图像输出边缘的产品:
g.V()
.has('label', 'product')
.as('product')
.outE('has_image')
.has('primary', true)
.inV()
.as('primary_image')
.in('has_image')
.out('belongs_to')
.as('category')
.select('product','primary_image','category')
.by(__.valueMap()).by(__.values('public_url')).by(__.values('name'))
所以这里发生了一些事情:
1) 您上面的脚本无法正常将数据插入 TinkerGraph(我没有 Cosmos 帐户)。我在下面添加了更新的脚本。
2) 您正在尝试的不需要左连接。由于这些都是产品的关系,您可以从产品顶点投影结果,如下所示:
g.V().
hasLabel('product').
project('product', 'category', 'image').
by(id()).
by(out('belongs_to').id()).
by(
__.out('has_image').fold().
coalesce(
unfold().id(),
constant('No Image')
)
)
这通过查找所有产品顶点然后 return 图像和类别
来实现3) 那里的合并语句基于 this Gremlin 配方来检查元素是否存在。如果它没有找到任何东西,因为配方不存在它 return 是一个常量值
4) Gremlin 不允许 returning a null
值所以你需要 return 一些东西
已更新添加脚本
//add product vertex
g.addV('product').property(id, 'product1')
g.addV('product').property(id, 'product2')
g.addV('product').property(id, 'product3')
g.addV('product').property(id, 'product4')
g.addV('product').property(id, 'product5')
//add product_category vertex
g.addV('product_category').property(id, 'category1')
g.addV('product_category').property(id, 'category2')
//connect product and product_categories
g.V('product1').addE('belongs_to').to(g.V('category1'))
g.V('product2').addE('belongs_to').to(g.V('category1'))
g.V('product3').addE('belongs_to').to(g.V('category2'))
g.V('product4').addE('belongs_to').to(g.V('category2'))
g.V('product5').addE('belongs_to').to(g.V('category2'))
//add image vertex
g.addV('image').property(id, 'image1').property('public_url', 'url_1')
g.addV('image').property(id, 'image2').property('public_url', 'url_2')
//link products to images
g.V('product1').addE('has_image').to(V('image1')).property('primary', true)
g.V('product2').addE('has_image').to(V('image2')).property('primary', true)