使用spark-csv重载方法错误

时间:2015-11-10 17:08:28

标签: scala apache-spark apache-spark-sql

我正在使用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

1 个答案:

答案 0 :(得分:8)

您需要将StructField&#39}作为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上没有显示,但是如果你查看了http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events-columnResize文档,那么它就清楚了。