基本 Arrow 数据结构#

Apache Arrow 提供用于表示数据的基本数据结构:ArrayChunkedArrayRecordBatchTable。本文介绍了如何从原始数据类型构建这些数据结构;具体来说,我们将使用表示天、月和年的不同大小的整数。我们将使用它们来创建以下数据结构

  1. Arrow Arrays

  2. ChunkedArrays

  3. RecordBatch,来自 Arrays

  4. Table,来自 ChunkedArrays

先决条件#

在继续之前,请确保您拥有

  1. Arrow 安装,您可以在此处设置:在您自己的项目中使用 Arrow C++

  2. 了解如何使用基本的 C++ 数据结构

  3. 了解基本的 C++ 数据类型

设置#

在尝试使用 Arrow 之前,我们需要填补一些空白

  1. 我们需要包含必要的头文件。

  2. A main() 需要将所有东西粘合在一起。

包含文件#

首先,和以往一样,我们需要一些包含文件。我们将获得 iostream 用于输出,然后从 api.h 导入 Arrow 的基本功能,如下所示

#include <arrow/api.h>

#include <iostream>

Main()#

接下来,我们需要一个 main() - Arrow 的常见模式如下

int main() {
  arrow::Status st = RunMain();
  if (!st.ok()) {
    std::cerr << st << std::endl;
    return 1;
  }
  return 0;
}

这使我们能够轻松地使用 Arrow 的错误处理宏,这些宏将返回到 main(),如果发生故障,将返回一个 arrow::Status 对象 - 并且这个 main() 将报告错误。请注意,这意味着 Arrow 从不抛出异常,而是依赖于返回 Status。有关更多信息,请阅读此处:约定

为了配合这个 main(),我们有一个 RunMain(),任何 Status 对象都可以从中返回 - 我们将在此处编写程序的其余部分

