使用spark-csv时出现了过载方法错误。

4
我正在使用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非常陌生,因此很难解析这个问题,但是我在按照这里的非常简单的例子进行操作。请问我在这里做错了什么?
1个回答

12
你需要将你的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文档,它就清楚了。


1
运行完美。但是,文档中的示例错误确实很不幸... - moustachio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接