官术网_书友最值得收藏!

Using the Neo4j shell

The Neo4j shell is a powerful interactive shell for interacting with the Neo4j database. It is used for performing the CRUD operations on graphs.

The Neo4j shell can be executed locally (on the same machine on which we have installed the Neo4j server) or remotely (by connecting the Neo4j shell to a remote sever).

By default, the Neo4j shell (<$NEO4J_HOME>/bin/neo4j-shell) can be executed on the same machine on which the Neo4j server is installed, but the following configuration changes are required in <$NEO4J_HOME>/conf/neo4j.properties to enable the connectivity of the Neo4j database from the remote machines:

  • remote_shell_enabled=true: This configuration enables the property
  • remote_shell_host=127.0.0.1: This configuration enables and provides the IP address of the machine on which the Neo4j server is installed
  • remote_shell_port=1337: This configuration enables and defines the port for incoming connections

Let's talk about various other options provided by the Neo4j shell for connecting to the local Neo4j server:

  • neo4j-shell -path <PATH>: This option shows the path of the database directory on the local file system. A new database will be created in case the given path does not contain a valid Neo4j database.
  • neo4j-shell -pid <PID>: This option connects to a specific process ID.
  • neo4j-shell -readonly: This option connects to the local database in the READ ONLY mode.
  • neo4j-shell -c <COMMAND>: This option executes a single Cypher statement and then the shell exits.
  • neo4j-shell -file <FILE >: This option reads the contents of the file (multiple Cypher CRUD operations), and then executes it.
  • neo4j-shell –config - <CONFIG>: This option reads the given configuration file (such as neo4j-server.properties) from the specified location, and then starts the shell.

The following are the options for connecting to the remote Neo4j server:

  • neo4j-shell -port <PORT>: This option connects to the server running on a port different to the default port (1337)
  • neo4j-shell -host <HOST>: This option shows the IP address or domain name of the remote host on which the Neo4j server is installed and running.

Let's move forward and get our hands dirty with the system.

To begin with and to make it simple, first we will insert the data, and then try to fetch the same data through the Neo4j shell.

Let's perform the following steps for running our Cypher queries in the Neo4j shell:

  1. Open your UNIX shell/console and execute <$NEO4J_HOME>/bin/neo4j start. This will start your Neo4j server in another process.
  2. In the same console, execute <$NEO4J_HOME>/bin/neo4j-shell to start the Neo4j shell.
  3. Next, execute the following set of statements on the console:
    CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});
    MATCH (n) return n;
    MATCH (n) delete n;
  4. You will see something like the following image on your console:

Yes, that's it…we are done!

We will pe deep into the details of the Cypher statements in the upcoming chapters, but let's see the results of each of the preceding Cypher statements:

  • CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});: This statement creates a node with two attributes, Name:"Noah" and ReleaseYear:"2014", and a label, Movie
  • MATCH (n) return n;: This statement searches the Neo4j database and prints all the nodes and their associated properties on the console
  • MATCH (n) delete n;: This statement searches the Neo4j database and deletes all the selected nodes
主站蜘蛛池模板: 滨海县| 曲阳县| 苍梧县| 汶川县| 田林县| 洪江市| 高青县| 盘锦市| 阿坝县| 合川市| 垦利县| 东乡| 龙门县| 芜湖县| 伊金霍洛旗| 左云县| 郴州市| 霍邱县| 仙居县| 巴青县| 泉州市| 湖南省| 高平市| 株洲县| 沐川县| 诏安县| 兴仁县| 轮台县| 宝坻区| 双鸭山市| 小金县| 四会市| 化州市| 闸北区| 建阳市| 库伦旗| 泰来县| 东乌珠穆沁旗| 塔河县| 饶阳县| 永年县|