代码之家  ›  专栏  ›  技术社区  ›  Juhan

使用Spark写入拼花文件时出现数字格式异常,原因是某些字段具有未知值[重复]

  •  0
  • Juhan  · 技术社区  · 7 年前

    我正在用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 = true)
     |-- mail: string (nullable = true)
    

     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")
    

    在此输入文件中,有些coulmns的值为“\N”。我想用空(“”)重新放置所有值为“\N”的coulmn valuse。当我试着把这个数据帧写成拼花文件时

     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)
    

    通过解决问题来编写拼花文件有什么帮助吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ferrmolina tschaible    7 年前

    我猜,例外不是在写作中发生的,而是在阅读中发生的。

    如果将int字段分配给CSV文件的架构,并且文件中的字段值包含“ \N ,则可能会发生这种错误。