Gremlin 如何遍历所有顶点并获取嵌套对象
我想以Gremlin 如何遍历所有顶点并获取嵌套对象,gremlin,gremlin-server,Gremlin,Gremlin Server,我想以 { country : {code:'IN',states: {code:'TG',cities: {code:'HYD',malls: {[shopping-mall1],[shopping-mall2],.....} }, {code:'PKL',malls: {[shopping-mall1],[shopping-mall2],.....} }
{ country :
{code:'IN',states:
{code:'TG',cities:
{code:'HYD',malls:
{[shopping-mall1],[shopping-mall2],.....}
},
{code:'PKL',malls:
{[shopping-mall1],[shopping-mall2],.....}
}
},
{code:'AP',cities:
{code:'VJY',malls:
{[shopping-mall1],[shopping-mall2],.....}
}
}
}
}
我的图表是格式化的
vertex: country ---> states ---->cities ---> mallls
edges: (type:'state') ('type','city')
ex: inE('typeOf').outV().has('type','state') move to next vertex "states".
next same inE('typeOf').outV().has('type','city') moves to "city" vertex. then "malls" vertex .
厌倦了编写代码,一些顶点没有城市,我有一个错误,这种情况下
错误
这就是我之所以使用coalesce的原因,因为有些州没有边'inE'partOf'。outV.有'type','city意味着没有城市
.by(coalesce(select('states').inE('partOf').outV().has('type','city'))
我的问题
g.V().hasLabel('Country').has('code','IN')
.project('country')
.by(project('code','states')
.by(values('code'))
.by(inE('partOf').outV().has('type','state').has('code').as('states').
project('code','cities')
.by(select('states').values('code'))
.by(coalesce(select('states').inE('partOf').outV().
has('type','city').has('code').as('cities').
project('code','malls')
.by(select('cities').values('code'))
.by(coalesce(select('cities').inE('partOf').outV().
has('type','malls').valueMap(),constant(0))),
constant(0)))))
但结果是
{country={code=IN, states={code=DD, cities=0}}}
here i am getting one state 'DD' and that state is no city,so it gives 'cities = 0".
上面的结果是只有一个州来了,我想要每个城市的所有州、城市和商场
请更新查询或更改查询,以收集您应该使用的所有结果。折叠遍历将返回所收集遍历的列表。如果没有fold,您将只获得第一次遍历,如示例中所示 为了保持类型不变,我将常量改为[],而不是0 还不清楚type属性是在边上还是在顶点上。我觉得把它放在边缘更合适,所以我也通过移动has'type'来修复它,。。。在inE和outV之间 最后,您不需要使用as存储遍历,然后选择它 此查询应提供所需的结果:
g.V().hasLabel('Country').has('code','IN')
.project('country')
.by(project('code','states')
.by(values('code'))
.by(inE('partOf').has('type','state').outV().has('code')
.project('code','cities')
.by(values('code'))
.by(coalesce(inE('partOf').has('type','city').outV().has('code')
.project('code','malls')
.by(values('code'))
.by(coalesce(
inE('partOf').has('type','malls').outV().valueMap(),
constant([])).fold()),
constant([])).fold())
.fold()))
你能不能提供一个小的图表,以小精灵脚本g.addV的形式…?上面添加了图片,请看里面的脚本形式,除非你有耐心等待有人将图片翻译成真实的图表。
g.V().hasLabel('Country').has('code','IN')
.project('country')
.by(project('code','states')
.by(values('code'))
.by(inE('partOf').has('type','state').outV().has('code')
.project('code','cities')
.by(values('code'))
.by(coalesce(inE('partOf').has('type','city').outV().has('code')
.project('code','malls')
.by(values('code'))
.by(coalesce(
inE('partOf').has('type','malls').outV().valueMap(),
constant([])).fold()),
constant([])).fold())
.fold()))