【问题标题】:Adding name of file when using sparklyr::spark_read_json使用 sparklyr::spark_read_json 时添加文件名
【发布时间】:2018-12-28 15:00:34
【问题描述】:

我有数百万个 json 文件,其中每个文件都包含相同数量的列,比如 xy。请注意,xy 的长度对于单个文件是相等的,但在比较两个不同文件时可能会有所不同。

问题在于,唯一分隔数据的是文件名。因此,在组合文件时,我希望将文件名包含在第三列中。这是否可能使用sparklyr::spark_read_json,即使用通配符时?

MWE:

library(sparklyr)

## Spark connection
sc <- spark_connect(master = "local", version = "2.1.0")

## Create data
data_dir <- tempdir()
tbl_json1 <- data.frame(x = 1:3, y = 1:3)
tbl_json2 <- data.frame(x = 1:10, y = 1:10)

## Write data to disk
write(jsonlite::toJSON(tbl_json1), sprintf("%s/tab1.json", data_dir))
write(jsonlite::toJSON(tbl_json2), sprintf("%s/tab2.json", data_dir))

## Read both files using wildcard 
combined_table <- spark_read_json(
    sc, 
    name = "combined_table", 
    path = sprintf("%s/*.json", data_dir)
)

## Tranfer results to R
library(dplyr)
dt <- combined_table %>% collect

# # A tibble: 13 x 2
#       x     y
#     <dbl> <dbl>
#  1    1.    1.
#  2    2.    2.
#  3    3.    3.
#  4    4.    4.
#  5    5.    5.
#  6    6.    6.
#  7    7.    7.
#  8    8.    8.
#  9    9.    9.
# 10   10.   10.
# 11    1.    1.
# 12    2.    2.
# 13    3.    3.

想要的输出

# # A tibble: 13 x 2
#       x     y     id
#     <dbl> <dbl> <chr>
#  1    1.    1.    tab2
#  2    2.    2.    tab2
#  3    3.    3.    tab2
#  4    4.    4.    tab2
#  5    5.    5.    tab2
#  6    6.    6.    tab2
#  7    7.    7.    tab2
#  8    8.    8.    tab2
#  9    9.    9.    tab2
# 10   10.   10.    tab2
# 11    1.    1.    tab1
# 12    2.    2.    tab1
# 13    3.    3.    tab1

【问题讨论】:

    标签: r apache-spark dplyr sparklyr


    【解决方案1】:

    您可以禁用急切缓存(无论如何您都应该这样做):

    combined_table <- spark_read_json(
      sc, 
      name = "combined_table", 
      path = sprintf("%s/*.json", data_dir),
      memory=FALSE
    )
    

    并使用input_file_name function:

    combined_table %>% mutate(id = input_file_name())
    
    # Source:   lazy query [?? x 3]
    # Database: spark_connection
           x     y id                              
       <dbl> <dbl> <chr>                           
     1     1     1 file:///tmp/RtmpnIAUek/tab2.json
     2     2     2 file:///tmp/RtmpnIAUek/tab2.json
     3     3     3 file:///tmp/RtmpnIAUek/tab2.json
     4     4     4 file:///tmp/RtmpnIAUek/tab2.json
     5     5     5 file:///tmp/RtmpnIAUek/tab2.json
     6     6     6 file:///tmp/RtmpnIAUek/tab2.json
     7     7     7 file:///tmp/RtmpnIAUek/tab2.json
     8     8     8 file:///tmp/RtmpnIAUek/tab2.json
     9     9     9 file:///tmp/RtmpnIAUek/tab2.json
    10    10    10 file:///tmp/RtmpnIAUek/tab2.json
    # ... with more rows
    

    如果需要,可以结合Hive's parse_url UDF:

    combined_table %>% mutate(id = parse_url(input_file_name(), "FILE"))
    
    # Source:   lazy query [?? x 3]
    # Database: spark_connection
           x     y id                       
       <dbl> <dbl> <chr>                    
     1     1     1 /tmp/RtmpnIAUek/tab2.json
     2     2     2 /tmp/RtmpnIAUek/tab2.json
     3     3     3 /tmp/RtmpnIAUek/tab2.json
     4     4     4 /tmp/RtmpnIAUek/tab2.json
     5     5     5 /tmp/RtmpnIAUek/tab2.json
     6     6     6 /tmp/RtmpnIAUek/tab2.json
     7     7     7 /tmp/RtmpnIAUek/tab2.json
     8     8     8 /tmp/RtmpnIAUek/tab2.json
     9     9     9 /tmp/RtmpnIAUek/tab2.json
    10    10    10 /tmp/RtmpnIAUek/tab2.json
    # ... with more rows
    

    您可以使用其他字符串处理函数来提取单个信息位。

    【讨论】:

    • 您可以使用regexp_extract(id, "(?&lt;=/)[^/]*$", 0)提取文件名
    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 2017-04-04
    • 2023-03-22
    • 2011-09-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多