我正在使用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很陌生,所以解析它很困难,但我在这里做错了什么?我在这里举一个非常简单的例子。
您需要将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
文档,这一点就很清楚了。