采用keys()查询:
MATCH (n:Test) RETURN distinct keys(n)
解析查询结果:
public static List<Object> objectsList(StatementResult result){
List<Object> list = new ArrayList<>();
while(result.hasNext()){
Record record = result.next();
for(int i=0;i<record.values().size();i++){
Value value = record.values().get(i);
list.add(value.asList());
}
}
return list;
}
测试:
public static void main(String[] args) {
List<String> keys = new ArrayList<>();
String cql = "MATCH (n:Test) RETURN distinct keys(n)";
StatementResult queryResult = Neo4jUtil.query(cql);
List<Object> parseResult = Neo4jResultUtils.objectsList(queryResult);
for (Object listObject : parseResult) {
if (listObject instanceof List) {
for (Object strOb : ((List) listObject).subList(0, ((List) listObject).size())) {
if (strOb instanceof String) {
if (!keys.contains(strOb)) {
keys.add(strOb.toString());
}
}
}
}
}
System.out.println("keys:" + keys);
}
Neo4jUtil----执行对应的cypher语句并返回json形式数据的工具
Neo4jResultUtils----解析查询结果的工具,objectsList是该工具中的一个方法