File: io_tutorial.rst

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (404 lines) | stat: -rw-r--r-- 14,258 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements.  See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership.  The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License.  You may obtain a copy of the License at

..   http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
.. software distributed under the License is distributed on an
.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
.. KIND, either express or implied.  See the License for the
.. specific language governing permissions and limitations
.. under the License.

.. default-domain:: cpp
.. highlight:: cpp

.. cpp:namespace:: arrow

==============
Arrow File I/O
==============

Apache Arrow provides file I/O functions to facilitate use of Arrow from
the start to end of an application. In this article, you will:

1. Read an Arrow file into a :class:`RecordBatch` and write it back out afterwards

2. Read a CSV file into a :class:`Table` and write it back out afterwards

3. Read a Parquet file into a :class:`Table` and write it back out afterwards

Pre-requisites
---------------

Before continuing, make sure you have:

1. An Arrow installation, which you can set up here: :doc:`/cpp/build_system`

2. An understanding of basic Arrow data structures from :doc:`/cpp/tutorials/basic_arrow`

3. A directory to run the final application in – this program will generate some files, so be prepared for that.

Setup
-----

Before writing out some file I/O, we need to fill in a couple gaps:

1. We need to include necessary headers.

2. A ``main()`` is needed to glue things together.

3. We need files to play with.

Includes
^^^^^^^^

Before writing C++ code, we need some includes. We'll get ``iostream`` for output, then import Arrow's
I/O functionality for each file type we'll work with in this article:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Includes)
  :end-before: (Doc section: Includes)

Main()
^^^^^^

For our glue, we’ll use the ``main()`` pattern from the previous tutorial on
data structures:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Main)
  :end-before: (Doc section: Main)

Which, like when we used it before, is paired with a ``RunMain()``:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: RunMain)
  :end-before: (Doc section: RunMain)

Generating Files for Reading
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We need some files to actually play with. In practice, you’ll likely
have some input for your own application. Here, however, we want to
explore doing I/O for the sake of it, so let’s generate some files to make
this easy to follow. To create those, we’ll define a helper function
that we’ll run first. Feel free to read through this, but the concepts
used will be explained later in this article. Note that we’re using the
day/month/year data from the previous tutorial. For now, just copy the
function in:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: GenInitialFile)
  :end-before: (Doc section: GenInitialFile)

To get the files for the rest of your code to function, make sure to
call ``GenInitialFile()`` as the very first line in ``RunMain()`` to initialize
the environment:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Gen Files)
  :end-before: (Doc section: Gen Files)

I/O with Arrow Files
--------------------

We’re going to go through this step by step, reading then writing, as
follows:

1. Reading a file

   a. Open the file

   b. Bind file to :class:`ipc::RecordBatchFileReader`

   c. Read file to :class:`RecordBatch`

2. Writing a file

   a. Get a :class:`io::FileOutputStream`

   b. Write to file from :class:`RecordBatch`

Opening a File
^^^^^^^^^^^^^^

To actually read a file, we need to get some sort of way to point to it.
In Arrow, that means we’re going to get a :class:`io::ReadableFile` object – much
like an :class:`ArrayBuilder` can clear and make new arrays, we can reassign this
to new files, so we’ll use this instance throughout the examples:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: ReadableFile Definition)
  :end-before: (Doc section: ReadableFile Definition)

A :class:`io::ReadableFile` does little alone – we actually have it bind to a file
with :func:`io::ReadableFile::Open`. For
our purposes here, the default arguments suffice:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow ReadableFile Open)
  :end-before: (Doc section: Arrow ReadableFile Open)

Opening an Arrow file Reader
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

An :class:`io::ReadableFile` is too generic to offer all functionality to read an Arrow file.
We need to use it to get an :class:`ipc::RecordBatchFileReader` object. This object implements
all the logic needed to read an Arrow file with correct formatting. We get one through
:func:`ipc::RecordBatchFileReader::Open`:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Read Open)
  :end-before: (Doc section: Arrow Read Open)

Reading an Open Arrow File to RecordBatch
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We have to use a :class:`RecordBatch` to read an Arrow file, so we’ll get a
:class:`RecordBatch`. Once we have that, we can actually read the file. Arrow
files can have multiple :class:`RecordBatches <RecordBatch>`, so we must pass an index. This
file only has one, so pass 0:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Read)
  :end-before: (Doc section: Arrow Read)

Prepare a FileOutputStream
^^^^^^^^^^^^^^^^^^^^^^^^^^

For output, we need a :class:`io::FileOutputStream`. Just like our :class:`io::ReadableFile`,
we’ll be reusing this, so be ready for that. We open files the same way
as when reading:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Write Open)
  :end-before: (Doc section: Arrow Write Open)

Write Arrow File from RecordBatch
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Now, we grab our :class:`RecordBatch` we read into previously, and use it, along
with our target file, to create a :class:`ipc::RecordBatchWriter`. The
:class:`ipc::RecordBatchWriter` needs two things:

1. the target file

2. the :class:`Schema` for our :class:`RecordBatch` (in case we need to write more :class:`RecordBatches <RecordBatch>` of the same format.)

The :class:`Schema` comes from our existing :class:`RecordBatch` and the target file is
the output stream we just created.

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Writer)
  :end-before: (Doc section: Arrow Writer)

