neo4j spark流scala的notserializableexception

8ehkhllq  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(388)

我正在尝试使用neo4j spark connector在neo4j中运行查询。我想将流中的值(由kafka作为字符串生成)传递到我的查询中。但是,我得到一个例外:

Caused by: java.io.NotSerializableException: org.apache.spark.SparkContext
Serialization stack:
    - object not serializable (class: org.apache.spark.SparkContext, value: org.apache.spark.SparkContext@54688d9f)
    - field (class: consumer.SparkConsumer$$anonfun$processingLogic$2, name: sc$1, type: class org.apache.spark.SparkContext)
    - object (class consumer.SparkConsumer$$anonfun$processingLogic$2, <function1>)
    - field (class: consumer.SparkConsumer$$anonfun$processingLogic$2$$anonfun$apply$3, name: $outer, type: class consumer.SparkConsumer$$anonfun$processingLogic$2)
    - object (class consumer.SparkConsumer$$anonfun$processingLogic$2$$anonfun$apply$3, <function1>)

以下是主要功能和查询逻辑的代码:

object SparkConsumer {
    def main(args: Array[String]) {
        val config = "neo4j_local"
        val sparkConf = new SparkConf().setMaster("local[*]").setAppName("KafkaSparkStreaming")
        setNeo4jSparkConfig(config, sparkConf)

        val sparkSession = SparkSession
          .builder()
          .config(sparkConf)
          .getOrCreate()

        val streamingContext = new StreamingContext(sparkSession.sparkContext, Seconds(3))
        streamingContext.sparkContext.setLogLevel("ERROR")
        val sqlContext = new SQLContext(streamingContext.sparkContext)
        val numStreams = 2
        val topics = Array("member_topic1")

        def kafkaParams(i: Int) = Map[String, Object](
          "bootstrap.servers" -> "localhost:9092",
          "key.deserializer" -> classOf[StringDeserializer],
          "value.deserializer" -> classOf[StringDeserializer],
          "group.id" -> "group2",
          "auto.offset.reset" -> "latest",
          "enable.auto.commit" -> (false: java.lang.Boolean)
        )

        val lines = (1 to numStreams).map(i => KafkaUtils.createDirectStream[String, String](
          streamingContext,
          LocationStrategies.PreferConsistent,
          ConsumerStrategies.Subscribe[String, String](topics, kafkaParams(i))
        ))

        val messages = streamingContext.union(lines)
        val wordsArrays = values.map(_.split(","))

        wordsArrays.foreachRDD(rdd => rdd.foreach(
          data => execNeo4jSearchQuery(data)(streamingContext.sparkContext)
        ))

        streamingContext.start()
        streamingContext.awaitTermination()
      }

    def execNeo4jSearchQuery(data: Array[String])(implicit sc: SparkContext) = {
        val neo = Neo4j(sc)
        val query = "my query"

        val paramsMap = Map("lat" -> data(1).toDouble, "lon" -> data(2).toDouble, "id" -> data(0).toInt)
        val df = neo.cypher(query, paramsMap).loadDataFrame("group_name" -> "string", "event_name" -> "string", "venue_name" -> "string", "distance" -> "double")
        println("\ndf:")
        df.show()
      }
}
rsl1atfo

rsl1atfo1#

不允许访问 SparkContext , SparkSession 或者从执行器创建分布式数据结构。因此:

wordsArrays.foreachRDD(rdd => rdd.foreach(
  data => execNeo4jSearchQuery(data)(streamingContext.sparkContext)
))

哪里 execNeo4jSearchQuery 电话:

neo.cypher(query, paramsMap).loadDataFrame

不是有效的Spark代码。
如果您想直接从 RDD.foreach 您必须使用标准客户机(anromcypher似乎提供了非常优雅的api),而不必转换为spark分布式结构。
有点不相关的注意事项-您可以考虑对具有 foreachPartition (还引发了在map/mappartitions上下文中初始化数据库连接的开销)。

相关问题