Gemlin 查询将树输出为特定格式

问题描述 投票:0回答:1

首先,我想说我对此很陌生,所以对任何错误表示歉意!

我有一个相当简单的食谱和成分层次结构。 我正在创建一个输出类似以下内容的查询:

{
  "id": "sandwich-a",
  "name": "Sandwich A",
  "composedOf": [
    {
      "id": "tomato",
      "name": "Tomato",
      "weight": 40,
      "composedOf": []
    },
    {
      "id": "sauce-a",
      "name": "Sauce A",
      "weight": 2.8,
      "composedOf": [
        {
          "id": "salt",
          "name": "Salt",
          "weight": null,
          "composedOf": []
        },
        {
          "id": "pepper",
          "name": "Pepper",
          "weight": null,
          "composedOf": []
        }
      ]
    },
    {
      "id": "sauce-b",
      "name": "Sauce B",
      "weight": null,
      "composedOf": [
        {
          "id": "water",
          "name": "Water",
          "weight": null,
          "composedOf": []
        },
        {
          "id": "lemon-juice",
          "name": "Lemon Juice",
          "weight": null,
          "composedOf": []
        }
      ]
    }
  ]
}

idname是顶点的属性。
权重是边缘上的一个属性,可能存在也可能不存在。
composedOf 是由带有标签 composed-of 的边连接的顶点的集合。

这是我迄今为止的疑问:

g.V()
    .has("id", "new-york-ciabatta")
    .project("id", "name", "composedOf")
        .by("id")
        .by("name")
        .by(
            repeat(
                out("composed-of")
                .outE().as('e')
                .inV().as('v')
                .simplePath()
                )
            .emit()
            .tree())

它可以工作,但会发送大量附加数据。我怎样才能让它输出类似于上面的 JSON?

Gremlify 示例:https://gremlify.com/dxy4uwpan7n/2

gremlin
1个回答
0
投票

我必须调整提供的示例图形数据和查询才能使其正常工作。这是我从 Gremlify 导出并修改后使用的图形数据:

g.addV('recipe').as('1').property(single, 'name', 'New York Ciabatta').property(single, 'clientId', 'XYZ').
  addV('ingredient').as('2').property(single, 'name', 'Water').property(single, 'clientId', 'XYZ').
  addV('ingredient').as('3').property(single, 'name', 'Salt').property(single, 'clientId', 'XYZ').property(single, 'saltContent', 100).
  addV('ingredient').as('4').property(single, 'name', 'Pepper').property(single, 'clientId', 'XYZ').
  addV('ingredient').as('5').property(single, 'name', 'Mustard').property(single, 'clientId', 'XYZ').
  addV('ingredient').as('6').property(single, 'name', 'Paprika').property(single, 'clientId', 'XYZ').
  addV('compound-ingredient').as('7').property(single, 'name', 'American Mustard').property(single, 'clientId', 'XYZ').
  addV('compound-ingredient').as('8').property(single, 'name', 'Cajun Seasoning Mix').property(single, 'clientId', 'XYZ').property(single, 'saltContent', 28.7).
  addE('composed-of').from('1').to('7').
  addE('composed-of').from('7').to('2').
  addE('composed-of').from('7').to('3').property('weight', 2.9).
  addE('composed-of').from('7').to('5').
  addE('composed-of').from('8').to('3').
  addE('composed-of').from('8').to('6').
  addE('composed-of').from('8').to('4')

tree
步骤可以采用
by
调制器,它允许您指定要在输出中包含哪些字段。例如,为了减少信息量,您可以尝试以下操作:

g.V().
  has("name", "New York Ciabatta").
  repeat(outE("composed-of").inV().simplePath()).
  until(not(out())).
  tree().
    by(label)
© www.soinside.com 2019 - 2024. All rights reserved.