我以Array [AnyRef]的形式从Neo4j获得了一些查询结果。每个数组元素如下所示:
Record<{group_name: "Amig@s & Friends", event_name: "Dia de Los muertos Day of the dead", venue_name: "Dvorak Park (Pilsen)", distance: 2615.3793888968457}>
问题是如何将group_name,event_name,venue_name和distance作为字段提取出来,以便将它们写入csv文件或在Spark中创建数据框?是否有一些提取记录值的方法?
这是我查询逻辑的代码:
def execNeo4jSearchQuery(neo4jSession: Session, data: Array[String]) = {
val query1 = "MATCH (m:Member)-[mtg_r:MT_TO_MEMBER]->(mt:MemberTopics)-[mtt_r:MT_TO_TOPIC]->(t:Topic), (t1:Topic)-[tt_r:GT_TO_TOPIC]->(gt:GroupTopics)-[tg_r:GT_TO_GROUP]->(g:Group)-[h_r:HAS]->(e:Event)-[a_r:AT]->(v:Venue) WHERE mt.topic_id = gt.topic_id AND distance(point({ longitude: " + data(2) + ", latitude: " + data(1) + "}),point({ longitude: v.lon, latitude: v.lat })) < 4000 AND mt.member_id = " + data(0) + " RETURN distinct g.group_name " +
"as group_name, e.event_name as event_name, v.venue_name as venue_name, distance(point({ longitude: " + data(2) + ", latitude: " + data(1) + "}),point({ longitude: v.lon, latitude: v.lat })) as distance"
val results = neo4jSession.run(query1).list()
val resultsArray = results
.toArray()
.map(_.toString)
.foreach(println)
}
答案 0 :(得分:2)
Session.run
返回StatementResult
,允许迭代Record
条目,因此可以根据需要进行转换。所以这里有几个例子。
要写入CSV文件:
// the fields to retrieve
val fieldsToRetrieve = Seq("group_name", "event_name", "venue_name")
// shortcut to produce a line in the CSV
def recordToCsv(rec: Record): String =
fieldsToRetrieve.map(rec.get(_).asString()).mkString(",")
// execute the query and write into CSV
val queryResult = neo4jSession.run("[query]")
val writer = new PrintWriter("myFile.csv")
while(queryResult.hasNext) {
val csvLine = recordToCsv(queryResult.next)
writer.println(csvLine)
}
// don't forget to close the writer
创建Spark DataFrame:
private val session: SparkSession = ...
import session.implicits._
import scala.collection.JavaConverters._ // necessary to convert Neo4j's list of Records
// the fields to retrieve
val fieldsToRetrieve = Seq("group_name", "event_name", "venue_name")
// shortcut to produce a sequence of values representing a row in a dataframe
def toRow(record: Record): Seq[String] =
fieldsToRetrieve.map(record.get(_).asString())
// execute the query and push the results into a Spark dataframe
val df = neo4jSession.run("[query]").list().asScala
.map(toRow).toDF()