【问题标题】:How do I increase decimal precision in Spark?如何提高 Spark 中的小数精度?
【发布时间】:2017-11-01 20:39:25
【问题描述】:

我有一个大的 DataFrame,它由约 550 列双精度和两列长 (id) 组成。正在从 csv 读取 550 列,我添加了两个 id 列。我对数据所做的唯一其他事情是将一些 csv 数据从字符串更改为双精度(“Inf”->“0”然后将列转换为双精度)并将 NaN 替换为 0:

df = df.withColumn(col.name + "temp", 
                             regexp_replace(
                                 regexp_replace(df(col.name),"Inf","0")
                                 ,"NaN","0").cast(DoubleType))
df = df.drop(col.name).withColumnRenamed(col.name + "temp",col.name)
df = df.withColumn("timeId", monotonically_increasing_id.cast(LongType))
df = df.withColumn("patId", lit(num).cast(LongType))
df = df.na.fill(0)

当我进行计数时,我收到以下错误:

IllegalArgumentException: requirement failed: Decimal precision 6 exceeds max precision 5

有数十万行,我正在从多个 csv 中读取数据。如何提高小数精度?还有其他可能发生的事情吗?当我阅读一些 csv 文件时,我只会收到此错误。他们能比其他人有更多的小数吗?

【问题讨论】:

    标签: python scala apache-spark spark-dataframe bigdata


    【解决方案1】:

    我认为该错误很容易解释 - 您需要使用 DecimalType 而不是 DoubleType

    试试这个:

    ...
    .cast(DecimalType(6)))
    

    继续阅读:

    https://spark.apache.org/docs/2.1.0/api/java/org/apache/spark/sql/types/DecimalType.html

    http://spark.apache.org/docs/2.0.2/api/python/_modules/pyspark/sql/types.html

    datatype for handling big numbers in pyspark

    【讨论】:

    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多