arrow::Status RunMain() {

制作一个 Arrow 数组#

构建 int8 数组#

鉴于我们有一些数据在标准 C++ 数组中,并且想要使用 Arrow,我们需要将数据从这些数组移动到 Arrow 数组。我们仍然保证 Array 中内存的连续性,因此在使用 Array 与 C++ 数组时,不用担心性能损失。构造 Array 的最简单方法是使用 ArrayBuilder

另请参见

数组,了解有关 Array 的更多技术细节

以下代码初始化了一个用于将保存 8 位整数的 ArrayArrayBuilder。具体来说,它使用在具体 arrow::ArrayBuilder 子类中存在的 AppendValues() 方法,将标准 C++ 数组的内容填充到 ArrayBuilder 中。请注意 ARROW_RETURN_NOT_OK 的使用。如果 AppendValues() 失败,这个宏将返回到 main(),它将打印出故障的含义。

  // Builders are the main way to create Arrays in Arrow from existing values that are not
  // on-disk. In this case, we'll make a simple array, and feed that in.
  // Data types are important as ever, and there is a Builder for each compatible type;
  // in this case, int8.
  arrow::Int8Builder int8builder;
  int8_t days_raw[5] = {1, 12, 17, 23, 28};
  // AppendValues, as called, puts 5 values from days_raw into our Builder object.
  ARROW_RETURN_NOT_OK(int8builder.AppendValues(days_raw, 5));

ArrayBuilder 具有我们希望在 Array 中的值后,我们可以使用 ArrayBuilder::Finish() 将最终结构输出到 Array - 具体来说,我们将输出到 std::shared_ptr<arrow::Array>。请注意以下代码中 ARROW_ASSIGN_OR_RAISE 的使用。 Finish() 输出一个 arrow::Result 对象,ARROW_ASSIGN_OR_RAISE 可以处理此对象。如果方法失败,它将返回到 main(),并返回一个解释错误原因的 Status。如果成功,它将最终输出分配给左手变量。

  // We only have a Builder though, not an Array -- the following code pushes out the
  // built up data into a proper Array.
  std::shared_ptr<arrow::Array> days;
  ARROW_ASSIGN_OR_RAISE(days, int8builder.Finish());

一旦 ArrayBuilder 调用了其 Finish 方法,其状态将重置,因此它可以再次使用,就像它是全新的。因此,我们对第二个数组重复上述过程

  // Builders clear their state every time they fill an Array, so if the type is the same,
  // we can re-use the builder. We do that here for month values.
  int8_t months_raw[5] = {1, 3, 5, 7, 1};
  ARROW_RETURN_NOT_OK(int8builder.AppendValues(months_raw, 5));
  std::shared_ptr<arrow::Array> months;
  ARROW_ASSIGN_OR_RAISE(months, int8builder.Finish());

构建 int16 数组#

ArrayBuilder 的类型在声明时指定。一旦完成,它的类型就不能更改。当我们切换到年份数据时,我们必须创建一个新的,年份数据需要一个至少为 16 位的整数。当然,有一个 ArrayBuilder 可以做到这一点。它使用完全相同的方法,但使用新的数据类型

  // Now that we change to int16, we use the Builder for that data type instead.
  arrow::Int16Builder int16builder;
  int16_t years_raw[5] = {1990, 2000, 1995, 2000, 1995};
  ARROW_RETURN_NOT_OK(int16builder.AppendValues(years_raw, 5));
  std::shared_ptr<arrow::Array> years;
  ARROW_ASSIGN_OR_RAISE(years, int16builder.Finish());

现在,我们有三个 Arrow Arrays,类型略有不同。

制作一个 RecordBatch#

只有当您有表格时,列式数据格式才会真正发挥作用。因此,让我们创建一个。我们将制作的第一种是 RecordBatch - 它在内部使用 Arrays,这意味着所有数据在每列中都将是连续的,但是任何追加或连接都需要复制。在给定现有 Arrays 的情况下,制作 RecordBatch 包含两个步骤

  1. 定义一个 Schema

  2. Schema 和数组加载到构造函数中

定义一个 Schema#

要开始制作 RecordBatch,我们首先需要定义列的特征,每个特征都由一个 Field 实例表示。每个 Field 包含其关联列的名称和数据类型;然后,Schema 将它们组合在一起并设置列的顺序,如下所示

  // Now, we want a RecordBatch, which has columns and labels for said columns.
  // This gets us to the 2d data structures we want in Arrow.
  // These are defined by schema, which have fields -- here we get both those object types
  // ready.
  std::shared_ptr<arrow::Field> field_day, field_month, field_year;
  std::shared_ptr<arrow::Schema> schema;

  // Every field needs its name and data type.
  field_day = arrow::field("Day", arrow::int8());
  field_month = arrow::field("Month", arrow::int8());
  field_year = arrow::field("Year", arrow::int16());

  // The schema can be built from a vector of fields, and we do so here.
  schema = arrow::schema({field_day, field_month, field_year});

构建一个 RecordBatch#

有了上一节中Arrays中的数据,以及上一步中Schema中的列描述,我们就可以创建RecordBatch。请注意,列的长度是必需的,并且所有列的长度都是共享的。

  // With the schema and Arrays full of data, we can make our RecordBatch! Here,
  // each column is internally contiguous. This is in opposition to Tables, which we'll
  // see next.
  std::shared_ptr<arrow::RecordBatch> rbatch;
  // The RecordBatch needs the schema, length for columns, which all must match,
  // and the actual data itself.
  rbatch = arrow::RecordBatch::Make(schema, days->length(), {days, months, years});

  std::cout << rbatch->ToString();

现在,我们的数据以一种简洁的表格形式存储在RecordBatch中。我们将在后面的教程中讨论如何使用它。

创建ChunkedArray#

假设我们想要一个由子数组组成的数组,因为它在连接时避免数据复制、并行化工作、将每个块放入缓存或超过标准 Arrow Array 中 2,147,483,647 行限制时非常有用。为此,Arrow 提供了ChunkedArray,它可以由单个 Arrow Arrays 组成。在本例中,我们可以重复使用之前创建的数组作为分块数组的一部分,从而可以在不复制数据的情况下扩展它们。因此,让我们构建几个更多的Arrays,为了方便使用,我们使用相同的构建器。

  // Now, let's get some new arrays! It'll be the same datatypes as above, so we re-use
  // Builders.
  int8_t days_raw2[5] = {6, 12, 3, 30, 22};
  ARROW_RETURN_NOT_OK(int8builder.AppendValues(days_raw2, 5));
  std::shared_ptr<arrow::Array> days2;
  ARROW_ASSIGN_OR_RAISE(days2, int8builder.Finish());

  int8_t months_raw2[5] = {5, 4, 11, 3, 2};
  ARROW_RETURN_NOT_OK(int8builder.AppendValues(months_raw2, 5));
  std::shared_ptr<arrow::Array> months2;
  ARROW_ASSIGN_OR_RAISE(months2, int8builder.Finish());

  int16_t years_raw2[5] = {1980, 2001, 1915, 2020, 1996};
  ARROW_RETURN_NOT_OK(int16builder.AppendValues(years_raw2, 5));
  std::shared_ptr<arrow::Array> years2;
  ARROW_ASSIGN_OR_RAISE(years2, int16builder.Finish());

为了支持在创建ChunkedArray 时使用任意数量的Arrays,Arrow 提供了ArrayVector。它为Arrays 提供了一个向量,我们将在下面使用它来准备创建ChunkedArray

  // ChunkedArrays let us have a list of arrays, which aren't contiguous
  // with each other. First, we get a vector of arrays.
  arrow::ArrayVector day_vecs{days, days2};

为了利用 Arrow,我们需要采取最后一步,并将其转换为ChunkedArray

  // Then, we use that to initialize a ChunkedArray, which can be used with other
  // functions in Arrow! This is good, since having a normal vector of arrays wouldn't
  // get us far.
  std::shared_ptr<arrow::ChunkedArray> day_chunks =
      std::make_shared<arrow::ChunkedArray>(day_vecs);

有了表示天值的ChunkedArray,我们只需要对月和年数据重复此过程。

  // Repeat for months.
  arrow::ArrayVector month_vecs{months, months2};
  std::shared_ptr<arrow::ChunkedArray> month_chunks =
      std::make_shared<arrow::ChunkedArray>(month_vecs);

  // Repeat for years.
  arrow::ArrayVector year_vecs{years, years2};
  std::shared_ptr<arrow::ChunkedArray> year_chunks =
      std::make_shared<arrow::ChunkedArray>(year_vecs);

这样,我们就得到了三个类型不同的ChunkedArrays

创建 Table#

我们可以使用上一节中的ChunkedArrays 来做的一件非常有用的事情就是创建Tables。与RecordBatch 类似,Table 存储表格数据。但是,Table 并不保证连续性,因为它是由ChunkedArrays 组成。这对于逻辑、并行化工作、将块放入缓存或超过Array 以及RecordBatch 中存在的 2,147,483,647 行限制非常有用。

如果你已经阅读了有关RecordBatch 的内容,你可能会注意到以下代码中的Table 构造函数实际上是相同的,只是它将列的长度放在位置 3,并创建一个Table。我们重复使用之前定义的Schema,并创建我们的Table

  // A Table is the structure we need for these non-contiguous columns, and keeps them
  // all in one place for us so we can use them as if they were normal arrays.
  std::shared_ptr<arrow::Table> table;
  table = arrow::Table::Make(schema, {day_chunks, month_chunks, year_chunks}, 10);

  std::cout << table->ToString();

现在,我们的数据以一种简洁的表格形式存储在Table 中。我们将在后面的教程中讨论如何使用它。

结束程序#

最后,我们只是返回Status::OK(),这样main() 就会知道我们已经完成了,并且一切正常。

  return arrow::Status::OK();
}

