如何使用gremlin API在Azure Cosmos DB中进行左联接

时间:2019-10-26 07:58:39

标签: azure-cosmosdb graph-databases azure-cosmosdb-gremlinapi

我正在研究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'))

现在您可以看到并非所有产品都有图像。

我希望能够选择具有类别和图像属性的所有产品。 如果产品没有图像,则仍应选择具有null图像属性的产品。

我正在尝试通过以下链接来执行类似于左联接的操作: http://sql2gremlin.com/#_left_join

但是不幸的是,Azure Cosmos的Gremlin API尚不支持match()步骤。

我当前的查询仅选择图像边缘向外的产品:

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 个答案:

答案 0 :(得分:1)

所以这里发生了几件事:

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')
      )
    )

这可以通过查找所有产品顶点,然后返回图像和类别来实现

3)那里的合并语句基于this Gremlin配方检查元素是否存在。如果找不到任何东西,因为没有配方,它将返回一个常量值

4)Gremlin不允许返回null值,因此您需要返回一些内容

更新的添加脚本

//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)