Search in sources :

Example 1 with MapResult

use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.

the class Json method toTree.

@Procedure("apoc.convert.toTree")
@Description("apoc.convert.toTree([paths],[lowerCaseRels=true]) creates a stream of nested documents representing the at least one root of these paths")
public // todo optinally provide root node
Stream<MapResult> toTree(@Name("paths") List<Path> paths, @Name(value = "lowerCaseRels", defaultValue = "true") boolean lowerCaseRels) {
    if (paths.isEmpty())
        return Stream.of(new MapResult(Collections.emptyMap()));
    Map<Long, Map<String, Object>> maps = new HashMap<>(paths.size() * 100);
    for (Path path : paths) {
        Iterator<PropertyContainer> it = path.iterator();
        while (it.hasNext()) {
            Node n = (Node) it.next();
            Map<String, Object> nMap = maps.computeIfAbsent(n.getId(), (id) -> toMap(n));
            if (it.hasNext()) {
                Relationship r = (Relationship) it.next();
                Node m = r.getOtherNode(n);
                Map<String, Object> mMap = maps.computeIfAbsent(m.getId(), (id) -> toMap(m));
                String typeName = r.getType().name();
                if (lowerCaseRels)
                    typeName = typeName.toLowerCase();
                mMap = addRelProperties(mMap, typeName, r);
                // parent-[:HAS_CHILD]->(child) vs. (parent)<-[:PARENT_OF]-(child)
                if (!nMap.containsKey(typeName))
                    nMap.put(typeName, new ArrayList<>(16));
                List list = (List) nMap.get(typeName);
                if (!list.contains(mMap))
                    // todo performance, use set instead and convert to map at the end?
                    list.add(mMap);
            }
        }
    }
    return paths.stream().map(Path::startNode).distinct().map(n -> maps.remove(n.getId())).map(m -> m == null ? Collections.<String, Object>emptyMap() : m).map(MapResult::new);
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) java.util(java.util) java.util.stream(java.util.stream) MapResult(apoc.result.MapResult) ObjectResult(apoc.result.ObjectResult) org.neo4j.procedure(org.neo4j.procedure) ListResult(apoc.result.ListResult) Description(org.neo4j.procedure.Description) JsonPath(com.jayway.jsonpath.JsonPath) StringResult(apoc.result.StringResult) JsonUtil(apoc.util.JsonUtil) java.io(java.io) org.neo4j.graphdb(org.neo4j.graphdb) Util(apoc.util.Util) MapResult(apoc.result.MapResult) Description(org.neo4j.procedure.Description)

Example 2 with MapResult

use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.

the class Xml method xmlXpathToMapResult.

private Stream<MapResult> xmlXpathToMapResult(@Name("url") String url, boolean simpleMode, String path, Map<String, Object> config) throws Exception {
    if (config == null)
        config = Collections.emptyMap();
    boolean failOnError = (boolean) config.getOrDefault("failOnError", true);
    List<MapResult> result = new ArrayList<>();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        FileUtils.checkReadAllowed(url);
        Document doc = documentBuilder.parse(Util.openInputStream(url, Collections.emptyMap(), null));
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        path = StringUtils.isEmpty(path) ? "/" : path;
        XPathExpression xPathExpression = xPath.compile(path);
        NodeList nodeList = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Deque<Map<String, Object>> stack = new LinkedList<>();
            handleNode(stack, nodeList.item(i), simpleMode);
            for (int index = 0; index < stack.size(); index++) {
                result.add(new MapResult(stack.pollFirst()));
            }
        }
    } catch (FileNotFoundException e) {
        if (!failOnError)
            return Stream.of(new MapResult(Collections.emptyMap()));
        else
            throw new FileNotFoundException(e.getMessage());
    } catch (Exception e) {
        if (!failOnError)
            return Stream.of(new MapResult(Collections.emptyMap()));
        else
            throw new Exception(e);
    }
    return result.stream();
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) FileNotFoundException(java.io.FileNotFoundException) MapResult(apoc.result.MapResult) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 3 with MapResult

