gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:32:40 26 4
gpt4 key购买 nike

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

我不确定为什么自述文件中没有这样显示,但是如果您检查 StructType文档,这一点很清楚。

关于scala - 使用spark-csv重载方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33635606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com