# Stream spanning tree to an output file

**URL:** <https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338>\
**Category:** Neo4j Graph Platform\
**Tags:** apoc, performance, cypher\
**Created:** [April 2, 2025, 12:52pm UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338 "2025-04-02T12:52:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamed.metalgear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hamed.metalgear/32/27543_2.png) [@hamed.metalgear](https://community.neo4j.com/u/hamed.metalgear)\
**Post date:** [April 2, 2025, 12:52pm UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338/1 "2025-04-02T12:52:09Z")

</div>

I am trying to get the neighbors of a given root node at the `n` hop distance.

I can use the following query on a small graph, or when the root node has few neighbors.

```auto
MATCH (root: Person { Name: "somename" })
CALL apoc.path.spanningTree(root, {
    maxLevel: 3, 
    bfs: true, 
    labelFilter: 'Person'
}
YIELD path
WITH 
    root, 
    nodes(path) AS pathNodes, 
    relationships(path) AS pathRels
RETURN 
    [root] AS root, 
    [n IN pathNodes WHERE n <> root] AS nodes, 
    pathRels AS relationships

```

However, when the root node has many neighbors at `n` hop distance (hundreds of millions), I run into out-of-memory related issues.

I need all the neighbors, hence, I do not limit the output size.

I was wondering if there is any way of streaming the neighbors of a given root node at `n` hop distance to an output file, such that they are serialized to a file as they are found instead of being collected in the memory.

---

<div class="post-metadata">

**Author:** ![joshcornejo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joshcornejo/32/34171_2.png) [@joshcornejo](https://community.neo4j.com/u/joshcornejo)\
**Post date:** [April 2, 2025, 1:19pm UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338/2 "2025-04-02T13:19:03Z")

</div>

Is your server configured to handle millions of neighbours? How much RAM do you have?

---

<div class="post-metadata">

**Author:** ![joshcornejo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joshcornejo/32/34171_2.png) [@joshcornejo](https://community.neo4j.com/u/joshcornejo)\
**Post date:** [April 2, 2025, 1:28pm UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338/3 "2025-04-02T13:28:42Z")

</div>

You can start by trying running a cli, rather than the browser:

`query.cypher`:

```auto
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
RETURN p.name, m.title LIMIT 10;

```

then you can run a command line:

`$ cypher-shell -u neo4j -p "password" < query.cypher > output.file`

---

<div class="post-metadata">

**Author:** ![hamed.metalgear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hamed.metalgear/32/27543_2.png) [@hamed.metalgear](https://community.neo4j.com/u/hamed.metalgear)\
**Post date:** [April 2, 2025, 9:21pm UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338/4 "2025-04-02T21:21:00Z")

</div>

I'm running from CLI. That won't answer my question. I need neighbors at `n` hop distance. As I said, I'm looking for options to stream to a file.

---

<div class="post-metadata">

**Author:** ![joshcornejo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joshcornejo/32/34171_2.png) [@joshcornejo](https://community.neo4j.com/u/joshcornejo)\
**Post date:** [April 3, 2025, 6:46am UTC](https://community.neo4j.com/t/stream-spanning-tree-to-an-output-file/73338/5 "2025-04-03T06:46:35Z")

</div>

`output.file` is a stream to a file ?
