簡體   English   中英

org.apache.spark.sql.types.DataTypeException:不支持的dataType:IntegerType

[英]org.apache.spark.sql.types.DataTypeException: Unsupported dataType: IntegerType

我是Spark和Scala的新手,我對此異常感到困惑,我試圖將一些額外字段(即StructField)添加到使用Spark SQL從Data Frame檢索到的現有StructType中,以使用Spark SQL並在異常下方進行getting。

代碼段:

val dfStruct:StructType=parquetDf.select("columnname").schema
dfStruct.add("newField","IntegerType",true)

線程“主”中的異常

 org.apache.spark.sql.types.DataTypeException: Unsupported dataType: IntegerType. If you have a struct and a field name of it has any special characters, please use backticks (`) to quote that field name, e.g. `x+y`. Please note that backtick itself is not supported in a field name.
    at org.apache.spark.sql.types.DataTypeParser$class.toDataType(DataTypeParser.scala:95)
    at org.apache.spark.sql.types.DataTypeParser$$anon$1.toDataType(DataTypeParser.scala:107)
    at org.apache.spark.sql.types.DataTypeParser$.parse(DataTypeParser.scala:111)

我可以看到在jira上運行了一些與此異常相關的未解決問題,但了解得不多。 我正在使用Spark 1.5.1版本

https://mail-archives.apache.org/mod_mbox/spark-issues/201508.mbox/%3CJIRA.12852533.1438855066000.143133.1440397426473@Atlassian.JIRA%3E

https://mail-archives.apache.org/mod_mbox/spark-issues/201508.mbox/%3CJIRA.12852533.1438855066000.143133.1440397426473@Atlassian.JIRA%3E

https://issues.apache.org/jira/browse/SPARK-9685

當您將StructType.add與以下簽名一起使用時:

add(name: String, dataType: String, nullable: Boolean)

dataType字符串應對應於.simpleString.typeName 對於IntegerType它可以是int

import org.apache.spark.sql.types._

IntegerType.simpleString
// String = int

integer

IntegerType.typeName
// String = integer

所以您需要的是這樣的:

val schema = StructType(Nil)

schema.add("foo", "int", true)
// org.apache.spark.sql.types.StructType = 
//   StructType(StructField(foo,IntegerType,true))

要么

schema.add("foo", "integer", true)
// org.apache.spark.sql.types.StructType = 
//   StructType(StructField(foo,IntegerType,true))

如果要傳遞IntegerType ,則必須是DataType而不是String

schema.add("foo", IntegerType, true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM