跳到內容

JsonTableReader 的包裝函式,用於將換行符分隔的 JSON (ndjson) 檔案讀取到資料框架或 Arrow 表格中。

用法

read_json_arrow(
  file,
  col_select = NULL,
  as_data_frame = TRUE,
  schema = NULL,
  ...
)

引數

file

字元檔案名稱或 URI、連線、字面資料(單一字串或 raw 向量)、Arrow 輸入串流,或具有路徑的 FileSystem (SubTreeFileSystem)。

如果為檔案名稱,則在完成時將開啟和關閉記憶體對應的 Arrow InputStream;壓縮將從檔案副檔名偵測並自動處理。如果提供輸入串流,則會保持開啟狀態。

若要識別為字面資料,輸入必須以 I() 包裝。

col_select

要保留的欄位名稱字元向量,如同 data.table::fread() 的 "select" 引數,或是欄位的 tidy selection specification,如同 dplyr::select() 中所使用的。

as_data_frame

函式應傳回 tibble (預設) 還是 Arrow Table

schema

描述表格的 Schema

...

傳遞給 JsonTableReader$create() 的其他選項

tibble,或如果 as_data_frame = FALSE 則為 Table。

詳細資訊

如果傳遞路徑,將從檔案副檔名偵測和處理壓縮 (例如 .json.gz)。

如果未提供 schema,則 Arrow 資料類型會從資料中推斷。

  • JSON null 值會轉換為 null() 類型,但可以回退到任何其他類型。

  • JSON 布林值會轉換為 boolean()

  • JSON 數字會轉換為 int64(),如果遇到非整數,則回退到 float64()

  • 種類為 "YYYY-MM-DD" 和 "YYYY-MM-DD hh:mm:ss" 的 JSON 字串會轉換為 timestamp(unit = "s"),如果發生轉換錯誤,則回退到 utf8()

  • JSON 陣列會轉換為 list_of() 類型,並且推斷會遞迴地在 JSON 陣列的值上進行。

  • 巢狀 JSON 物件會轉換為 struct() 類型,並且推斷會遞迴地在 JSON 物件的值上進行。

as_data_frame = TRUE 時,Arrow 類型會進一步轉換為 R 類型。

範例

tf <- tempfile()
on.exit(unlink(tf))
writeLines('
    { "hello": 3.5, "world": false, "yo": "thing" }
    { "hello": 3.25, "world": null }
    { "hello": 0.0, "world": true, "yo": null }
  ', tf, useBytes = TRUE)

read_json_arrow(tf)
#> # A tibble: 3 x 3
#>   hello world yo   
#>   <dbl> <lgl> <chr>
#> 1  3.5  FALSE thing
#> 2  3.25 NA    NA   
#> 3  0    TRUE  NA   

# Read directly from strings with `I()`
read_json_arrow(I(c('{"x": 1, "y": 2}', '{"x": 3, "y": 4}')))
#> # A tibble: 2 x 2
#>       x     y
#>   <int> <int>
#> 1     1     2
#> 2     3     4