scala - 转换不兼容的 DecimalType 与 ClassCastException 时的 Apache Spark Null 值

标签 scala apache-spark casting

类型转换 DecimalType(10,5 ) 例如99999.99999DecimalType( 5,4) 在 Apache Spark 中默默返回 null
在这种情况下,是否可以更改此行为并允许 Spark 抛出异常(例如某些 CastException)并使作业失败而不是静默返回 null ?

最佳答案

根据 Git 中心文档,https://github.com/apache/spark/blob/3ab96d7acf870e53c9016b0b63d0b328eec23bed/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L499

/** * Change the precision / scale in a given decimal to those set in decimalType (if any), * returning null if it overflows or modifying value in-place and returning it if successful. * * NOTE: this modifies value in-place, so don't call it on external data. */



还有另一个线程,表明如果无法转换,则可能没有直接方法使代码失败。 Spark: cast decimal without changing nullable property of column .
所以,也许你可以尝试检查 null类型转换列中的值并创建逻辑以失败(如果有)?

关于scala - 转换不兼容的 DecimalType 与 ClassCastException 时的 Apache Spark Null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688810/

相关文章:

scala - sbt:如何编写仅在测试失败时才运行 testQuick 的任务?

scala - Typesafe Play WS 作为 SBT 项目中的依赖项

scala - 找到 : org. apache.spark.sql.Dataset[(Double, Double)] 需要 : org. apache.spark.rdd.RDD[(Double, Double)]

python - 在 Spark RDD 和/或 Spark DataFrames 中 reshape /透视数据

JAVA函数重载与泛型类型问题

scala - 重新分配给 Scala 中的 val

scala - Spark : Read and Write to Parquet leads to OutOfMemoryError: Java heap space

c++ - 如何为相等的模板参数类型专门化/重载函数?

java - 促进原始类型

Scala:无法从内部类引用中获取外部类成员