We can just call :func:`ipc::RecordBatchWriter::WriteRecordBatch` with our :class:`RecordBatch` to fill up our
file:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Write)
  :end-before: (Doc section: Arrow Write)

For IPC in particular, the writer has to be closed since it anticipates more than one batch may be written. To do that:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Arrow Close)
  :end-before: (Doc section: Arrow Close)

Now we’ve read and written an IPC file!

I/O with CSV
------------

We’re going to go through this step by step, reading then writing, as
follows:

1. Reading a file

   a. Open the file

   b. Prepare Table

   c. Read File using :class:`csv::TableReader`

2. Writing a file

   a. Get a :class:`io::FileOutputStream`

   b. Write to file from :class:`Table`

Opening a CSV File
^^^^^^^^^^^^^^^^^^

For a CSV file, we need to open a :class:`io::ReadableFile`, just like an Arrow file,
and reuse our :class:`io::ReadableFile` object from before to do so:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: CSV Read Open)
  :end-before: (Doc section: CSV Read Open)

Preparing a Table
^^^^^^^^^^^^^^^^^

CSV can be read into a :class:`Table`, so declare a pointer to a :class:`Table`:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: CSV Table Declare)
  :end-before: (Doc section: CSV Table Declare)

Read a CSV File to Table
^^^^^^^^^^^^^^^^^^^^^^^^

The CSV reader has option structs which need to be passed – luckily,
there are defaults for these which we can pass directly. For reference
on the other options, go here: :doc:`/cpp/api/formats`.
without any special delimiters and is small, so we can make our reader
with defaults:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: CSV Reader Make)
  :end-before: (Doc section: CSV Reader Make)

With the CSV reader primed, we can use its :func:`csv::TableReader::Read` method to fill our
:class:`Table`:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: CSV Read)
  :end-before: (Doc section: CSV Read)

Write a CSV File from Table
^^^^^^^^^^^^^^^^^^^^^^^^^^^

CSV writing to :class:`Table` looks exactly like IPC writing to :class:`RecordBatch`,
except with our :class:`Table`, and using :func:`ipc::RecordBatchWriter::WriteTable` instead of
:func:`ipc::RecordBatchWriter::WriteRecordBatch`. Note that the same writer class is used --
we're writing with :func:`ipc::RecordBatchWriter::WriteTable` because we have a :class:`Table`. We’ll target
a file, use our :class:`Table’s <Table>` :class:`Schema`, and then write the :class:`Table`:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: CSV Write)
  :end-before: (Doc section: CSV Write)

Now, we’ve read and written a CSV file!

File I/O with Parquet
---------------------

We’re going to go through this step by step, reading then writing, as
follows:

1. Reading a file

   a. Open the file

   b. Prepare :class:`parquet::arrow::FileReader`

   c. Read file to :class:`Table`

2. Writing a file

   a. Write :class:`Table` to file

Opening a Parquet File
^^^^^^^^^^^^^^^^^^^^^^

Once more, this file format, Parquet, needs a :class:`io::ReadableFile`, which we
already have, and for the :func:`io::ReadableFile::Open` method to be called on a file:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Parquet Read Open)
  :end-before: (Doc section: Parquet Read Open)

Setting up a Parquet Reader
^^^^^^^^^^^^^^^^^^^^^^^^^^^

As always, we need a Reader to actually read the file. We’ve been
getting Readers for each file format from the Arrow namespace. This
time, we enter the Parquet namespace to get the :class:`parquet::arrow::FileReader`:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Parquet FileReader)
  :end-before: (Doc section: Parquet FileReader)

Now, to set up our reader, we call :func:`parquet::arrow::OpenFile`. Yes, this is necessary
even though we used :func:`io::ReadableFile::Open`. Note that we pass our
:class:`parquet::arrow::FileReader` by reference, instead of assigning to it in output:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Parquet OpenFile)
  :end-before: (Doc section: Parquet OpenFile)

Reading a Parquet File to Table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

With a prepared :class:`parquet::arrow::FileReader` in hand, we can read to a
:class:`Table`, except we must pass the :class:`Table` by reference instead of outputting to it:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Parquet Read)
  :end-before: (Doc section: Parquet Read)

Writing a Parquet File from Table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For single-shot writes, writing a Parquet file does not need a writer object. Instead, we give
it our table, point to the memory pool it will use for any necessary
memory consumption, tell it where to write, and the chunk size if it
needs to break up the file at all:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Parquet Write)
  :end-before: (Doc section: Parquet Write)

Ending Program
--------------

At the end, we just return :func:`Status::OK`, so the ``main()`` knows that
we’re done, and that everything’s okay. Just like in the first tutorial.

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: Return)
  :end-before: (Doc section: Return)

With that, you’ve read and written IPC, CSV, and Parquet in Arrow, and
can properly load data and write output! Now, we can move into
processing data with compute functions in the next article.

Refer to the below for a copy of the complete code:

.. literalinclude:: ../../../../cpp/examples/tutorial_examples/file_access_example.cc
  :language: cpp
  :start-after: (Doc section: File I/O)
  :end-before: (Doc section: File I/O)
  :linenos:
  :lineno-match: