Sparklyr - 小数精度 8 超过最大精度 7

Sparklyr - Decimal precision 8 exceeds max precision 7

我正在尝试使用 spark_read_csv 将一个大数据库复制到 Spark 中,但我收到以下错误输出:

Error: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 16.0 failed 4 times, most recent failure: Lost task 0.3 in stage 16.0 (TID 176, 10.1.2.235): java.lang.IllegalArgumentException: requirement failed: Decimal precision 8 exceeds max precision 7

data_tbl <- spark_read_csv(sc, "data", "D:/base_csv", delimiter = "|", overwrite = TRUE)

这是一个大数据集,大约有 580 万条记录,我的数据集有 Intnumchr.

类型的数据

我认为您有几个选项,具体取决于您使用的 spark 版本

Spark >=1.6.1

从这里开始:https://docs.databricks.com/spark/latest/sparkr/functions/read.df.html 看来,您可以专门指定您的架构以强制它使用双打

csvSchema <- structType(structField("carat", "double"), structField("color", "string"))
diamondsLoadWithSchema<- read.df("/databricks-datasets/Rdatasets/data-001/csv/ggplot2/diamonds.csv",
                                 source = "csv", header="true", schema = csvSchema)

Spark < 1.6.1 考虑 test.csv

1,a,4.1234567890
2,b,9.0987654321

您可以轻松地提高效率,但我认为您已经掌握了要点

linesplit <- function(x){
  tmp <- strsplit(x,",")
  return ( tmp)
}

lineconvert <- function(x){
  arow <- x[[1]]
  converted <- list(as.integer(arow[1]), as.character(arow[2]),as.double(arow[3]))
  return (converted)
}
rdd <- SparkR:::textFile(sc,'/path/to/test.csv')
lnspl <- SparkR:::map(rdd, linesplit)
ll2 <- SparkR:::map(lnspl,lineconvert)
ddf <- createDataFrame(sqlContext,ll2)
head(ddf)

  _1 _2           _3
1  1  a 4.1234567890
2  2  b 9.0987654321

注意:SparkR::: 方法是私有的是有原因的,文档说 'be careful when you use this'