Code Monkey home page Code Monkey logo

map-reduce's Introduction

map-reduce

A simple MapReduce platform using Perl 5 and Redis

Example

This example will calculate the number of characters in each word of the sentence and then drop any that have less than 3 characters.

#!/usr/bin/env perl
use strict;
use warnings;
use Redis::hiredis;
use MapReduce;

# This is necessary at the moment until we can improve redis integration
my $redis = Redis::hiredis->new();

$redis->connect('127.0.0.1', 6379);
$redis->select(9);
$redis->flushdb();

# We have to save references to the mappers and reducers to avoid
# them getting garbage collected immediately.
my @workers;

# Fork one mapper (you can use several)
for (1..1) {
    push @workers, MapReduce::Mapper->new(
        daemon => 1,
    );
}

# Fork one reducer (currently only one is useful)
push @workers, MapReduce::Reducer->new(
    daemon => 1,
);

my $mr = MapReduce->new(
    name    => 'count-chars',
    mapper  => \&mapper,
    reducer => \&reducer,
);

my $text = 'MapReduce is a programming model for processing large data sets in parallel.';

my @words = split qr{\s+}, $text;

for my $index (0 .. $#words) {
    $mr->input({
        key  => $index,
        word => $words[$index],
    });
}

my $results = $mr->all_results;

for my $result (@$results) {
    printf STDERR "Character count for word '%s': %s\n",
        $result->{word}, $result->{count};
}

sub mapper {
    my ($mapper, $input) = @_;
    
    $input->{count} = length $input->{word};
    
    return $input;
}

sub reducer {
    my ($reducer, $inputs) = @_;
    
    # Keep only inputs with a count > 3
    return [ grep { $_->{count} > 3 } @$inputs ];
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.