neo4j连接spark(使用neo4j-connector-apache-spark_2.12)
一、依赖
spark版本:2.4.5
scala版本:2.12
这里使用的依赖是:
<dependency>
<groupId>neo4j-contrib</groupId>
<artifactId>neo4j-connector-apache-spark_2.12</artifactId>
<version>4.0.1_for_spark_2.4</version>
</dependency>
<!--其他存储库列表-->
<repository>
<id> SparkPackagesRepo</id>
<url>http://dl.bintray.com/spark-packages/maven</url>
</repository>
</repositories>
其他人说的这个依赖我没有在maven仓库找到
< dependency >
< groupId > neo4j-contrib </ groupId >
< artifactId > neo4j-spark-connector </ artifactId >
< version > 2.1.0-M4 </ version >
</ dependency >
二、spark代码
查询:
val df = spark.read.format("org.neo4j.spark.DataSource")
.option("url", "bolt://localhost:7687")
.option("authentication.basic.username", "neo4j")
.option("authentication.basic.password", "neo4j")
.option("labels", "节点")
.load()
df.show()
结果:
插入:
import spark.implicits._
val df = Seq(
("猪大哥"),
("猪小弟")
).toDF("name")
df.write.format("org.neo4j.spark.DataSource")
.mode(SaveMode.ErrorIfExists)
.option("url", "bolt://localhost:7687")
.option("authentication.basic.username", "neo4j")
.option("authentication.basic.password", "neo4j")
.option("labels", ":pig")
.save()
条件查询:
val df = spark.read.format("org.neo4j.spark.DataSource")
.option("url", "bolt://localhost:7687")
.option("authentication.basic.username", "neo4j")
.option("authentication.basic.password", "wcc")
.option("labels", "pig")
.load()
df.where("name='猪爸爸'").where("age = 25").show()
结果: