我们假设我们有一个Spark DataFrame
df.getClass
Class[_ <: org.apache.spark.sql.DataFrame] = class org.apache.spark.sql.DataFrame
Run Code Online (Sandbox Code Playgroud)
使用以下架构
df.printSchema
root
|-- rawFV: string (nullable = true)
|-- tk: array (nullable = true)
| |-- element: string (containsNull = true)
Run Code Online (Sandbox Code Playgroud)
鉴于列的每一行tk都是一个字符串数组,如何编写一个Scala函数来返回每行中的元素数量?
让我们定义一个Spark管道,它将几列组合在一起,然后应用特征哈希:
val df = sqlContext.createDataFrame(Seq((0.0, 1.0, 2.0), (3.0, 4.0, 5.0))).toDF("colx", "coly", "colz")
val va = new VectorAssembler().setInputCols(Array("colx", "coly", "colz")).setOutputCol("ft")
val hashIt = new HashingTF().setInputCol("ft").setOutputCol("ft2")
val pipeline = new Pipeline().setStages(Array(va, hashIt))
Run Code Online (Sandbox Code Playgroud)
使用pipeline.fit(df)throws 安装管道:
java.lang.IllegalArgumentException:要求失败:输入列必须是ArrayType,但是得到了org.apache.spark.mllib.linalg.VectorUDT@f71b0bce
是否有允许VectorAssembler并HashingTF能够一起工作的变压器?