flow php

Example: Collect

Topic: Data frame


Description

Collect is used to make sure that all rows are processed at once. This means that all rows are loaded into memory and processed at once. It's useful mostly for debugging and while working with relatively small datasets. In order to control memory consumption please use batchSize.

Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, to_stream};

require __DIR__ . '/../../../autoload.php';

data_frame()
    ->read(from_array([
        ['id' => 1, 'name' => 'John'],
        ['id' => 2, 'name' => 'Doe'],
        ['id' => 3, 'name' => 'Jane'],
        ['id' => 4, 'name' => 'Smith'],
        ['id' => 5, 'name' => 'Alice'],
    ]))
    ->collect() // alternatively we can also use ->batchSize(-1)
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+-------+
| id |  name |
+----+-------+
|  1 |  John |
|  2 |   Doe |
|  3 |  Jane |
|  4 | Smith |
|  5 | Alice |
+----+-------+
5 rows

Contributors

Join us on GitHub external resource
scroll back to top