我正在尝试将顶点的所有属性放在地图中,并向该地图添加其他值。稍后,我想将此映射作为JSON响应返回。一般来说,我发现neo4j的REST-API有点冗长,除了其他值之外,我还需要顶点的ID。因此,我决定直接通过gremlin查询完成此操作。
结果应该如下所示:
[ {"id": 1, "name": "Name1"}, {"id": 2, "name": "Name2"} ]
我使用以下gremlin脚本成功做到了这一点:
x = [];
g.v( 1 ).out.transform{
m = [:];
m.putAll( it.map() );
m.put( "id", it.id );
x.add( m )
}.iterate();
x
然而,我在neo4j社区1.8.2和1.9M05的查询中遇到了问题。
a) neo4j 1.8.2返回字符串的JSONArray,而不是JSONObjects的JSONArray:
[ "{id: 1, name: Name1}", "{id: 2, name: Name2}" ]
b) neo4j 1.9M5返回异常:
{
"message":"Invalid list type: map",
"exception":"IllegalStateException",
"stacktrace":
[
"org.neo4j.server.rest.repr.RepresentationFormat.serializeList(RepresentationFormat.java:65)",
"org.neo4j.server.rest.repr.ListRepresentation.serialize(ListRepresentation.java:50)",
"org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:179)",
"org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:131)",
"org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:117)",
"org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:55)",
"org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:122)",
"java.lang.reflect.Method.invoke(Method.java:597)"
]
}
如果我修改gremlin脚本并将x更改为映射,则它在两个neo4j版本中都有效:
x = [:];
g.v( 1 ).out.transform{
m = [:];
m.putAll( it.map() );
m.put( "id", it.id );
x.put(it.id, m );
}.iterate();
x
返回
{ "1" : {"id": 1, "name": "Name1"}, "2" : {"id": 2, "name": "Name2"} }
然而,结果现在是一个包含JSONObjects的JSONObject。
有没有办法用gremlin解决我的问题?我两天前才开始学习gremlin。
发布于 2013-04-18 22:27:19
由于neo4j的原因,这两种情况都不起作用。当涉及到自定义地图时,1.8.2版的JSONSerialization很差。已提供1.9版的修复程序。
https://stackoverflow.com/questions/15571058
复制相似问题