Online Course Basic Neo4j 4.0 Administration Introduction to Neo4j Overview of Neo4j Administration Managing a Neo4j Instance Using cypher-shell to Manage Databases Copying Databases Changing the Database Location Checking Database Consistency Scripting to Manage Databases Configuring Plugins Managing HTTP Ports… Read more →

Scripting to Manage Databases

About this module

In most cases, you will want to automate the management of a Neo4j instance.

At the end of this module, you should be able to:

  • Create Cypher scripts to change a database.
  • Create shell scripts to execute Cypher scripts.
  • Create shell scripts to execute a set of shell scripts.

Scripting with cypher-shell

As a database administrator, you may need to automate changes to databases. The most common types of changes that administrators may want to perform are related to the application. As an administrator, you work with architects and developers to create scripts to:

  • Create or drop databases.
  • Create constraints.
  • Create indexes.
  • Load data.
  • Update data.
  • Restructure the graph (change graph data model).
  • Add users and access control.

Example: Adding constraints – 1

Suppose that we create 3 files to make changes:

1. AddConstraintsMovies.cypher that contains the Cypher statements to execute in cypher-shell:

CREATE CONSTRAINT ON (m:Movie) ASSERT m.title IS UNIQUE;
CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE;
CALL db.constraints();

Note that each Cypher statement must end with a ;.

Example: Adding constraints – 2

2. AddConstraints.sh that invokes cypher-shell using a set of Cypher statements and specifies verbose output:

cat /usr/local/work/AddConstraintsMovies.cypher |
       /usr/bin/cypher-shell -u neo4j -p <InstancePassword>
       --database movies --format verbose

Note that this statement must not have linefeeds in it.

Example: Adding constraints – 3

3. PrepareDB.sh that initializes the log file, PrepareDB.log, and calls the script to add the constraints:

rm -rf /usr/local/work/PrepareDB.log
/usr/local/work/AddConstraints.sh 2>&1 >> /usr/local/work/PrepareDB.log
# Other scripts here

When the PrepareDB.sh script runs its scripts, all output will be written to the log file, including error output. Then you can simply check the log file to make sure it ran as expected. Using the Cypher file, together with the run scripts, you can perform something like this to automate changes to a database or set of databases.

Exercise #8: Scripting changes to the database

In this Exercise, you will gain experience scripting with cypher-shell. You will create three files in the /home/ubuntu/files directory:

  • AddConstraintsMovies.cypher
  • AddConstraints.sh
  • MaintainDB.sh

Before you begin

  1. Make sure you have a terminal window open to your Docker Neo4j instance for this course.
  2. Ensure that the Docker Neo4j instance is started.

Exercise steps:

  1. log in to the Docker Neo4j instance, with bash:
    [sudo] docker exec -it neo4j /bin/bash
  2. Navigate to the /usr directory.
  3. Create a directory /usr/scripts.
  4. Change to the /usr/scripts directory.
  5. There is no built-in editor for this instance so you will create the scripts using the cat command. Create a Cypher script in the /usr/scripts directory named AddConstraintsMovies.cypher as follows:
    1. Copy the following text to your clipboard:
      CREATE CONSTRAINT TitleConstraint ON (m:Movie) ASSERT m.title IS UNIQUE;
      CREATE CONSTRAINT NameConstraint ON (p:Person) ASSERT p.name IS UNIQUE;
    2. Type cat > AddConstraintsMovies.cypher.
    3. Press ENTER.
    4. Paste what you have in the clipboard.
    5. Press ENTER.
    6. Enter CTL+d.
    7. Confirm that the file has the correct contents using the command: cat AddConstraintsMovies.cypher

CreatedCypherScriptDocker

  1. Create a shell script in the /usr/scripts directory named AddConstraints.sh that will forward AddConstraints.cypher to cypher-shell as follows:
    1. Copy the following text to your clipboard:
      cat /usr/scripts/AddConstraintsMovies.cypher | /var/lib/neo4j/bin/cypher-shell -u neo4j -p <Neo4j instance password>  --format verbose
    2. Change the text in the clipboard to use your password you set up for your Neo4j instance
    3. Type cat > AddConstraints.sh.
    4. Press ENTER.
    5. Paste what you have in the clipboard.
    6. Press ENTER.
    7. Enter CTL+d.
    8. Confirm that the file has the correct contents using the command: cat AddConstraints.sh

CreatedBashScriptDocker

  1. Create a shell script in the /usr/scripts directory named MaintainDB.sh that will initialize the log file and then call AddConstraints.sh as follows:
    1. Copy the following text to your clipboard:
      rm -rf /var/lib/neo4j/logs/reports/MaintainDB.log
      /usr/scripts/AddConstraints.sh 2>&1 >> /var/lib/neo4j/logs/reports/MaintainDB.log
    2. Type cat > MaintainDB.sh.
    3. Press ENTER.
    4. Paste what you have in the clipboard.
    5. Press ENTER.
    6. Enter CTL+d.
  2. Confirm that the file has the correct contents using the command: cat MaintainDB.sh

    CreatedBashScriptDocker2

  3. Ensure that the scripts you created have execute permissions.
  4. Run the MaintainDB.sh script and view the log file.

    RunMaintainDBDocker

  5. Confirm that it created the constraints in the default database (maindb). (Check using cypher-shell (CALL db.constraints();))

ConfirmConstraintsDocker

Exercise summary

You have now written some shell scripts to manage a database.

Check your understanding

Question 1

What tools do you typically use to automate changes to databases?

Select the correct answers.

  • Terminal shell that you are comfortable with.
  • neo4j
  • neo4j-admin
  • cypher-shell

Question 2

What tools do you typically use to automate management of databases?

Select the correct answers.

  • Terminal shell that you are comfortable with.
  • neo4j
  • neo4j-admin
  • cypher-shell

Question 3

What type of file would this content be placed in for scripting purposes?

CREATE CONSTRAINT ON (d:Department) ASSERT d.num IS UNIQUE;
CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE;
CALL db.constraints();

Select the correct answer.

  • .txt
  • .db
  • .cypher
  • .sh

Summary

You should now be able to:

  • Create Cypher scripts to change a database.
  • Create shell scripts to execute Cypher scripts.
  • Create shell scripts to execute a set of shell scripts.

Stay Connected

Sign up to find out more about Neo4j's upcoming events & meetups.