Database Reference
In-Depth Information
Before running a LoaD CSV command in a live environment, it would be prudent to test the CSV file before
creating any new nodes or relationships. one way to test would be to execute a LoaD CSV command and simply return
the contents, as shown in Listing 5-3.
Note
Listing 5-3. Cypher Command to Test CSV File
LOAD CSV WITH HEADERS FROM " http://site.com/THEFILE.csv " AS csvLine
RETURN csvLine.header1, csvLine.header2, csvLine.headerX
The first CSV file we will import is going to create the User nodes. In this example, the users have a userId (which
we can reference later to create relationships), a username, and a first name. The Cypher statement also includes a
PERIODIC COMMIT statement to ensure that the query data does not stack up.
Creating a Unique Index
Before you run the LOAD CSV comment in Listing 5-3, you need to create an index for later lookups in the import
process. Because you know ahead of time that the userId value will be unique, you can add a unique constraint that
creates a unique index, which is faster than a standard index. In your web UI, you can run the code in Listing 5-4 to
create the unique index on the User node.
Listing 5-4. Cypher Command for All Users to Follow All Other Users
CREATE CONSTRAINT ON (u:User) ASSERT u.userId IS UNIQUE
Next, by running the Cypher statement in Listing 5-5, you add the Users to the graph. You can verify the results by
running MATCH (n:`User`) RETURN n LIMIT 25 .
Listing 5-5. Cypher Command for All Users to Follow All Other Users
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM " http://www.graphstory.com/practicalneo4j-book/code/chapter5/csv/list_of_
users.csv " AS csvLine
CREATE (u:User { userId: toInt(csvLine.userId), username: csvLine.username, firstname: csvLine.
firstname })
Creating Relationships
The second CSV file we import will create the relationships between the nodes. There are number of methods to
create the CSV for this purpose. In this case, I just created a simple PHP script to generate a CSV file, as shown in
Listing 5-6. This script creates a way for each user to randomly follow ten other users in the graph.
 
 
Search WWH ::




Custom Search