This document will show you how to export your query results from an AuraDB Instance to a file.
The examples in this document use the bash shell along with the Cypher Shell.
Method
The basic idea behind exporting your query results to a file is to use Cypher Shell to export a Stream along with a redirect to redirect the output to a file.
You can choose to store the query you need to run in a file or in a variable. It is recommended to store the query in a file.
The Cypher Shell will automatically use credentials stored in the NEO4J_USERNAME and NEO4J_PASSWORD environment variables. If you wish to specify the credentials as part of the command instead of using the default environment variables you can do so using the -u and -p flags for Username and Password respectively.
For more information on Cypher Shell, please see: https://neo4j.com/docs/operations-manual/current/tools/cypher-shell/
The query file or variable can then be passed to the Cypher Shell as below:
# You can pass in the query to the Cypher Shell like below, you can also do so with a file, using cat, type or echo followed by | similar to below. The dbid will need changing to the id of your AuraDB Instance
cat $CQL | cypher-shell --format plain -a neo4j+s://dbid.databases.neo4j.io > results.file
Exporting to GraphML
Below is an example file showing how to use the apoc.export.graphml.query procedure:
WITH "MATCH (a)-[:ACTED_IN]->(m)
RETURN (a)-[]->(m)" as query
CALL apoc.export.graphml.query(query, "", {stream:true})
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data;
After the WITH clause, between double quotes, write the query you need the results from. This query will be passed to the apoc.export.graphml.query procedure as the first argument.
The second argument is a blank string, this would usually be the file name to export to but in this case we must export to a stream so a blank string is necessary. Omitting the parameter entirely will cause an error to be thrown.
The final argument tells the procedure to export the information as a stream.
For further information on how this procedure works, please see: https://neo4j.com/labs/apoc/4.4/overview/apoc.export/apoc.export.graphml.query/
Given that we are using a file called cql.query to store the query and the file is in our working directory, the below shows how to run the query and redirect the result stream to a file:
#!/bin/bash
# Pass the created file to the cypher shell, using cat filename followed by | . Change dbid to the id of your AuraDB Instance
cat cql.query | cypher-shell --format plain -a neo4j+s://dbid.databases.neo4j.io > results.graphml
Comments
0 comments
Article is closed for comments.