【问题标题】:How to read a parquet file in R without using spark packages?如何在不使用火花包的情况下读取 R 中的镶木地板文件?
【发布时间】:2018-05-10 19:08:06
【问题描述】:

我可以通过使用 sparklyr 或使用不同的 spark 包在网上找到很多答案,这实际上需要启动一个 spark 集群,这是一种开销。在 python 中,我可以找到一种方法来使用“pandas.read_parquet”或 python 中的 Apache 箭头 - 我正在寻找类似的东西。

【问题讨论】:

  • 您将来也许还可以为此使用 Apache Arrow。有一个为其构建 R 绑定的拉取请求:github.com/apache/arrow/pull/1815 使用它们,您应该能够在没有火花的情况下在 R 中加载 Parquet 文件。
  • @xhochy 听起来不错。但除此之外,您认为我们现在还有什么可以使用的吗?
  • 我在 R 中使用 reticulate 包来利用 python read_parquet。它实际上工作得很好,读取文件非常快。唯一的问题是,将它从 pandas 数据帧转换为 r 数据帧需要 10 倍的时间。所以最后,如果性能不是问题,我只能推荐这种方法。作为奖励,如果这是一个问题(例如从 s3 加载时),文件会非常小。很难理解,R 在这里落后太多了。

标签: r parquet


【解决方案1】:

你可以简单地使用箭头包:

install.packages("arrow")
library(arrow)
read_parquet("myfile.parquet")

【讨论】:

    【解决方案2】:

    使用 reticulate,您可以使用 python 中的 pandas 来读取 parquet 文件。这可以为您省去运行 spark 实例的麻烦。在 apache arrow 发布它们的版本之前,可能会失去序列化性能。如上评论所述。

    library(reticulate)
    library(dplyr)
    pandas <- import("pandas")
    read_parquet <- function(path, columns = NULL) {
    
      path <- path.expand(path)
      path <- normalizePath(path)
    
      if (!is.null(columns)) columns = as.list(columns)
    
      xdf <- pandas$read_parquet(path, columns = columns)
    
      xdf <- as.data.frame(xdf, stringsAsFactors = FALSE)
    
      dplyr::tbl_df(xdf)
    
    }
    
    read_parquet(PATH_TO_PARQUET_FILE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 2019-02-19
      • 2016-01-18
      • 1970-01-01
      • 2022-01-20
      • 2016-08-31
      • 2023-03-20
      • 2017-03-17
      相关资源
      最近更新 更多