11#! /usr/bin/env bash
22
33# Utilizes Neo4j's HTTP API to execute a Cypher query from an input file and provides the results in CSV format.
4- # Use it when "cypher-shell" is not present or not flexible enough.
4+ # Use it when "cypher-shell" is not present, not flexible enough or to avoid an additional dependency .
55
66# It requires "cURL" ( https://curl.se ) and "jq" ( https://stedolan.github.io/jq ) to be installed.
77# The environment variable NEO4J_INITIAL_PASSWORD needs to be set.
88
99# Using "cypher-shell" that comes with Neo4j server is much simpler to use:
10- # cat $cypher_query_file_name | $NEO4J_HOME/bin/cypher-shell -u neo4j -p password --format plain
10+ # cat $cypher_query_file_name | $NEO4J_HOME/bin/cypher-shell -u neo4j -p password --format plain --param "number ⇒ 3"
1111
1212# Note: These command line arguments are supported:
1313# -> "filename" of the cypher query file (required, unnamed first argument)
1414# -> "--no_source_reference" to not append the cypher query file name as last CSV column
15+ # -> any following key=value arguments are used as query parameters
1516
1617# Overrideable Defaults
1718NEO4J_HTTP_PORT=${NEO4J_HTTP_PORT:- " 7474" } # Neo4j HTTP API port for executing queries
2627# Input Arguments: Initialize arguments and set default values for optional ones
2728cypher_query_file_name=" "
2829no_source_reference=false
30+ query_parameters=" "
2931
3032# Input Arguments: Function to print usage information
3133print_usage () {
@@ -36,17 +38,17 @@ print_usage() {
3638
3739# Input Arguments: Parse the command-line arguments
3840while [[ $# -gt 0 ]]; do
39- key =" $1 "
41+ arg =" $1 "
4042
41- case $key in
43+ case $arg in
4244 --no-source-reference-column)
4345 no_source_reference=true
4446 shift
4547 ;;
4648 * )
47- if [[ -z " $cypher_query_file_name " ]]; then
49+ if [[ -z " ${ cypher_query_file_name} " ]]; then
4850 # Input Arguments: Read the first unnamed input argument containing the name of the cypher file
49- cypher_query_file_name=" $key "
51+ cypher_query_file_name=" ${arg} "
5052 # echo "Cypher File: $cypher_query_file_name"
5153
5254 # Input Arguments: Check the first input argument to be a valid file
@@ -56,18 +58,26 @@ while [[ $# -gt 0 ]]; do
5658 exit 1
5759 fi
5860 else
59- echo " Error: Unknown option: $key " >&2
60- print_usage
61- exit 1
61+ # Convert key=value argument to JSON "key": "value"
62+ json_parameter=$( echo " ${arg} " | awk -F' =' ' { print "\""$1"\": \""$2"\""}' | grep -iv ' \"#' )
63+ if [[ -z " ${query_parameters} " ]]; then
64+ # Add first query parameter directly
65+ query_parameters=" ${json_parameter} "
66+ else
67+ # Append next query parameter separated by a comma and a space
68+ query_parameters=" ${query_parameters} , ${json_parameter} "
69+ fi
6270 fi
6371 shift
6472 ;;
6573 esac
6674done
6775
76+ # echo "executeQuery: query_parameters: ${query_parameters}"
77+
6878# Read the file that contains the Cypher query
6979original_cypher_query=$( < " ${cypher_query_file_name} " )
70- # echo "Original Query: $original_cypher_query"
80+ # echo "executeQuery: Original Query: $original_cypher_query"
7181
7282# Encode the string containing the Cypher query to be used inside JSON using jq ( https://stedolan.github.io/jq )
7383# Be sure to put double quotes around the original Cypher query to prevent newlines from beeing removed.
@@ -77,11 +87,11 @@ original_cypher_query=$(<"${cypher_query_file_name}")
7787# . means "output the root of the JSON document"
7888# Source: https://stackoverflow.com/questions/10053678/escaping-characters-in-bash-for-json
7989cypher_query=$( echo -n " ${original_cypher_query} " | jq -Rsa .)
80- # echo "Cypher Query: $cypher_query"
90+ # echo "executeQuery: Cypher Query: $cypher_query"
8191
8292# Put the query inside the structure that is expected by the Neo4j HTTP API
83- cypher_query_for_api=" {\" statements\" :[{\" statement\" :${cypher_query} ,\" includeStats\" : false}]}"
84- # echo "Cypher Query for API: ${cypher_query_for_api}"
93+ cypher_query_for_api=" {\" statements\" :[{\" statement\" :${cypher_query} ,\" parameters \" :{ ${query_parameters} }, \" includeStats\" : false}]}"
94+ # echo "executeQuery: Cypher Query for API: ${cypher_query_for_api}"
8595
8696# Calls the Neo4j HTTP API using cURL ( https://curl.se )
8797cyper_query_result=$( curl --silent -S --fail-with-body -H Accept:application/json -H Content-Type:application/json \
0 commit comments