gpt4 book ai didi

scala - 如何解决 java.lang.NumberFormatException : null 问题

转载 作者:行者123 更新时间:2023-12-01 01:48:50 26 4
gpt4 key购买 nike

我正在加载一个包含大约 500,000 条记录的文件,例如这样

ROW_ID, COLOR_CODE, SHADE_ID
21, 22, 321
23, 31, 321

我这样加载它:

 val colorSchema = StructType(Array(
StructField("ROW_ID", IntegerType, true),
StructField("COLOR_CODE", IntegerType, true),
StructField("SHADE_ID", IntegerType, true)

def makeSchema(filename:String, tableName:String,
tableSchema:StructType,uri:String){

val table = spark.read.
format("com.databricks.spark.csv").
option("header", "true").
schema(tableSchema).load(uri+filename).cache()
table.registerTempTable(tableName.toUpperCase)
}

makeSchema("colors.csv","colors",colorSchema,"s3://bucket/")

上面的代码运行良好。但是,当我运行以下查询时,出现错误 java.lang.NumberFormatException: null

val r = spark.sql("select * from colors where COLOR_CODE = 22").take(1)

我做错了什么?我怎样才能有效地发现这个问题?我已目视扫描文件以查看 COLOR_CODE 是否缺少值,但我看不到任何视觉...

更新

我提出了一个单独的问题,进一步缩小了问题范围。 CSV 现在只有 1 行,但我仍然遇到相同的错误。 https://stackoverflow.com/questions/40564201/how-to-resolve-java-lang-numberformatexception-null-in-spark-sql

最佳答案

也许您的 csv 中有 null/空值,或其他无法解析为 int 的字符串。

如果问题出在空值上,您可以尝试以下操作:

val table = spark.read.
format("com.databricks.spark.csv").
option("header", "true").
option("nullValue","null").
option("treatEmptyValuesAsNulls,","true").
schema(tableSchema).load(uri+filename).cache()

关于scala - 如何解决 java.lang.NumberFormatException : null 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563777/

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