如何在 Gremlin 查询中使用“project”步骤和“tree”步骤

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

我希望能够以类别的子级位于

children
属性内的方式在图形数据库中获取我的类别。

我知道这可以通过项目步骤实现,但是如何将其与树步骤一起使用?

这就是我想要实现的数组结构:

[
    {
        'name' : 'shoes',
        'children': [
            {
                'name' : 'nike',
                'children' : [
                    {
                        'name' : 'jordan 1',
                        'children' : []
                    },
                    {
                        'name' : 'air force 1',
                        'children' : []
                    },
                ],
            },
            {
                'name' : 'adidas',
                'children': [
                    {
                        'name' : 'nike',
                        'children' : [
                            {
                                'name' : 'yeezy',
                                'children' : []
                            },
                            {
                                'name' : 'stan smith',
                                'children' : []
                            },
                        ],
                    },
                ],
            },
        ],
    },
]

请参见下图查看图形数据库。 enter image description here

使用下面的代码从上图中为您的图形数据库播种。

g
.addV('category')
.property('name', 'shoes')
.as('shoes')
.addE('has')
.to(
    addV('category')
    .property('name', 'nike')
    .as('nike')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'air force 1')
    )
    .select('nike')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'jordan 1')
    )
    .select('nike')
)
.select('shoes')
.addE('has')
.to(
    addV('category')
    .property('name', 'adidas')
    .as('adidas')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'yeezy')
    )
    .select('adidas')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'stan smith')
    )
    .select('adidas')
)
gremlin graph-databases
1个回答
0
投票

这里的问题类似:如何将树格式的 Gremlin GraphSON 转换为 CosmosDB 中的自定义 JSON 树格式?

Java GLV 更好地实现了

tree()
步骤,因为有一个本机类可以将结果返回到:https://tinkerpop.apache.org/javadocs/current/full/org/apache/ Tinkerpop/gremlin/process/traversal/step/util/Tree.html

其他 GLV 将返回类似 JSON (GraphSON) 的详细结果。这主要是因为其他 GLV(如 Python、Node 等)尚未实现 Tree 类,或者没有树可以映射到的任何其他数据类型/结构。您最好返回整个树 JSON 对象并创建一个方法将其转换为您想要的格式。

© www.soinside.com 2019 - 2024. All rights reserved.