-
-
Save retrography/fdbf658116ddb7fa7c51 to your computer and use it in GitHub Desktop.
Fetches graph data directly from Neo4j REST API into R. Useful for R igraph users.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requirements | |
#sudo apt-get install libcurl4-gnutls-dev # for RCurl on linux | |
#install.packages('RCurl') | |
#install.packages('RJSONIO') | |
library('RCurl') | |
library('RJSONIO') | |
query <- function(querystring) { | |
h = basicTextGatherer() | |
curlPerform(url="http://localhost:7474/db/data/cypher", | |
postfields=paste('query',curlEscape(querystring), sep='='), | |
writefunction = h$update, | |
verbose = FALSE | |
) | |
result <- fromJSON(h$value()) | |
data <- data.frame(t(sapply(result$data, unlist))) | |
names(data) <- result$columns | |
data | |
} | |
# EXAMPLE | |
# ======= | |
# Cypher Query: | |
q <- "match (o:Organization)-[r]-(p:Person) return o.name,o.location,p.account,p.name,p.email limit 20" | |
data <-query(q) | |
head(data,20) | |
# Output: | |
# o.name o.location p.account p.name p.email | |
# 1 PerfectLine Estonia kritik | |
# 2 Sappho OSS London UK andrewheald | |
# 3 The 88 NYC aface1 | |
# 4 The 88 NYC xbilldozer | |
# 5 The 88 NYC chadyj | |
# 6 The 88 NYC benmanns Benjamin Manns [email protected] | |
# 7 simplabs Munich, Germany marcoow | |
# 8 Everyday Hero Brisbane, Australia soloman1124 | |
# 9 Everyday Hero Brisbane, Australia orodio | |
# 10 Everyday Hero Brisbane, Australia justinhennessy | |
# 11 Everyday Hero Brisbane, Australia coop Tim Cooper [email protected] | |
# 12 Everyday Hero Brisbane, Australia evilmarty Marty Zalega [email protected] | |
# 13 Sorenson Media Salt Lake City, UT & San Diego, CA bcarlson | |
# 14 Sorenson Media Salt Lake City, UT & San Diego, CA elmomalmo | |
# 15 Sorenson Media Salt Lake City, UT & San Diego, CA enthooz | |
# 16 3scale Barcelona, Spain and Sunnyvale, USA solso | |
# 17 3scale Barcelona, Spain and Sunnyvale, USA MarkCheshire | |
# 18 3scale Barcelona, Spain and Sunnyvale, USA rhoml | |
# 19 3scale Barcelona, Spain and Sunnyvale, USA mikz Michal Cichra | |
# 20 3scale Barcelona, Spain and Sunnyvale, USA njyx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
_Caution:_ This script runs into trouble if the query response is in a non-tabular format or includes missing values. Neo4j does not support null-valued properties and any property with a null value is removed from the database. In other words running
SET node.property = NULL
elicits the same result as runningREMOVE node.property
, which means it deletes the property. Neo4j's REST API responds to queries in JSON, which is schema-less as well, and does not represent missing properties in any way. When converting the JSON output to a tabular format, this results in rows with variable number of columns. In case your database contains missing values, either replace them with symbolic values or convert them on the fly within your query. If none of these is an option, you need to use a tabular format like CSV for data interchange.