如何遍历所有顶点并获取嵌套对象

时间:2019-08-10 13:48:40

标签: gremlin gremlin-server

enter image description here我想以

的形式获取嵌套对象
{ 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],.....}
         }
      }
   }
}

MY图的格式

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 .

由于厌倦了编写代码,某些顶点没有城市,所以我有一个错误之处。”

错误

The provided traverser does not map to a value: v[8320]->[JanusGraphVertexStep(IN,[partOf],vertex), HasStep([type.eq(city)]), JanusGraphPropertiesStep([code],value)]

这就是为什么我使用合并的原因,因为某些州没有边缘'inE('partOf')。outV()。has('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".

以上结果是只有一个州来临,我希望每个城市中的所有州,城市和购物中心都可以。

请更新查询或更改查询

1 个答案:

答案 0 :(得分:2)

为了收集所有结果,您应该使用.fold()遍历,它返回收集到的遍历的列表。没有折叠,您将只获得示例中的第一个遍历。

为了保持类型相同,我将常量更改为[]而不是0。

还不清楚“ type”属性是在边缘还是在顶点。我发现将其放在边缘更合适,因此我也通过在inE()和outV()之间移动has('type',...)来固定它。

最后,您不需要使用“ as”来“存储”遍历,然后“选择”它。

此查询应为您提供所需的结果:

myres = [[[[0.07338447]],[[0.92661554]]]]
for i in myres[0]:
    print(i[0][0])
相关问题