use of org.neo4j.driver.util.Pair in project zeppelin by apache.
the class Neo4jCypherInterpreter method runQuery.
private InterpreterResult runQuery(String cypherQuery, InterpreterContext interpreterContext) {
if (StringUtils.isBlank(cypherQuery)) {
return new InterpreterResult(Code.SUCCESS);
}
try {
Iterator<Record> result = this.neo4jConnectionManager.execute(cypherQuery, interpreterContext).iterator();
Set<Node> nodes = new HashSet<>();
Set<Relationship> relationships = new HashSet<>();
List<String> columns = new ArrayList<>();
List<List<String>> lines = new ArrayList<List<String>>();
while (result.hasNext()) {
Record record = result.next();
List<Pair<String, Value>> fields = record.fields();
List<String> line = new ArrayList<>();
for (Pair<String, Value> field : fields) {
if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.NODE())) {
nodes.add(field.value().asNode());
} else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP())) {
relationships.add(field.value().asRelationship());
} else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.PATH())) {
nodes.addAll(Iterables.asList(field.value().asPath().nodes()));
relationships.addAll(Iterables.asList(field.value().asPath().relationships()));
} else {
setTabularResult(field.key(), field.value(), columns, line, InternalTypeSystem.TYPE_SYSTEM);
}
}
if (!line.isEmpty()) {
lines.add(line);
}
}
if (!nodes.isEmpty()) {
return renderGraph(nodes, relationships);
} else {
return renderTable(columns, lines);
}
} catch (Exception e) {
LOGGER.error("Exception while interpreting cypher query", e);
return new InterpreterResult(Code.ERROR, e.getMessage());
}
}
Aggregations