The Twelfth Lab

Program with Neo4j

Novy 20, 2015

(I) Download & Install Neo4j

  •         1) Download Neo4j Community Edition here
                    You can download Neo4j to your own laptop
                    Or download Neo4j to Linux machines in the undergrad lab
  •         2) Install Neo4j
                    Follow the instructions
  •         3) Start Neo4j
                    Windows: Run the installer; Double-click to start.
                    MacOS & Linux: Open a terminal, Go to the bin folder in Neo4j folder, Start with ./neo4j start
  •         4) Neo4j Manual.
                    Read Installation and Deployment , if you have any question of installing Neo4j.

(II) Configure Neo4j

  •         1) Neo4j Shell
                    After you start the Neo4j Service
                    Open Neo4j Shell with ./neo4j-shell in bin folder
  •         2) Web Interface
                    Open Web Interface with http://localhost:7474
  •         3) Database Location
                    The defaul database is in [neo4j_folder]/data/graph.db directory
  •         4) Configuration File
                    The configuration file is [neo4j_folder]/conf/
                    You can change the database location by modifying org.neo4j.server.database.location=[database directory]
                    You can also change the ports for the web interface
                    You had better stop Neo4j before you change the configuration file
                    The manual of server configuration can be found here

(III) Import Data

  •         1) Stop Neo4j Service
  •         2) Create a directory
                    Create a directory for importing data
                    For example: mkdir [neo4j_folder]/data/hubway.db
  •         3) Download files
                    Stations data: hubway_stations_out.csv
                    Trips data: hubway_trips_sample_out.csv
  •         4) Import data
                    a) Go to the bin folder in neo4j folder,
                    b) Use the following command to import the data:
                            ./neo4j-import --into [Database_Directory] --nodes [Path]/hubway_stations_out.csv --relationships [Path]/hubway_trips_sample_out.csv
                    c) Change [Database_Directory] to your database directory.
                    d) Change [Path] to your real path where the data files can be located.
                    e) Use pwd to check the directory
                    f) You should be able to see: Imported 142 nodes, 100000 relationships
  •         5) Change the configuration file
                    a) Open the configuration file conf/
                    b) Change the database location org.neo4j.server.database.location=[Database_Directory]
                    c) For example: org.neo4j.server.database.location=data/hubway.db
                            (If you use the directory created in 2) to store the database)
  •         6) Re-Start Neo4j Service
  •         7) You can find more about importing data here

(IV) Cypher Query Language

  •         1) Start Neo4j Shell
  •         2) Check Number of Nodes
                    MATCH (n) RETURN COUNT(*);
  •         3) Check Number of Edges
                    MATCH (m)-[r]->(n) RETURN COUNT(*);
  •         4) Implement Queries in Lab5
                    Find all stations with names including string 'Boston'
                              SQL: SELECT * FROM stations WHERE station LIKE '%Boston%';
                              NoSQL: MATCH (n) WHERE CONTAINS 'Boston' RETURN n;
  •         5) Cypher Query Language Syntax
                    Read manual here