use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.

the class LoadJson method loadJsonStream.

public static Stream<MapResult> loadJsonStream(@Name("url") String url, @Name("headers") Map<String, Object> headers, @Name("payload") String payload, String path, boolean failOnError) {
    headers = null != headers ? headers : new HashMap<>();
    headers.putAll(extractCredentialsIfNeeded(url, failOnError));
    Stream<Object> stream = JsonUtil.loadJson(url, headers, payload, path, failOnError);
    return stream.flatMap((value) -> {
        if (value instanceof Map) {
            return Stream.of(new MapResult((Map) value));
        }
        if (value instanceof List) {
            if (((List) value).isEmpty())
                return Stream.empty();
            if (((List) value).get(0) instanceof Map)
                return ((List) value).stream().map((v) -> new MapResult((Map) v));
            return Stream.of(new MapResult(Collections.singletonMap("result", value)));
        }
        if (!failOnError)
            throw new RuntimeException("Incompatible Type " + (value == null ? "null" : value.getClass()));
        else
            return Stream.of(new MapResult(Collections.emptyMap()));
    });
}
Also used : MapUtil(apoc.util.MapUtil) java.util(java.util) Stream(java.util.stream.Stream) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Context(org.neo4j.procedure.Context) MapResult(apoc.result.MapResult) ObjectResult(apoc.result.ObjectResult) Name(org.neo4j.procedure.Name) Description(org.neo4j.procedure.Description) URI(java.net.URI) Procedure(org.neo4j.procedure.Procedure) JsonUtil(apoc.util.JsonUtil) MapResult(apoc.result.MapResult)

Example 4 with MapResult

use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.

the class Xml method xmlToMapResult.

private Stream<MapResult> xmlToMapResult(@Name("url") String url, boolean simpleMode) {
    try {
        XMLStreamReader reader = getXMLStreamReaderFromUrl(url);
        final Deque<Map<String, Object>> stack = new LinkedList<>();
        do {
            handleXmlEvent(stack, reader, simpleMode);
        } while (proceedReader(reader));
        return Stream.of(new MapResult(stack.getFirst()));
    } catch (IOException | XMLStreamException e) {
        throw new RuntimeException("Can't read url " + cleanUrl(url) + " as XML", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) MapResult(apoc.result.MapResult)

Example 5 with MapResult

use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.

the class Meta method schema.

@Procedure
@Description("apoc.meta.schema  - examines a subset of the graph to provide a map-like meta information")
public Stream<MapResult> schema() {
    MetaStats metaStats = collectStats();
    Map<String, Map<String, MetaResult>> metaData = collectMetaData();
    Map<String, Object> relationships = collectRelationshipsMetaData(metaStats, metaData);
    Map<String, Object> nodes = collectNodesMetaData(metaStats, metaData, relationships);
    nodes.putAll(relationships);
    return Stream.of(new MapResult(nodes));
}
Also used : Collectors.toMap(java.util.stream.Collectors.toMap) Collections.singletonMap(java.util.Collections.singletonMap) MapResult(apoc.result.MapResult)

Aggregations

MapResult (apoc.result.MapResult)8 IOException (java.io.IOException)3 ObjectResult (apoc.result.ObjectResult)2 JsonUtil (apoc.util.JsonUtil)2 java.util (java.util)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Description (org.neo4j.procedure.Description)2 ListResult (apoc.result.ListResult)1 StringResult (apoc.result.StringResult)1 MapUtil (apoc.util.MapUtil)1 QueueBasedSpliterator (apoc.util.QueueBasedSpliterator)1 Util (apoc.util.Util)1 JsonPath (com.jayway.jsonpath.JsonPath)1 java.io (java.io)1 FileNotFoundException (java.io.FileNotFoundException)1 URI (java.net.URI)1 Collections.singletonList (java.util.Collections.singletonList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1