【问题标题】:Custom schema in spark-csv throwing error in spark 1.4.1spark-csv 中的自定义模式在 spark 1.4.1 中抛出错误
【发布时间】:2015-12-21 14:41:08
【问题描述】:

我尝试在 spark 1.4.1 的 spark-shell 中使用 spark -csv 包处理 CSV 文件。

scala> import org.apache.spark.sql.hive.HiveContext                                                                                                  
import org.apache.spark.sql.hive.HiveContext                                                                                                         

scala> import org.apache.spark.sql.hive.orc._                                                                                                        
import org.apache.spark.sql.hive.orc._                                                                                                               

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

scala> val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)                                                                               
15/12/21 02:06:24 WARN SparkConf: The configuration key 'spark.yarn.applicationMaster.waitTries' has been deprecated as of Spark 1.3 and and may be removed in the future. Please use the new key 'spark.yarn.am.waitTime' instead.                                                                       
15/12/21 02:06:24 INFO HiveContext: Initializing execution hive, version 0.13.1                                                                      
hiveContext: org.apache.spark.sql.hive.HiveContext = org.apache.spark.sql.hive.HiveContext@74cba4b                                                   

scala> val customSchema = StructType(Seq(StructField("year", IntegerType, true),StructField("make", StringType, true),StructField("model", StringType, true),StructField("comment", StringType, true),StructField("blank", StringType, true)))
customSchema: org.apache.spark.sql.types.StructType = StructType(StructField(year,IntegerType,true), StructField(make,StringType,true), StructField(model,StringType,true), StructField(comment,StringType,true), StructField(blank,StringType,true))                                                     

scala> val customSchema = (new StructType).add("year", IntegerType, true).add("make", StringType, true).add("model", StringType, true).add("comment", StringType, true).add("blank", StringType, true)
:24: error: not enough arguments for constructor StructType: (fields: Array[org.apache.spark.sql.types.StructField])org.apache.spark.sql.types.StructType. Unspecified value parameter fields.                                                                                                                  

val customSchema = (new StructType).add("year", IntegerType, true).add("make", StringType, true).add("model", StringType,true).add("comment", StringType, true).add("blank", StringType, true)   

【问题讨论】:

    标签: apache-spark spark-dataframe spark-csv


    【解决方案1】:

    根据 Spark 1.4.1 文档,StructType 没有无参数构造函数,这就是您收到错误的原因。您需要升级到 1.5.x 以获取无参数构造函数,或者按照第一个示例中的建议创建架构。

    val customSchema = StructType(Seq(StructField("year", IntegerType, true),StructField("make", StringType, true),StructField("model", StringType, true),StructField("comment", StringType, true),StructField("blank", StringType, true)))
    

    【讨论】:

    • 您能否将此问题标记为已回答,请:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2015-02-04
    • 1970-01-01
    • 2023-03-09
    • 2014-11-17
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多