Skip to content

Commit

Permalink
Added more examples (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Feb 4, 2024
1 parent 720ac6a commit 7cd391c
Show file tree
Hide file tree
Showing 22 changed files with 480 additions and 6 deletions.
33 changes: 33 additions & 0 deletions examples/topics/aggregations/average/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\average;
use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\from_rows;
use function Flow\ETL\DSL\int_entry;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\rows;
use function Flow\ETL\DSL\to_output;

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

df()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
row(int_entry('a', 200)),
row(int_entry('a', 400)),
row(int_entry('a', 400))
)))
->aggregate(average(ref('a')))
->write(to_output(false))
->run();

// +-------+
// | a_avg |
// +-------+
// | 240 |
// +-------+
// 1 rows
33 changes: 33 additions & 0 deletions examples/topics/aggregations/first/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\first;
use function Flow\ETL\DSL\from_rows;
use function Flow\ETL\DSL\int_entry;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\rows;
use function Flow\ETL\DSL\to_output;

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

df()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
row(int_entry('a', 200)),
row(int_entry('a', 400)),
row(int_entry('a', 400))
)))
->aggregate(first(ref('a')))
->write(to_output(false))
->run();

// +-----+
// | a |
// +-----+
// | 100 |
// +-----+
// 1 rows
8 changes: 8 additions & 0 deletions examples/topics/aggregations/group_by/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
->groupBy(ref('group'))
->write(to_output(truncate: false))
->run();

// +-------+
// | group |
// +-------+
// | A |
// | B |
// +-------+
// 2 rows
37 changes: 37 additions & 0 deletions examples/topics/aggregations/group_by_sum/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\from_array;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\sum;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_array([
['id' => 1, 'group' => 'A', 'value' => 100],
['id' => 2, 'group' => 'B', 'value' => 200],
['id' => 3, 'group' => 'A', 'value' => 300],
['id' => 4, 'group' => 'B', 'value' => 100],
['id' => 5, 'group' => 'A', 'value' => 200],
['id' => 6, 'group' => 'B', 'value' => 100],
['id' => 7, 'group' => 'A', 'value' => 400],
['id' => 8, 'group' => 'B', 'value' => 20],
['id' => 9, 'group' => 'A', 'value' => 800],
['id' => 10, 'group' => 'B', 'value' => 40],
]))
->groupBy(ref('group'))
->aggregate(sum(ref('value')))
->write(to_output(truncate: false))
->run();

// +-------+-----------+
// | group | value_sum |
// +-------+-----------+
// | A | 1800 |
// | B | 460 |
// +-------+-----------+
// 2 rows
33 changes: 33 additions & 0 deletions examples/topics/aggregations/last/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\from_rows;
use function Flow\ETL\DSL\int_entry;
use function Flow\ETL\DSL\last;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\rows;
use function Flow\ETL\DSL\to_output;

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

df()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
row(int_entry('a', 200)),
row(int_entry('a', 400)),
row(int_entry('a', 400))
)))
->aggregate(last(ref('a')))
->write(to_output(false))
->run();

// +-----+
// | a |
// +-----+
// | 400 |
// +-----+
// 1 rows
33 changes: 33 additions & 0 deletions examples/topics/aggregations/max/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\from_rows;
use function Flow\ETL\DSL\int_entry;
use function Flow\ETL\DSL\max;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\rows;
use function Flow\ETL\DSL\to_output;

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

df()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
row(int_entry('a', 200)),
row(int_entry('a', 400)),
row(int_entry('a', 400))
)))
->aggregate(max(ref('a')))
->write(to_output(false))
->run();

// +-------+
// | a_max |
// +-------+
// | 400 |
// +-------+
// 1 rows
33 changes: 33 additions & 0 deletions examples/topics/aggregations/min/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\from_rows;
use function Flow\ETL\DSL\int_entry;
use function Flow\ETL\DSL\min;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\rows;
use function Flow\ETL\DSL\to_output;

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

df()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
row(int_entry('a', 200)),
row(int_entry('a', 400)),
row(int_entry('a', 400))
)))
->aggregate(min(ref('a')))
->write(to_output(false))
->run();

// +-------+
// | a_min |
// +-------+
// | 100 |
// +-------+
// 1 rows
20 changes: 20 additions & 0 deletions examples/topics/data_source/array/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\from_array;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_array([
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
['id' => 5],
]))
->write(to_output(false))
->run();
22 changes: 22 additions & 0 deletions examples/topics/data_source/csv/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\CSV\from_csv;
use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_csv(
__DIR__ . '/input/dataset.csv',
with_header: true,
empty_to_null: true,
delimiter: ',',
enclosure: '"',
escape: '\\',
characters_read_in_line: 1000
))
->write(to_output(false))
->run();
5 changes: 5 additions & 0 deletions examples/topics/data_source/csv/input/dataset.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name,email,active
1,John,[email protected],true
2,Paul,[email protected],true
3,George,[email protected],false
4,Ringo,[email protected],true
30 changes: 30 additions & 0 deletions examples/topics/data_source/data_frame/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\from_array;
use function Flow\ETL\DSL\from_data_frame;
use function Flow\ETL\DSL\lit;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(
from_data_frame(
data_frame()
->read(from_array(
[
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
['id' => 5],
]
))
->withEntry('timestamp', lit(\time()))
)
)
->write(to_output(false))
->run();
16 changes: 16 additions & 0 deletions examples/topics/data_source/json/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\JSON\from_json;
use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_json(
__DIR__ . '/input/dataset.json',
))
->write(to_output(false))
->run();
26 changes: 26 additions & 0 deletions examples/topics/data_source/json/output/dataset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"id": "1",
"name": "John",
"email": "[email protected]",
"active": "true"
},
{
"id": "2",
"name": "Paul",
"email": "[email protected]",
"active": "true"
},
{
"id": "3",
"name": "George",
"email": "[email protected]",
"active": "false"
},
{
"id": "4",
"name": "Ringo",
"email": "[email protected]",
"active": "true"
}
]
16 changes: 16 additions & 0 deletions examples/topics/data_source/parquet/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\Parquet\from_parquet;
use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_parquet(
__DIR__ . '/input/dataset.parquet',
))
->write(to_output(false))
->run();
Binary file not shown.
19 changes: 19 additions & 0 deletions examples/topics/data_source/sequence_date/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\data_frame;
use function Flow\ETL\DSL\from_sequence_date_period;
use function Flow\ETL\DSL\to_output;

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

data_frame()
->read(from_sequence_date_period(
'date',
new DateTimeImmutable('now'),
new DateInterval('P1D'),
new DateTimeImmutable('now + 60 days'),
))
->write(to_output(false))
->run();
Loading

0 comments on commit 7cd391c

Please sign in to comment.