Last active
February 19, 2021 15:52
-
-
Save fbiville/bf617f29a18b1ecc688bf312915d83f1 to your computer and use it in GitHub Desktop.
Get Neo4j with Movies dataset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
function main() { | |
local neo4j_version="${1}" | |
local data_url="${2}" | |
local local_movies_path | |
local_movies_path=$(download_movies_data "${data_url}") | |
local mounted_data_folder="/movies" | |
local password="abcde" | |
local container_id | |
container_id=$(start_container "${neo4j_version}" "$(dirname "${local_movies_path}")" "${mounted_data_folder}" "${password}") | |
local mounted_file | |
mounted_file="${mounted_data_folder}/$(basename "$local_movies_path")" | |
load_data "${container_id}" "${mounted_file}" "${password}" "${neo4j_version}" | |
echo "${container_id}" | |
} | |
function download_movies_data() { | |
local data_url="${1}" | |
local movies_data_file | |
movies_data_file=$(mktemp) | |
curl --fail --silent --output "${movies_data_file}" "${data_url}" | |
echo "${movies_data_file}" | |
} | |
function start_container() { | |
local container_id | |
neo4j_version="${1}" | |
local_data_folder="${2}" | |
mounted_data_folder="${3}" | |
password="${4}" | |
container_id=$(docker run --detach --rm \ | |
--env NEO4J_AUTH=neo4j/"${password}" \ | |
--publish=7687:7687 \ | |
--volume "${local_data_folder}":"${mounted_data_folder}" \ | |
--health-cmd "cypher-shell -u neo4j -p ${password} 'RETURN 1'" \ | |
--health-interval 5s \ | |
--health-timeout 5s \ | |
--health-retries 5 \ | |
neo4j:"${neo4j_version}") | |
until docker inspect --format "{{json .State.Health.Status }}" "${container_id}" | grep --quiet --max-count 1 "healthy"; do | |
sleep 5; | |
done | |
echo "${container_id}" | |
} | |
function load_data() { | |
local container_id="${1}" | |
local cypher_file="${2}" | |
local password="${3}" | |
local neo4j_version="${4}" | |
if [[ "${neo4j_version}" == 3* ]]; then | |
docker exec --interactive --tty "${container_id}" sh -c "cat '${cypher_file}' | cypher-shell -u neo4j -p '${password}'" | |
else | |
docker exec --interactive --tty "${container_id}" cypher-shell -u neo4j -p "${password}" --file "${cypher_file}" > /dev/null | |
fi | |
} | |
main \ | |
"${NEO4J_VERSION:-latest}" \ | |
"https://raw.githubusercontent.com/neo4j-graph-examples/movies/8508a527d8aa1c261b0978d1d5b3156d4ac8328e/scripts/import.cypher" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment