CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.3 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.3
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
      • Iterator
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class MapReduce

Implements a simplistic version of the popular Map-Reduce algorithm. Acts like an iterator for the original passed data after each result has been processed, thus offering a transparent wrapper for results coming from any source.

Namespace: Cake\Collection\Iterator

Property Summary

  • $_counter protected
    int

    Count of elements emitted during the Reduce phase

  • $_data protected
    iterable

    Holds the original data that needs to be processed

  • $_executed protected
    bool

    Whether the Map-Reduce routine has been executed already on the data

  • $_intermediate protected
    array

    Holds the shuffled results emitted from the map phase

  • $_mapper protected
    callable

    A callable that will be executed for each record in the original data

  • $_reducer protected
    callable|null

    A callable that will be executed for each intermediate record emitted during the Map phase

  • $_result protected
    array

    Holds the results as emitted during the reduce phase

Method Summary

  • __construct() public

    Constructor

  • _execute() protected

    Runs the actual Map-Reduce algorithm. This iterates the original data and calls the mapper function for each record, then for each intermediate bucket created during the Map phase call the reduce function.

  • emit() public

    Appends a new record to the final list of results and optionally assign a key for this record.

  • emitIntermediate() public

    Appends a new record to the bucket labeled with $key, usually as a result of mapping a single record from the original data.

  • getIterator() public

    Returns an iterator with the end result of running the Map and Reduce phases on the original data

Method Detail

__construct() ¶ public

__construct(iterable $data, callable $mapper, callable|null $reducer = null)

Constructor

Example:

Separate all unique odd and even numbers in an array

 $data = new \ArrayObject([1, 2, 3, 4, 5, 3]);
 $mapper = function ($value, $key, $mr) {
     $type = ($value % 2 === 0) ? 'even' : 'odd';
     $mr->emitIntermediate($value, $type);
 };

$reducer = function ($numbers, $type, $mr) {
     $mr->emit(array_unique($numbers), $type);
 };
 $results = new MapReduce($data, $mapper, $reducer);

Previous example will generate the following result:

 ['odd' => [1, 3, 5], 'even' => [2, 4]]
Parameters
iterable $data

The original data to be processed.

callable $mapper

the mapper callback. This function will receive 3 arguments. The first one is the current value, second the current results key and third is this class instance so you can call the result emitters.

callable|null $reducer optional

the reducer callback. This function will receive 3 arguments. The first one is the list of values inside a bucket, second one is the name of the bucket that was created during the mapping phase and third one is an instance of this class.

_execute() ¶ protected

_execute(): void

Runs the actual Map-Reduce algorithm. This iterates the original data and calls the mapper function for each record, then for each intermediate bucket created during the Map phase call the reduce function.

Returns
void
Throws
LogicException
if emitIntermediate was called but no reducer function was provided

emit() ¶ public

emit(mixed $val, mixed $key = null): void

Appends a new record to the final list of results and optionally assign a key for this record.

Parameters
mixed $val

The value to be appended to the final list of results

mixed $key optional

An optional key to assign to the value

Returns
void

emitIntermediate() ¶ public

emitIntermediate(mixed $val, mixed $bucket, mixed $key = null): void

Appends a new record to the bucket labeled with $key, usually as a result of mapping a single record from the original data.

Parameters
mixed $val

The record itself to store in the bucket

mixed $bucket

the name of the bucket where to put the record

mixed $key optional

An optional key to assign to the value

Returns
void

getIterator() ¶ public

getIterator(): Traversable

Returns an iterator with the end result of running the Map and Reduce phases on the original data

Returns
Traversable

Property Detail

$_counter ¶ protected

Count of elements emitted during the Reduce phase

Type
int

$_data ¶ protected

Holds the original data that needs to be processed

Type
iterable

$_executed ¶ protected

Whether the Map-Reduce routine has been executed already on the data

Type
bool

$_intermediate ¶ protected

Holds the shuffled results emitted from the map phase

Type
array

$_mapper ¶ protected

A callable that will be executed for each record in the original data

Type
callable

$_reducer ¶ protected

A callable that will be executed for each intermediate record emitted during the Map phase

Type
callable|null

$_result ¶ protected

Holds the results as emitted during the reduce phase

Type
array
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs