flow php

Example: Jsonl

Topic: Data reading

Description

Read data from a json lines formatted file

function from_json_lines(string|Path $path);

Additional options:

  • withPointer(string $pointer) - default null, used to iterate only results of a subtree, read more about pointers
  • withSchema(Schema $schema) - the schema of the dataset, when not set, it will be auto-detected
composer.json
{
    "name": "flow-php/examples",
    "description": "Flow PHP - Examples",
    "license": "MIT",
    "type": "library",
    "require": {
        "flow-php/etl": "1.x-dev",
        "flow-php/etl-adapter-json": "1.x-dev"
    }
}
code.php
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\JSON\from_json_lines;
use function Flow\ETL\DSL\{bool_schema, data_frame, int_schema, schema, str_schema, to_stream};

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

$schema = schema(
    int_schema('id'),
    str_schema('name'),
    str_schema('email'),
    bool_schema('active'),
);

data_frame()
    ->read(
        from_json_lines(__DIR__ . '/input/dataset.jsonl')
            ->withSchema($schema)
    )
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+--------+------------------+--------+
| id |   name |            email | active |
+----+--------+------------------+--------+
|  1 |   John |   [email protected] |   true |
|  2 |   Paul |   [email protected] |   true |
|  3 | George | [email protected] |  false |
|  4 |  Ringo |   [email protected] |   true |
+----+--------+------------------+--------+
4 rows

Contributors

Join us on GitHub external resource
scroll back to top