Running CQL file in Neo4j - neo4j

I have a CQL file called Novis.cql. Its somewhere random on my harddrive, but I want to run it in Neo4J to create my graph (it contains 500+ lines of code).
Where do I have to place it? And what command do I have to run nowadays to get it working? I've read and searched for answers, but some of the commands like Neo4jshell dont seem to work any longer...
Any help would be very appreciated!

The cypher-shell tool has been available for a while (starting with version 3.0, if not earlier), and you can use it to execute a Cypher query from a file that can be anywhere in your file system.
For example (on a linux/unix system), a command line like this will work (if you are in the neo4j home directory):
cat /my/full/path/my_code.cql | bin/cypher-shell -u neo4j -p secret
In neo4j 4.0 a new -f option was added to make it simpler:
bin/cypher-shell -u neo4j -p secret -f /my/full/path/my_code.cql

Related

Issue Importing the Paradise Papers Dataset into Neo4j

Hej all,
I am having an issue with importing the Paradise Papers dataset into a Neo4j (3.3.2) database.
It seems that the data is imported correctly into the database, as reported by neo4j-admin import.
...
IMPORT DONE in 1m 4s 889ms.
Imported:
867931 nodes
1657838 relationships
17838925 properties
Peak memory usage: 488.28 MB
...
However, after importing the data, the database seems to be empty, as reported by the Cypher queries MATCH (n) RETURN count(n); and CALL apoc.meta.graph();
...
count(n)
0
nodes, relationships
[], []
...
The following link points to a script, which should reproduce my issue. It is a Bash script for OS X/BSD (I think the -E switch for sed does not exist on Linux). Additionally, the script requires Docker to be installed and running on the system.
https://github.com/HelgeCPH/cypher_kernel/blob/master/example/import_data.sh
To run the script quickly:
wget https://raw.githubusercontent.com/HelgeCPH/cypher_kernel/master/example/import_data.sh
chmod u+x import_data.sh
./import_data.sh
I cannot see what I am doing wrong. Do I have to point to the database explicitely when running cypher-shell?
Checking on the container, the database files exist (ls -ltrh data/databases/graph.db) and their timestamps correspond to the time when importing the data.
Thanks in advance for your help!
You had multiple errors on your script :
Nodes were not loaded, because in the CSV the :ID column is not set. That's why I have added this part :
for file in import/csv_paradise_papers/.nodes..csv
do
sed -i -E '1s/node_id/node_id:ID/' $file
done
Labels of node were also not set. It's possible to set them directly in the command line like this : --nodes:MyLabel
If you do a query on Neo4j when the server is restarting, you will probably receive an error because the server is not yet ready. That's why I have added a sleep 5 at the end.
A better approach would be to wait until you have response from the server with someting like this :
until $(curl --output /dev/null --silent --head --fail http://localhost:7474); do
printf '.'
sleep 1
done
Last point, I don't know why, but if you do the restart of neo4j inside the container, you will not see the imported data. But if you restart the container itself it's OK ...

How to Load Cypher File into Neo4j

I have generated a cypher file and want to load it into neo4j.
The only relevant documentation I could find was about loading csv's.
I also tried the shell but it seems to have no effect
cypher-shell.bat -uneo4j -pne04j < db.cql
Copy paste into localhost:7474/browser makes the browser unresponsive.
In the current Neo4j version you can use Cypher Shell to achieve your goal.
From the docs, Invoke Cypher Shell with a Cypher script from the command line:
$ cat db.cql | bin/cypher-shell -u yourneo4juser -p yourpassword
Note that this example is based in a Linux instalation. If you are using Neo4j with Windows, you will need to adjust this command to your needs.
Not sure when this has been added but in the current version (4.4) an alternative way to load a cypher file to NEO4J (GUI) is by dragging and dropping the cypher file over the web browser, which then offers two options, either to add it to the favorites or to paste it in the editor.

Where to find dumped data (using dump command in Neo4j Shell) in Neo4j

Wondering where to find the dumped file of neo4j dump command. I was running Neo4j v2.1.3 in windows operating system. Please help thanks.
I believe the dump command just outputs to the console, so you need to redirect the output. Something like:
Neo4jShell.bat -c "dump match (n:People)-[:Shared]-(m) return n,m;" > social.connection.db\test2.cql
Edited with the Windows version of the command
For UNIX systems a similar command works:
neo4j-shell -c "dump match (n:People)-[:Shared]-(m) return n,m;" > social.connection.db/test2.cql

How to execute Cypher in a file?

I am working on windows. I have created a text file of Cypher query using notepad. How can I run the query in the file using Neo4jShell or Neo4j web interface console.
On Debian/Ubuntu or any *nix installations, use the following from terminal:
$ neo4j-shell -c < path-to-cypher-query-file.cql
Note that each cypher query in the file must end in a semicolon and must be separated by a blank line from the other query. Also, the .cql ending (file format) is not mandatory.
Just add -file as a parameter when starting the console.
On windows, it would look like this :
Neo4jShell.bat -file path/to/cql/file
Or you could also print the result into a new file
Neo4jShell.bat -file path/to/cql/file > path/to/output/file
I'm also sure there is a way to do it from within the shell and not at startup, as it was once demonstrated to me by Stefan Armbruster but for the love of god, I can't remember how he did it. But this approach works as well.
$ neo4j-shell -file query.cql
or using cypher-shell
$ cat query.cql | cypher-shell
The neo4jShell.bat file has been removed since this question was asked. The new approach to execute cypher files is to use the web application called LazyWebCypher .
With Neo4j web interface I just do copy&paste.
On the console I sometimes use curl to talk to Neo4j's REST interface. That allows me to use the same queries (with references to separate parameters) that I have in my application.
You have to wrap the query in your file into a json object for that.
data.json:
{
"query":"match (u:User) where u.username={username} return u",
"params":{"username":"trenkerbe"}
}
command:
curl -i -X POST -H "Content-Type: application/json" -d #data.json http://localhost:7474/db/data/cypher
./bin/neo4j-shell -path ../data/databases/ -c < commands.cql
on Neo4j 3.2.1

neo4j script file format - is there any?

I would like to predefine some graph data for neo4j and be able to load it, maybe via a console tool. I'd like it to be precisely the same as MySQL CLI and .sql files. Does anyone know if there exists a file format like .neo or .neo4j? I couldn't find such thing in the docs...
We usually do .cql or .cypher for script files. You can pipe it to the shell to run it, like so:
./neo4j-shell -c < MY_FILE.cypher
Michael Hunger was doing some great work on this feature, also, just recently. He got performance up and noise down from the console. I hope it gets into 1.9 release.
From https://groups.google.com/forum/#!topic/opencypher/PO5EnspBLs0
1:
"Sorry for the late reply, but we just wanted to inform you that the official recommendation is to use .cypher.
We'll be formalising this in the style guide soon."
2:
"In training run by Neo4j, we've historically used .cyp. I believe the preference is to use .cypher, and .cyp when an extension of 3 chars is required."
3:
"Note: '.cql' is already used for Cassandra - https://cassandra.apache.org/doc/cql/CQL.html"
From the above extracts:
1st preference is .cypher
2nd preference is .cyp (1st 3 characters of cypher)
Don't use .cql
More:
If you need a color coding in notepad++, download the xml given at https://gist.github.com/nicolewhite/b0344ea475852c8c9571 , import it via menu Language > User Defined Language > Import > Restart the Notepad++, open a file with .cypher that has some cyper query language)
Sample cypher is below:
MATCH (:Person {name: "Ann"}) -[:FB_FRIENDS]-> Create (:Person {name: "Dan"})
Hope that helps someone.
Using neo4j-client as the CLI for Neo4j allows for easy evaluation of scripts. There are several ways to work with a script containing multiple cypher commands:
You can pipe the script in via standard input, e.g.:
neo4j-client -u neo4j -P localhost < my_script.cyp
You can use the command line option --source or -i, e.g.:
neo4j-client -u neo4j -P -i my_script.cyp localhost
You can start an interactive shell, and then source the script:
$ neo4j-client localhost
Username: neo4j
Password: *****
neo4j-client 1.2.1.
Enter `:help` for usage hints.
Connected to 'neo4j://neo4j#localhost:7687'
neo4j>
neo4j> :source my_script.cyp
The extension .cyp is most commonly used for scripts.

Resources