总结#

这样,您就创建了 Arrow 中的基本数据结构,并且可以在下一篇文章中继续学习如何使用文件 I/O 在程序中输入和输出这些数据。

请参考以下内容以获取完整的代码副本

 19// (Doc section: Includes)
 20#include <arrow/api.h>
 21
 22#include <iostream>
 23// (Doc section: Includes)
 24
 25// (Doc section: RunMain Start)
 26arrow::Status RunMain() {
 27  // (Doc section: RunMain Start)
 28  // (Doc section: int8builder 1 Append)
 29  // Builders are the main way to create Arrays in Arrow from existing values that are not
 30  // on-disk. In this case, we'll make a simple array, and feed that in.
 31  // Data types are important as ever, and there is a Builder for each compatible type;
 32  // in this case, int8.
 33  arrow::Int8Builder int8builder;
 34  int8_t days_raw[5] = {1, 12, 17, 23, 28};
 35  // AppendValues, as called, puts 5 values from days_raw into our Builder object.
 36  ARROW_RETURN_NOT_OK(int8builder.AppendValues(days_raw, 5));
 37  // (Doc section: int8builder 1 Append)
 38
 39  // (Doc section: int8builder 1 Finish)
 40  // We only have a Builder though, not an Array -- the following code pushes out the
 41  // built up data into a proper Array.
 42  std::shared_ptr<arrow::Array> days;
 43  ARROW_ASSIGN_OR_RAISE(days, int8builder.Finish());
 44  // (Doc section: int8builder 1 Finish)
 45
 46  // (Doc section: int8builder 2)
 47  // Builders clear their state every time they fill an Array, so if the type is the same,
 48  // we can re-use the builder. We do that here for month values.
 49  int8_t months_raw[5] = {1, 3, 5, 7, 1};
 50  ARROW_RETURN_NOT_OK(int8builder.AppendValues(months_raw, 5));
 51  std::shared_ptr<arrow::Array> months;
 52  ARROW_ASSIGN_OR_RAISE(months, int8builder.Finish());
 53  // (Doc section: int8builder 2)
 54
 55  // (Doc section: int16builder)
 56  // Now that we change to int16, we use the Builder for that data type instead.
 57  arrow::Int16Builder int16builder;
 58  int16_t years_raw[5] = {1990, 2000, 1995, 2000, 1995};
 59  ARROW_RETURN_NOT_OK(int16builder.AppendValues(years_raw, 5));
 60  std::shared_ptr<arrow::Array> years;
 61  ARROW_ASSIGN_OR_RAISE(years, int16builder.Finish());
 62  // (Doc section: int16builder)
 63
 64  // (Doc section: Schema)
 65  // Now, we want a RecordBatch, which has columns and labels for said columns.
 66  // This gets us to the 2d data structures we want in Arrow.
 67  // These are defined by schema, which have fields -- here we get both those object types
 68  // ready.
 69  std::shared_ptr<arrow::Field> field_day, field_month, field_year;
 70  std::shared_ptr<arrow::Schema> schema;
 71
 72  // Every field needs its name and data type.
 73  field_day = arrow::field("Day", arrow::int8());
 74  field_month = arrow::field("Month", arrow::int8());
 75  field_year = arrow::field("Year", arrow::int16());
 76
 77  // The schema can be built from a vector of fields, and we do so here.
 78  schema = arrow::schema({field_day, field_month, field_year});
 79  // (Doc section: Schema)
 80
 81  // (Doc section: RBatch)
 82  // With the schema and Arrays full of data, we can make our RecordBatch! Here,
 83  // each column is internally contiguous. This is in opposition to Tables, which we'll
 84  // see next.
 85  std::shared_ptr<arrow::RecordBatch> rbatch;
 86  // The RecordBatch needs the schema, length for columns, which all must match,
 87  // and the actual data itself.
 88  rbatch = arrow::RecordBatch::Make(schema, days->length(), {days, months, years});
 89
 90  std::cout << rbatch->ToString();
 91  // (Doc section: RBatch)
 92
 93  // (Doc section: More Arrays)
 94  // Now, let's get some new arrays! It'll be the same datatypes as above, so we re-use
 95  // Builders.
 96  int8_t days_raw2[5] = {6, 12, 3, 30, 22};
 97  ARROW_RETURN_NOT_OK(int8builder.AppendValues(days_raw2, 5));
 98  std::shared_ptr<arrow::Array> days2;
 99  ARROW_ASSIGN_OR_RAISE(days2, int8builder.Finish());
100
101  int8_t months_raw2[5] = {5, 4, 11, 3, 2};
102  ARROW_RETURN_NOT_OK(int8builder.AppendValues(months_raw2, 5));
103  std::shared_ptr<arrow::Array> months2;
104  ARROW_ASSIGN_OR_RAISE(months2, int8builder.Finish());
105
106  int16_t years_raw2[5] = {1980, 2001, 1915, 2020, 1996};
107  ARROW_RETURN_NOT_OK(int16builder.AppendValues(years_raw2, 5));
108  std::shared_ptr<arrow::Array> years2;
109  ARROW_ASSIGN_OR_RAISE(years2, int16builder.Finish());
110  // (Doc section: More Arrays)
111
112  // (Doc section: ArrayVector)
113  // ChunkedArrays let us have a list of arrays, which aren't contiguous
114  // with each other. First, we get a vector of arrays.
115  arrow::ArrayVector day_vecs{days, days2};
116  // (Doc section: ArrayVector)
117  // (Doc section: ChunkedArray Day)
118  // Then, we use that to initialize a ChunkedArray, which can be used with other
119  // functions in Arrow! This is good, since having a normal vector of arrays wouldn't
120  // get us far.
121  std::shared_ptr<arrow::ChunkedArray> day_chunks =
122      std::make_shared<arrow::ChunkedArray>(day_vecs);
123  // (Doc section: ChunkedArray Day)
124
125  // (Doc section: ChunkedArray Month Year)
126  // Repeat for months.
127  arrow::ArrayVector month_vecs{months, months2};
128  std::shared_ptr<arrow::ChunkedArray> month_chunks =
129      std::make_shared<arrow::ChunkedArray>(month_vecs);
130
131  // Repeat for years.
132  arrow::ArrayVector year_vecs{years, years2};
133  std::shared_ptr<arrow::ChunkedArray> year_chunks =
134      std::make_shared<arrow::ChunkedArray>(year_vecs);
135  // (Doc section: ChunkedArray Month Year)
136
137  // (Doc section: Table)
138  // A Table is the structure we need for these non-contiguous columns, and keeps them
139  // all in one place for us so we can use them as if they were normal arrays.
140  std::shared_ptr<arrow::Table> table;
141  table = arrow::Table::Make(schema, {day_chunks, month_chunks, year_chunks}, 10);
142
143  std::cout << table->ToString();
144  // (Doc section: Table)
145
146  // (Doc section: Ret)
147  return arrow::Status::OK();
148}
149// (Doc section: Ret)
150
151// (Doc section: Main)
152int main() {
153  arrow::Status st = RunMain();
154  if (!st.ok()) {
155    std::cerr << st << std::endl;
156    return 1;
157  }
158  return 0;
159}
160
161// (Doc section: Main)