跳过内容

本文介绍了 arrow 提供的各种数据和元数据对象类型,并说明了这些对象的结构。

Arrow 元数据类

arrow 包定义了以下用于表示元数据的类

  • Schema 是一个 Field 对象列表,用于描述表格数据对象的结构;其中
  • Field 指定一个字符串名称和一个 DataType;并且
  • DataType 是一个控制值如何表示的属性

考虑这个例子

df <- data.frame(x = 1:3, y = c("a", "b", "c"))
tb <- arrow_table(df)
tb$schema
## Schema
## x: int32
## y: string
## 
## See $metadata for additional Schema metadata

自动推断的 schema 也可以手动创建

schema(
  field(name = "x", type = int32()),
  field(name = "y", type = utf8())
)
## Schema
## x: int32
## y: string

schema() 函数允许使用以下简写来定义字段

schema(x = int32(), y = utf8())
## Schema
## x: int32
## y: string

有时手动指定 schema 很重要,尤其是当您希望对 Arrow 数据类型进行精细控制时

arrow_table(df, schema = schema(x = int64(), y = utf8()))
## Table
## 3 rows x 2 columns
## $x <int64>
## $y <string>
## 
## See $metadata for additional Schema metadata
arrow_table(df, schema = schema(x = float64(), y = utf8()))
## Table
## 3 rows x 2 columns
## $x <double>
## $y <string>
## 
## See $metadata for additional Schema metadata

R 对象属性

Arrow 支持附加到 Schemas 的自定义键值元数据。当我们将 data.frame 转换为 Arrow Table 或 RecordBatch 时,包会将附加到 data.frame 列的任何 attributes() 存储在 Arrow 对象 Schema 中。以这种方式添加到对象的属性存储在 r 键下,如下所示

# data frame with custom metadata
df <- data.frame(x = 1:3, y = c("a", "b", "c"))
attr(df, "df_meta") <- "custom data frame metadata"
attr(df$y, "col_meta") <- "custom column metadata"

# when converted to a Table, the metadata is preserved
tb <- arrow_table(df)
tb$metadata
## $r
## $r$attributes
## $r$attributes$df_meta
## [1] "custom data frame metadata"
## 
## 
## $r$columns
## $r$columns$x
## NULL
## 
## $r$columns$y
## $r$columns$y$attributes
## $r$columns$y$attributes$col_meta
## [1] "custom column metadata"
## 
## 
## $r$columns$y$columns
## NULL

也可以使用如下命令在任何其他您希望的键下分配额外的字符串元数据

tb$metadata$new_key <- "new value"

当将 Table 写入 Arrow/Feather 或 Parquet 格式时,附加到 Schema 的元数据会保留。当将这些文件读入 R,或在 Table 或 RecordBatch 上调用 as.data.frame() 时,列属性会恢复到结果 data.frame 的列中。这意味着自定义数据类型,包括 haven::labelledvctrs 注释和其他,在通过 Arrow 进行往返时会保留。

请注意,存储在 $metadata$r 中的属性仅R能理解。如果您将包含 haven 列的 data.frame 写入 Feather 文件并在 Pandas 中读取,则 haven 元数据在那里将无法识别。同样,Pandas 写入其自己的自定义元数据,R 包不会使用。但是,您可以为您的应用程序定义自定义元数据约定,并为其他元数据键分配任何(字符串)值。

进一步阅读

  • 要了解有关 arrow 元数据的更多信息,请参阅 schema() 的文档。
  • 要了解有关数据类型的更多信息,请参阅数据类型文章