Scala 由于某些字段具有未知值,使用Spark写入拼花地板文件时出现数字格式异常
我正在使用spark读取一个文本文件,模式如下Scala 由于某些字段具有未知值,使用Spark写入拼花地板文件时出现数字格式异常,scala,apache-spark,apache-spark-sql,parquet,Scala,Apache Spark,Apache Spark Sql,Parquet,我正在使用spark读取一个文本文件,模式如下 root |-- id: long (nullable = true) |-- name: string (nullable = true) |-- style: string (nullable = true) |-- code: integer (nullable = true) |-- state_code: integer (nullable = true) |-- post_code: integer (nullable =
root
|-- id: long (nullable = true)
|-- name: string (nullable = true)
|-- style: string (nullable = true)
|-- code: integer (nullable = true)
|-- state_code: integer (nullable = true)
|-- post_code: integer (nullable = true)
|-- mail: string (nullable = true)
这是我的Scala代码
val myDf = sqlContext.read.format("csv")
.option("header", "true")
.option("inferSchema", "false")
.schema(myschema)
.option("delimiter", "|")
.option("nullValue", "")
.option("treatEmptyValuesAsNulls", "true")
.load("Path to file")
在此输入文件中,有些coulmn的值为\N。我想将所有coulmn的值\N改为空。当我尝试将此数据帧作为拼花文件写入时
myDf.na.replace(myDf.columns.toSeq, Map("\\N" -> "")).write.format("parquet").save("path to output")
它抛出了一个例外
java.lang.NumberFormatException: For input string: "\N"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:12)
at java.lang.Integer.parseInt(Integer.java:80)
通过解决问题来编写拼花地板文件有什么帮助吗?猜一猜,异常不是在书写中发生的,而是在阅读过程中发生的 如果将具有int字段的架构分配给CSV文件,并且文件中的字段值包含,则可能会发生此类错误
猜猜看,你可以用其他动作表演等代替拼花地板上的保存,并得到相同的例外情况。试试这个:Map\\\\N->@SCouto没有办法仍然得到相同的例外情况答案已经在你前面的问题中提供了。请检查一次@ChandanRay Solution notworking@Juhan我为您提供的解决方案是什么。请检查。让我们不要打开两个相同的问题。myDf.na.replacemyDf.columns,Map\\\\N->。显示也抛出相同的异常。请尝试不使用na.replace。同样从您的测试来看,很明显,这不是拼花地板保存问题,而是读取问题,需要另一个修复。是的,即使是myDf.show也会抛出相同的异常手段,架构与CSV文件结构不匹配,可以更正。架构已修复,读取文件时是否有方法\N替换