Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a cypher script file from Terminal with the cypher-shell neo4j command?

I have a cypher script file and I would like to run it directly.

All answers I could find on SO to the best of my knowledge use the command neo4j-shell which in my version (Neo4j server 3.5.5) seems to be deprecated and substituted with the command cyphershell.

Using the command sudo ./neo4j-community-3.5.5/bin/cypher-shell --help I got the following instructions.

usage: cypher-shell [-h] [-a ADDRESS] [-u USERNAME] [-p PASSWORD] [--encryption {true,false}] [--format {auto,verbose,plain}] [--debug] [--non-interactive] [--sample-rows SAMPLE-ROWS] [--wrap {true,false}] [-v] [--driver-version] [--fail-fast | --fail-at-end] [cypher]

A command line shell where you can execute Cypher against an instance of Neo4j. By default the shell is interactive but you can use it for scripting by passing cypher directly on the command line or by piping a file with cypher statements (requires Powershell on Windows).

My file is the following which tries to create a graph from csv files and it comes from the book "Graph Algorithms".

WITH "https://github.com/neo4j-graph-analytics/book/raw/master/data" AS base 
WITH base + "transport-nodes.csv" AS uri
LOAD CSV WITH HEADERS FROM uri AS row
MERGE (place:Place {id:row.id})
SET place.latitude = toFloat(row.latitude),
  place.longitude = toFloat(row.latitude),
  place.population = toInteger(row.population)

WITH "https://github.com/neo4j-graph-analytics/book/raw/master/data/" AS base 
WITH base + "transport-relationships.csv" AS uri
LOAD CSV WITH HEADERS FROM uri AS row
MATCH (origin:Place {id: row.src})
MATCH (destination:Place {id: row.dst})
MERGE (origin)-[:EROAD {distance: toInteger(row.cost)}]->(destination)

When I try to pass the file directly with the command:

sudo ./neo4j-community-3.5.5/bin/cypher-shell neo_4.cypher

first it asks for username and password but after typing the correct password (the wrong password results in the error The client is unauthorized due to authentication failure.) I get the error:

Invalid input 'n': expected <init> (line 1, column 1 (offset: 0))
"neo_4.cypher"
 ^

When I try piping with the command:

 sudo cat neo_4.cypher| sudo ./neo4j-community-3.5.5/bin/cypher-shell -u usr -p 'pwd'

no output is generated and no graph either.

How to run a cypher script file with the neo4j command cypher-shell?

like image 800
Francesco Boi Avatar asked May 08 '19 10:05

Francesco Boi


People also ask

How do you run a Cypher?

You can run Cypher statements in the following ways: Typing Cypher statements directly into the interactive shell. Running Cypher statements from a file with the interactive shell. Running Cypher statements from a file as a cypher-shell argument.

How can you run CQL commands in Neo4j?

Neo4j CQL has commands to perform Database operations. Neo4j CQL supports many clauses such as WHERE, ORDER BY, etc., to write very complex queries in an easy manner. Neo4j CQL supports some functions such as String, Aggregation. In addition to them, it also supports some Relationship Functions.

Can you run Cypher statements on the system database in Neo4j?

You typically use cypher-shell to execute Cypher against a user database, but you can also use it to perform some management tasks against the Neo4j instance by accessing the system database.

How do I run Cypher against an instance of Neo4j?

A command line shell where you can execute Cypher against an instance of Neo4j. By default the shell is interactive but you can use it for scripting by passing cypher directly on the command line or by piping a file with cypher statements (requires Powershell on Windows).

How do I use Cypher in shell script?

By default the shell is interactive but you can use it for scripting by passing cypher directly on the command line or by piping a file with cypher statements (requires Powershell on Windows). My file is the following which tries to create a graph from csv files and it comes from the book "Graph Algorithms".

What is C Cypher shell CLI?

Cypher Shell is a command-line tool that comes with the Neo4j installation. It can also be downloaded from Neo4j Download Center and installed separately. Cypher Shell CLI is used to run queries and perform administrative tasks against a Neo4j instance.

What are the cypher-shell procedures and functions?

These procedures can be used to run files that are usually run by cypher-shell. e.g. files generated by Export to Cypher Script . They automatically skip :begin/:commit/:rollback operations as they are executed in a single transaction per file. The available procedures and functions are described in the following table:


3 Answers

Use cypher-shell -f yourscriptname. Check with --help for more description.

like image 84
TFuto Avatar answered Nov 15 '22 11:11

TFuto


I think the key is here:

cypher-shell -- help

... Stuff deleted

positional arguments:
  cypher                 an optional string of cypher to execute and then exit

This means that the paremeter is actual cypher code, not a file name. Thus, this works:

GMc@linux-ihon:~> cypher-shell "match(n) return n;"
username: neo4j
password: ****
+-----------------------------+
| n                           |
+-----------------------------+
| (:Job {jobName: "Job01"})   |
| (:Job {jobName: "Job02"})   |

But this doesn't (because the text "neo_4.cypher" isn't a valid cypher query)

cypher-shell neo_4.cypher

The help also says:

example of piping a file:
  cat some-cypher.txt | cypher-shell

So:

cat neo_4.cypher | cypher-shell

should work. Possibly your problem is all of the sudo's. Specifically the cat ... | sudo cypher-shell. It is possible that sudo is protecting cypher-shell from some arbitrary input (although it doesn't seem to do so on my system).

If you really need to use sudo to run cypher, try using the following:

sudo cypher-shell arguments_as_needed < neo_4.cypher

Oh, also, your script doesn't have a return, so it probably won't display any data, but you should still see the summary reports of records loaded.

Perhaps try something simpler first such as a simple match ... return ... query in your script.

Oh, and don't forget to terminate the cypher query with a semi-colon!

like image 44
GMc Avatar answered Nov 15 '22 12:11

GMc


The problem is in the cypher file: each line should end with a semicolon: ;. I still need sudo to run the program.

The file taken from the book seems to contain other errors as well actually.

like image 32
Francesco Boi Avatar answered Nov 15 '22 10:11

Francesco Boi