Chapter 5 Sending data to the API

5.1 Reading and sending a cypher file :

  • read_cypher reads a cypher file and returns a tibble of all the calls
read_cypher("data-raw/create.cypher")
## # A tibble: 5 x 1
##   cypher                                                                   
##   <chr>                                                                    
## 1 CREATE CONSTRAINT ON (b:Band) ASSERT b.name IS UNIQUE;                   
## 2 CREATE CONSTRAINT ON (c:City) ASSERT c.name IS UNIQUE;                   
## 3 CREATE CONSTRAINT ON (r:record) ASSERT r.name IS UNIQUE;                 
## 4 CREATE (ancient:Band {name: 'Ancient', formed: 1992}), (acturus:Band {na…
## 5 ""
  • send_cypher reads a cypher file, and send it the the API. By default, the stats are returned.
send_cypher("data-raw/constraints.cypher", con)

5.2 Sending csv to Neo4J

The load_csv sends an csv from an url to the Neo4J browser.

The args are :

  • on_load : the code to execute on load
  • con : the connexion object
  • url : the url of the csv to send
  • header : wether or not the csv has a header
  • periodic_commit : the volume for PERIODIC COMMIT
  • as : the AS argument for LOAD CSV
  • format : the format of the result
  • include_stats : whether or not to include the stats
  • meta : whether or not to return the meta information
on_load_query <- 'CREATE (n:Product)
  SET n = row,
  n.unitPrice = toFloat(row.unitPrice),
  n.unitsInStock = toInteger(row.unitsInStock), n.unitsOnOrder = toInteger(row.unitsOnOrder),
  n.reorderLevel = toInteger(row.reorderLevel), n.discontinued = (row.discontinued <> "0");'
# Send the csv 
load_csv(url = "http://data.neo4j.com/northwind/products.csv", 
         con = con, header = TRUE, periodic_commit = 50, 
         as = "row", on_load = on_load_query)
## No data returned.
## # A tibble: 12 x 2
##    type                  value
##    <chr>                 <dbl>
##  1 contains_updates          1
##  2 nodes_created            77
##  3 nodes_deleted             0
##  4 properties_set         1155
##  5 relationships_created     0
##  6 relationship_deleted      0
##  7 labels_added             77
##  8 labels_removed            0
##  9 indexes_added             0
## 10 indexes_removed           0
## 11 constraints_added         0
## 12 constraints_removed       0

5.3 Transform elements to cypher queries

  • vec_to_cypher() creates a list :
vec_to_cypher(iris[1, 1:3], "Species")
## [1] "(:`Species` {`Sepal.Length`: '5.1', `Sepal.Width`: '3.5', `Petal.Length`: '1.4'})"
  • and vec_to_cypher_with_var() creates a cypher call starting with a variable :
vec_to_cypher_with_var(iris[1, 1:3], "Species", a)
## [1] "(a:`Species` {`Sepal.Length`: '5.1', `Sepal.Width`: '3.5', `Petal.Length`: '1.4'})"

This can be combined inside a cypher call:

paste("MERGE", vec_to_cypher(iris[1, 1:3], "Species"))
## [1] "MERGE (:`Species` {`Sepal.Length`: '5.1', `Sepal.Width`: '3.5', `Petal.Length`: '1.4'})"