Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 1: replicate users.

What is the current approach to getting a Graphx graph from Neo4j?


(already asked on SO)

I understand that the previous (deprecated) Neo4j Spark Connector allowed for the generation of Spark Graphs and GraphFrames using the corresponding methods of the org.neo4j.spark.Neo4j class. With the Neo4j class gone, the only examples I found using the new approach of generating DataFrames is based on something like:"org.neo4j.spark.DataSource")
    .option("url", "bolt://localhost:7687")
    .option("query", "...")

How do I get Graphx Graph instances directly using the current Neo4j Connector for Apache Spark? Or would I need to combine separate DataFrames with edges and nodes?


Graph Voyager

Hi @mcsoini you can easily transform your Dataframe into an RDD by invoking df.rdd

i.e. given a graph like this (Person)-[:KNOWS]->(Person):

val persons: RDD[(VertexId, (String, String))] ="org.neo4j.spark.DataSource")
    .option("url", "bolt://localhost:7687")
    .option("labels", ":Person")
    .map(row => (row.getAs[Long]("<id>"), (row.getAs[String]("name"), row.getAs[Long]("surname"))))

val knows: RDD[Edge(VertexId, VertexId, String)] ="org.neo4j.spark.DataSource")
    .option("url", "bolt://localhost:7687")
    .option("", "false")
    .option("relationship", "KNOWS")
    .option("relationship.source.labels", "Person")
    .option("", "Person")
    .map(row => Edge(row.getAs[Long]("`<>`"), row.getAs[Long]("`<>`"), row.getAs[Long]("`<rel.type>`")))

// and then
val graph = Graph(users, knows)
Nodes 2022
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online