Search in sources :

Example 16 with Record

use of org.neo4j.driver.Record 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)

Example 17 with Record

use of org.neo4j.driver.Record in project zeppelin by apache.

the class Neo4jCypherInterpreter method getLabels.

public Map<String, String> getLabels(boolean refresh) {
    if (labels == null || refresh) {
        Map<String, String> old = labels == null ? new LinkedHashMap<>() : new LinkedHashMap<>(labels);
        labels = new LinkedHashMap<>();
        Iterator<Record> result = this.neo4jConnectionManager.execute("CALL db.labels()").iterator();
        Set<String> colors = new HashSet<>();
        while (result.hasNext()) {
            Record record = result.next();
            String label = record.get("label").asString();
            String color = old.get(label);
            while (color == null || colors.contains(color)) {
                color = Neo4jConversionUtils.getRandomLabelColor();
            }
            colors.add(color);
            labels.put(label, color);
        }
    }
    return labels;
}
Also used : Record(org.neo4j.driver.Record) HashSet(java.util.HashSet)

Example 18 with Record

use of org.neo4j.driver.Record in project zeppelin by apache.

the class Neo4jCypherInterpreter method getTypes.

private Set<String> getTypes(boolean refresh) {
    if (types == null || refresh) {
        types = new HashSet<>();
        Iterator<Record> result = this.neo4jConnectionManager.execute("CALL db.relationshipTypes()").iterator();
        while (result.hasNext()) {
            Record record = result.next();
            types.add(record.get("relationshipType").asString());
        }
    }
    return types;
}
Also used : Record(org.neo4j.driver.Record)

Example 19 with Record

use of org.neo4j.driver.Record in project spring-boot by spring-projects.

the class Neo4jHealthIndicator method runHealthCheckQuery.

private void runHealthCheckQuery(Health.Builder builder) {
    // all possible workloads
    try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) {
        Result result = session.run(CYPHER);
        Record record = result.single();
        ResultSummary resultSummary = result.consume();
        this.healthDetailsHandler.addHealthDetails(builder, new Neo4jHealthDetails(record, resultSummary));
    }
}
Also used : ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result)

Example 20 with Record

use of org.neo4j.driver.Record in project neo4j by neo4j.

the class CypherShellTest method setParamShouldAddParamWithSpecialCharactersAndValue.

@Test
public void setParamShouldAddParamWithSpecialCharactersAndValue() throws ParameterException, CommandException {
    Value value = mock(Value.class);
    Record recordMock = mock(Record.class);
    BoltResult boltResult = new ListBoltResult(asList(recordMock), mock(ResultSummary.class));
    when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenReturn(Optional.of(boltResult));
    when(recordMock.get("bo`b")).thenReturn(value);
    when(value.asObject()).thenReturn("99");
    assertTrue(offlineTestShell.getParameterMap().allParameterValues().isEmpty());
    Object result = offlineTestShell.getParameterMap().setParameter("`bo``b`", "99");
    assertEquals(99L, result);
    assertEquals(99L, offlineTestShell.getParameterMap().allParameterValues().get("bo`b"));
}
Also used : ListBoltResult(org.neo4j.shell.state.ListBoltResult) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Mockito.anyObject(org.mockito.Mockito.anyObject) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Aggregations

Record (org.neo4j.driver.Record)33 Test (org.junit.Test)21 Value (org.neo4j.driver.Value)21 ListBoltResult (org.neo4j.shell.state.ListBoltResult)18 ResultSummary (org.neo4j.driver.summary.ResultSummary)12 BoltResult (org.neo4j.shell.state.BoltResult)11 InternalRecord (org.neo4j.driver.internal.InternalRecord)10 HashMap (java.util.HashMap)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Result (org.neo4j.driver.Result)8 StringContains.containsString (org.hamcrest.core.StringContains.containsString)7 Node (org.neo4j.driver.types.Node)7 Relationship (org.neo4j.driver.types.Relationship)7 Session (org.neo4j.driver.Session)6 ArrayList (java.util.ArrayList)5 Driver (org.neo4j.driver.Driver)5 List (java.util.List)4 DurationValue (org.neo4j.driver.internal.value.DurationValue)4 NodeValue (org.neo4j.driver.internal.value.NodeValue)4 PathValue (org.neo4j.driver.internal.value.PathValue)4