Search in sources :

Example 1 with Pair

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());
    }
}
Also used : Node(org.neo4j.driver.types.Node) ArrayList(java.util.ArrayList) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.neo4j.driver.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 Record (org.neo4j.driver.Record)1 Value (org.neo4j.driver.Value)1 Node (org.neo4j.driver.types.Node)1 Relationship (org.neo4j.driver.types.Relationship)1 Pair (org.neo4j.driver.util.Pair)1