Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用SparkML预测模型时的任务序列化问题_Scala_Apache Spark_Spark Streaming_Apache Spark Mllib - Fatal编程技术网

Scala 使用SparkML预测模型时的任务序列化问题

Scala 使用SparkML预测模型时的任务序列化问题,scala,apache-spark,spark-streaming,apache-spark-mllib,Scala,Apache Spark,Spark Streaming,Apache Spark Mllib,运行此代码时,我收到任务序列化错误,其中myDstream是DStream[String],而session是String: val model = GradientBoostedTreesModel.load(sc,mySet.value("modelAddress") + mySet.value("modelId")) val newDstream = myDstream.map(session => { val features : Array

运行此代码时,我收到任务序列化错误,其中
myDstream
DStream[String]
,而
session
String

      val model = GradientBoostedTreesModel.load(sc,mySet.value("modelAddress") + mySet.value("modelId"))
      val newDstream = myDstream.map(session => {
        val features : Array[String] = UtilsPredictor.getFeatures()
        val parsedSession = UtilsPredictor.parseJSON(session)
        var input: String = ""
        var count: Integer = 1
        for (i <- 0 until features.length) {
          if (count < features.length) {
            input += parsedSession(features(i)) + ","
            count += 1
          }
          else {
            input += parsedSession(features(i))
          }
        }
        input = "[" + input + "]"
        val vecTest = Vectors.parse(input)
        parsedSession + ("prediction_result" -> model.predict(vecTest).toString)
      })


      newDstream.foreachRDD(session => {
        session.foreachPartition({ partitionOfRecords =>
            //...
        })
      })
  • 确保您的类是可序列化的
  • @transient
    添加到可能导致任务序列化错误的代码块中。此批注将跳过特定实体的序列化计算/考虑
通常,这就是我们在应用程序中编写日志时所做的,如下所示

 @transient private lazy val log = LoggerFactory.getLogger(getClass) 
  • 确保您的类是可序列化的
  • @transient
    添加到可能导致任务序列化错误的代码块中。此批注将跳过特定实体的序列化计算/考虑
通常,这就是我们在应用程序中编写日志时所做的,如下所示

 @transient private lazy val log = LoggerFactory.getLogger(getClass) 

你是说这个吗@瞬态私有lazy val newDstream=myDstream.map(会话=>{`您的类扩展是否可序列化?请尝试使用他的@transient val parsedSession=UtilsPredictor.parseJSON(会话)是的,
UtilsPredictor
扩展了可序列化。问题不是由
UtilsPredictor
引起的。问题与
model.predict
有关。尽管
GradientBoostedTreesModel
是可序列化的,
vecTest
(它是
字符串
)不是。所以,如果我遵循你的逻辑,我应该做
@transient val vecTest=…
。对吗?让我测试一下。是的。试一试你的意思是什么?`@transient private lazy val newDstream=myDstream.map(session=>{`你的类可序列化吗?试试他的@transient val parsedSession=UtilsPredictor.parseJSON(session)是的,
UtilsPredictor
扩展了可序列化。问题不是由
UtilsPredictor
引起的。问题与
model.predict
有关。尽管
GradientBoostedTreesModel
是可序列化的,
vecTest
(它是
字符串
)不是。所以,如果我遵循你的逻辑,我应该做
@transient val vecTest=…
。对吗?让我测试一下。是的。试一试在我看来,问题可能出在你省略的代码中(
/…
)因为你说过部分代码触发了它,而被序列化的是
foreach
的主体。在我看来,问题可能在于你省略的代码(
/…
),因为你说过部分代码触发了它,而被序列化的是
foreach
的主体。