Database Reference
In-Depth Information
Listing 5-6. PHP Script to Create Followers for Users
$fp = fopen('followers.csv', 'w');
fputcsv($fp, array( 'userId' , 'followerid' ));
//total number of userIds in the user list
$totalusers = 5492;
for ($i = 1; $i <= $totalusers; $i++) {
$rn = rand(1, $totalusers);
$c = 1;
for ($j = $rn; $j <= $totalusers; $j++) {
$c++;
if($j! = $i) {
fputcsv($fp, array($i, $j));
}
if($c == 10){
break;
}
}
}
fclose($fp);
echo 'Data saved to csvfile.csv';
Loading the Relationships
Next, you can use the LOAD CSV command, as shown in Listing 5-7, to match the userId via the unique index and to
create a relationship of FOLLOWS between the nodes.
Listing 5-7. Cypher Command for All Users to Follow All Other Users
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM " http://www.graphstory.com/practicalneo4j-book/code/chapter5/csv/
followers.csv " AS csvLine
MATCH (u1:User { userId: toInt(csvLine.userId)} ) ,(u2:User { userId: toInt(csvLine.followerid)} )
CREATE (u1)-[:FOLLOWS]->(u2)
By running the command shown in Listing 5-8, you can output a sample of the new relationships created with the
LOAD CSV command, which should look similar to the output shown in Figure 5-2 .
Listing 5-8. Cypher Command for All Users to Follow All Other Users
MATCH (a)-[:`FOLLOWS`]->(b) RETURN a,b LIMIT 25
 
Search WWH ::




Custom Search