2017 © Pedro PelĂĄez
 

library php-mapreduce

A local implementation of the map/reduce strategy in PHP

image

jotaelesalinas/php-mapreduce

A local implementation of the map/reduce strategy in PHP

  • Sunday, October 29, 2017
  • by jotaelesalinas
  • Repository
  • 2 Watchers
  • 4 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

php-mapreduce

Latest Version on Packagist ![Software License][ico-license] Build Status ![Total Downloads][ico-downloads], (*1)

PHP PSR-4 compliant library to easily do non-distributed local map-reduce., (*2)

Install

Via Composer, (*3)

``` bash $ composer require jotaelesalinas/php-mapreduce, (*4)


## Basic usage ```php require_once __DIR__ . '/vendor/autoload.php'; $source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $mapper = fn($item) => $item * 2; $reducer = fn($carry, $item) => ($carry ?? 0) + $item; $result = MapReduce\MapReduce::create() ->setInput($source) ->setMapper($mapper) ->setReducer($reducer) ->run(); print_r($result);

The output is:, (*5)

Array
(
    [0] => 110
)

Filters

$odd_numbers = fn($item) => $item % 2 === 0;
$greater_than_10 = fn($item) => $item > 10;

$result = MapReduce\MapReduce::create([
        "input" => $source, 
        "mapper" => $mapper, 
        "reducer" => $reducer, 
    ])
    // only odd numbers are passed to the mapper function
    ->setPreFilter($odd_numbers)
    // only numbers greater than 10 are passed to the reducer function
    ->setPostFilter($greater_than_10)
    ->run();

print_r($result);

The output is:, (*6)

Array
(
    [0] => 48
)

Groups

Group by the value of a field (valid for arrays and objects):, (*7)

$source = [
    [ "first_name" => "Susanna", "last_name" => "Connor",  "member" => "y", "age" => 20],
    [ "first_name" => "Adrian",  "last_name" => "Smith",   "member" => "n", "age" => 22],
    [ "first_name" => "Mike",    "last_name" => "Mendoza", "member" => "n", "age" => 24],
    [ "first_name" => "Linda",   "last_name" => "Duguin",  "member" => "y", "age" => 26],
    [ "first_name" => "Bob",     "last_name" => "Svenson", "member" => "n", "age" => 28],
    [ "first_name" => "Nancy",   "last_name" => "Potier",  "member" => "y", "age" => 30],
    [ "first_name" => "Pete",    "last_name" => "Adams",   "member" => "n", "age" => 32],
    [ "first_name" => "Susana",  "last_name" => "Zommers", "member" => "y", "age" => 34],
    [ "first_name" => "Adrian",  "last_name" => "Deville", "member" => "n", "age" => 36],
    [ "first_name" => "Mike",    "last_name" => "Cole",    "member" => "n", "age" => 38],
    [ "first_name" => "Mike",    "last_name" => "Angus",   "member" => "n", "age" => 40],
];

// mapper does nothing
$mapper = fn($x) => $x;

// number of persons and sum of ages
$reduceAgeSum = function ($carry, $item) {
    if (is_null($carry)) {
        return [
            'count' => 1,
            'age_sum' => $item['age'],
        ];
    }

    $count = $carry['count'] + 1;
    $age_sum = $carry['age_sum'] + $item['age'];

    return compact('count', 'age_sum');
};

$result = MapReduce\MapReduce::create([
        "input" => $source, 
        "mapper" => $mapper, 
        "reducer" => $reduceAgeSum, 
    ])
    // group by field 'member'
    ->setGroupBy('member')
    ->run();

print_r($result);

The output is:, (*8)

Array
(
    [y] => Array
        (
            [count] => 4
            [age_sum] => 110
        )

    [n] => Array
        (
            [count] => 7
            [age_sum] => 220
        )

)

Group by a custom value generated from each item:, (*9)

$closestTen = fn($x) => floor($x['age'] / 10) * 10;

