使用定义的功能Spark 2.4?

时间:2019-04-08 12:31:49

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

我正在运行kmeans算法,创建一个VectorAssembler,将inputcols设置为(“经度”,“纬度”),并将outputCol设置为(“位置”)。 我需要将数据从json文件群集到3个群集。 我通过经度和纬度对数据进行分类,并创建矢量位置以将两者连接起来。 位置和纬度为DoubleType。 我认为是因为位置向量 我收到以下错误:

19/04/08 15:20:56 ERROR Executor: Exception in task 0.0 in stage 1.0 (TID 1)
org.apache.spark.SparkException: Failed to execute user defined function(VectorAssembler$$Lambda$1629/684426930: (struct<latitude:double,longitude:double>) => struct<type:tinyint,size:int,indices:array<int>,values:array<double>>)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)

这是我的代码:

import org.apache.spark.sql.{SQLContext, SparkSession}
import org.apache.spark.sql.types.{DataTypes, DoubleType, StructType}
import org.apache.spark.ml.clustering.{KMeans, KMeansModel}
import org.apache.spark.ml.evaluation.ClusteringEvaluator
import org.apache.spark.ml.feature
import org.apache.spark.{SparkConf, SparkContext, sql}
import org.apache.spark.ml.feature.{Binarizer, Interaction, VectorAssembler}
import org.apache.spark.sql
import org.apache.spark.sql.expressions.UserDefinedFunction
//plotting




object Clustering_kmeans {

  def main(args: Array[String]): Unit = {

    println("hello world me")

    // Spark Session
   val  sc = SparkSession.builder().appName("Clustering_Kmeans").master("local[*]").getOrCreate()


    import sc.implicits._
    sc.sparkContext.setLogLevel("WARN")
    // Loads data.
    val stations = sc.sqlContext.read.option("multiline",true).json("/home/aymenstien/Téléchargements/Brisbane_CityBike.json")
// trans
    val st = stations.withColumn("longitude", $"longitude".cast(sql.types.DoubleType))
      .withColumn("latitude", $"latitude".cast(sql.types.DoubleType)).cache()


    val stationVA = new VectorAssembler().setInputCols(Array("latitude","longitude")).setOutputCol("location")
    val stationWithLoc =stationVA.transform(st)


//print

    stationWithLoc.show(truncate = false)
    stationWithLoc.printSchema()
    //val x = st.select('longitude).as[Double].collect()
    //val y = st.select('latitude).as[Double].collect()

//st.printSchema()

  }

}

这是 架构

root
 |-- address: string (nullable = true)
 |-- coordinates: struct (nullable = true)
 |    |-- latitude: double (nullable = true)
 |    |-- longitude: double (nullable = true)
 |-- id: double (nullable = true)
 |-- latitude: double (nullable = true)
 |-- longitude: double (nullable = true)
 |-- name: string (nullable = true)
 |-- position: string (nullable = true)
 |-- location: vector (nullable = true)

1 个答案:

答案 0 :(得分:0)

由于所有功能都使用nullable = true,因此如果您有任何null,VectorAssembler都会引发错误。尝试将handleInvalid设置为"skip"。这将滤除任何空行。

val stationVA = new VectorAssembler().
                     setInputCols(Array("latitude","longitude")).
                     setOutputCol("location").
                     setHandleInvalid("skip")