scala – 使用spark-csv重载方法错误

我正在使用Databricks spark-csv包(通过Scala API),并且在定义自定义模式时遇到问题.

用启动控制台后

spark-shell  --packages com.databricks:spark-csv_2.11:1.2.0

我导入了我必要的类型

import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType}

然后只需尝试定义此架构:

val customSchema = StructType(
    StructField("user_id", IntegerType, true),
    StructField("item_id", IntegerType, true),
    StructField("artist_id", IntegerType, true),
    StructField("scrobble_time", StringType, true))

但是我收到以下错误:

<console>:26: error: overloaded method value apply with alternatives:
  (fields: Array[org.apache.spark.sql.types.StructField])org.apache.spark.sql.types.StructType <and>
  (fields: java.util.List[org.apache.spark.sql.types.StructField])org.apache.spark.sql.types.StructType <and>
  (fields: Seq[org.apache.spark.sql.types.StructField])org.apache.spark.sql.types.StructType
 cannot be applied to (org.apache.spark.sql.types.StructField, org.apache.spark.sql.types.StructField, org.apache.spark.sql.types.StructField, org.apache.spark.sql.types.StructField)
       val customSchema = StructType(

我对scala很新,所以解析这个问题很困难,但我在这里做错了什么?我正在按照非常简单的例子here.

最佳答案
你需要将你的StructField集合作为Seq传递.

像以下任何一种作品:

val customSchema = StructType(Seq(StructField("user_id", IntegerType, true), StructField("item_id", IntegerType, true), StructField("artist_id", IntegerType, true), StructField("scrobble_time", StringType, true)))

val customSchema = (new StructType)
  .add("user_id", IntegerType, true)
  .add("item_id", IntegerType, true)
  .add("artist_id", IntegerType, true)
  .add("scrobble_time", StringType, true)

val customSchema = StructType(StructField("user_id", IntegerType, true) :: StructField("item_id", IntegerType, true) :: StructField("artist_id", IntegerType, true) :: StructField("scrobble_time", StringType, true) :: Nil)

我不确定为什么它不会在README上显示,但是如果你查看StructType文档,那么它就很清楚了.

转载注明原文:scala – 使用spark-csv重载方法错误 - 代码日志