$result = MapReduce\MapReduce::create([
        "input" => $source, 
        "mapper" => $mapper, 
        "reducer" => $reduceAgeSum, 
    ])
    // group by age ranges of 10
    ->setGroupBy($closestTen)
    ->run();

print_r($result);

The output is:, (*10)

Array
(
    [20] => Array
        (
            [count] => 5
            [age_sum] => 120
        )

    [30] => Array
        (
            [count] => 5
            [age_sum] => 170
        )

    [40] => Array
        (
            [count] => 1
            [age_sum] => 40
        )

)

Input

MapReduce accepts as input any data of type iterable. That means, arrays and traversables, e.g. generators., (*11)

This is very handy when reading from big files that do not fit in memory., (*12)

$result = MapReduce\MapReduce::create([
        "mapper" => $mapper, 
        "reducer" => $reducer, 
    ])
    ->setInput(csvReadGenerator('myfile.csv'))
    ->run();

Multiple inputs can be specified, passing several arguments to setInput(), as long as all of them are iterable:, (*13)

$result = MapReduce\MapReduce::create([
        "mapper" => $mapper, 
        "reducer" => $reducer, 
    ])
    ->setInput($arrayData, csvReadGenerator('myfile.csv'))
    ->run();

Output

MapReduce can be configured to write the final data to one or more destinations., (*14)

Each destination has to be a Generator:, (*15)

$result = MapReduce\MapReduce::create([
        "mapper" => $mapper, 
        "reducer" => $reducer, 
    ])
    ->setOutput(csvWriteGenerator('results.csv'))
    ->run();

Multiple outputs can be specified as well:, (*16)

$result = MapReduce\MapReduce::create([
        "mapper" => $mapper, 
        "reducer" => $reducer, 
    ])
    ->setOutput(csvWriteGenerator('results.csv'), consoleGenerator())
    ->run();

To help working with input and output generators, it is recommended to use the package jotaelesalinas/php-generators, but it is not mandatory., (*17)

You can see more elaborated examples under the folder examples., (*18)

Change log

Please see CHANGELOG for more information what has changed recently., (*19)

Testing

bash $ composer test, (*20)

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*21)

Security

If you discover any security related issues, please DM me to @jotaelesalinas instead of using the issue tracker., (*22)

To do

  • [ ] Add events to help see progress in large batches
  • [ ] Add docs
  • [ ] Insurance example
    • [ ] adapt to new library
    • [ ] add insured values
    • [ ] improve kml output (info, markers)

Credits

License

The MIT License (MIT). Please see License File for more information., (*23)

The Versions

29/10 2017

dev-master

9999999-dev https://github.com/jotaelesalinas/php-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-mapreduce

29/10 2017

v1.0.5

1.0.5.0 https://github.com/jotaelesalinas/php-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-mapreduce

30/09 2017

v1.0.4

1.0.4.0 https://github.com/jotaelesalinas/php-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-mapreduce

31/08 2016

v1.0.2

1.0.2.0 https://github.com/jotaelesalinas/php-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-mapreduce

30/08 2016

v1.0.1

1.0.1.0 https://github.com/jotaelesalinas/php-local-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map local mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-local-mapreduce

29/08 2016

v1.0.0

1.0.0.0 https://github.com/jotaelesalinas/php-local-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map local mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-local-mapreduce

28/08 2016

v0.2.1

0.2.1.0 https://github.com/jotaelesalinas/php-local-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map local mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-local-mapreduce

26/08 2016

v0.2.0

0.2.0.0 https://github.com/jotaelesalinas/php-local-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map local mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-local-mapreduce

21/08 2016

v0.1.0

0.1.0.0 https://github.com/jotaelesalinas/php-local-mapreduce

A local implementation of the map/reduce strategy in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

php map local mapreduce reduce jotaelesalinas jlsalinas map-reduce non-distribted php-local-mapreduce