【问题标题】:IncompatibleSchemaException: Unexpected type VectorUDT when serializing in Avro formatIncompatibleSchemaException:以 Avro 格式序列化时出现意外类型 VectorUDT
【发布时间】:2017-03-16 15:32:06
【问题描述】:

我正在使用 Spark Mllib 为我的数据生成预测,然后以 Avro 格式将它们存储到 HDFS:

val dataPredictions = myModel.transform(myData)
val output = dataPredictions.select("is", "probability", "prediction")
output.write.format("com.databricks.spark.avro").save(path)

我收到以下异常:

com.databricks.spark.avro.SchemaConverters$IncompatibleSchemaException:
    Unexpected type org.apache.spark.ml.linalg.VectorUDT@3bfc3ba7.

我的理解是“预测”列格式不能序列化为 Avro。

  • 如何将 VectorUDT 转换为数组,以便在 Avro 中对其进行序列化?
  • 是否有更好的替代方案(我无法摆脱 Avro 格式)?

【问题讨论】:

    标签: scala apache-spark apache-spark-mllib avro spark-avro


    【解决方案1】:

    要将任何Vector 转换为Array[Double],您可以使用以下UDF:

    import org.apache.spark.sql.functions.udf
    import org.apache.spark.sql.functions.col
    import org.apache.spark.ml.linalg.Vector
    
    val vectorToArrayUdf = udf((vector: Vector) => vector.toArray)
    
    // The following will work
    val output = dataPredictions
        .withColumn("probabilities", vectorToArrayUdf(col("probability")))
        .select("id", "probabilities", "prediction")
    
    output.write.format("com.databricks.spark.avro").save(path)
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2020-05-21
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多