【问题标题】:Issue with VectorUDT when using Spark ML使用 Spark ML 时出现 VectorUDT 问题
【发布时间】:2016-08-16 17:53:29
【问题描述】:

我正在编写一个 UDAF,以应用于 Vector (spark.ml.linalg.Vector) 类型的 Spark 数据框列。我依赖 spark.ml.linalg 包,这样我就不必在数据帧和 RDD 之间来回走动。

在 UDAF 中,我必须为输入、缓冲区和输出模式指定数据类型:

def inputSchema = new StructType().add("features", new VectorUDT())
def bufferSchema: StructType =
    StructType(StructField("list_of_similarities", ArrayType(new VectorUDT(), true), true) :: Nil)

override def dataType: DataType = ArrayType(DoubleType,true) 

VectorUDT 是我将与 spark.mllib.linalg.Vector 一起使用的: https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala

但是,当我尝试从 spark.ml 导入它时:import org.apache.spark.ml.linalg.VectorUDT 我收到一个运行时错误(构建期间没有错误):

class VectorUDT in package linalg cannot be accessed in package org.apache.spark.ml.linalg 

这是预期的/您能建议一种解决方法吗?

我正在使用 Spark 2.0.0

【问题讨论】:

    标签: scala apache-spark spark-dataframe apache-spark-ml


    【解决方案1】:

    在 Spark 2.0.0 中,正确的做法是使用 org.apache.spark.ml.linalg.SQLDataTypes.VectorType 而不是 VectorUDT。它是在this issue 中介绍的。

    【讨论】:

    • 你是怎么找到这个的?我已经使用 spark 代码库四年了,但发现这令人费解。
    • 注:源代码有这个/** * User-defined type for [[Vector]] in [[mllib-local]] which allows easy interaction with SQL * via [[org.apache.spark.sql.Dataset]]. */ private[spark] class VectorUDT extends UserDefinedType[Vector] { 所以这里没有提到VectorType ..
    • 我真的不记得我是如何找到它的,但我记得花了很长时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多