A simple simulator of a system which implements map/reduce paradigm.
Project description
MREDU
A simple simulator of a system which implements map/reduce paradigm similarly to how Apache Hadoop does. Its objective is to be used as an educational tool to learn how to code map/reduce algorithms without needing to install complex components.
Requirements
To use it you will need:
-
A Python 3.8+ interpreter.
-
Install required packages from PyPi:
$> pip install mredu
Examples
There are several examples of use of the simulator in the examples directory.
- example1: Does some calculations from a list of tuples.
- example2: Calculates the histogram of the number of words per line in the file quijote.txt from data folder.
- example3: The ubiquitous word-count example written to run on the simulator and applied to the same quijote.txt file.
- example4: Inverse k,v -> v,k agrupation
Development
To contribute to this project, you will need to set up a development environment.
-
Clone the repository and create a virtual environment:
git clone https://github.com/ramonpin/mredu.git cd mredu python3 -m venv .venv source .venv/bin/activate
-
Install dependencies:
Install the required dependencies, including the ones for development and testing:
pip install -r requirements.txt
-
Install the package in editable mode:
This will allow you to import the package in your tests and run it as if it were installed, but your local changes will be reflected immediately.
pip install -e .
-
Run the tests:
To make sure everything is working correctly, run the test suite:
pytest
Docs
mredu simulates a MapReduce environment. The process is as follows:
- Input: You start with an input sequence of
(key, value)pairs.mreduprovides helper functions to read data from files into this format. - Map: A
mapperfunction is applied to each(key, value)pair, producing a new sequence of(key, value)pairs. - Shuffle & Sort: The framework automatically groups the pairs from the map phase by key.
- Reduce: A
reducerfunction is applied to each key and its list of associated values, producing the final result.
Core Functions
input_file(path): Reads a text file line by line, producing a sequence of(line_number, line_content)pairs.input_kv_file(path, sep): Reads a text file line by line, splitting each line bysepto produce(key, value)pairs.map_red(input_sequence, mapper, reducer): Chains together the map, shuffle/sort, and reduce steps. It takes an input sequence and the mapper and reducer functions as arguments.run(map_red_process): Executes the full MapReduce process and prints the resulting(key, value)pairs to the console, separated by a tab.
Example: Word Count
Here is how you would implement the classic word count example using mredu.
First, you define your mapper function. It takes a key and a value as input (in this case, line number and line text). It splits the line into words, and for each word, it returns a (word, 1) pair.
import re
def mymap(_, v):
words = list(filter(lambda s: s != '', re.split(r'\W', v)))
return [(word.lower(), 1) for word in words]
Next, you define your reducer function. It takes a key (a word) and a list of values (a list of 1s) and returns a pair with the word and the sum of the values.
def myred(k, vs):
return k, len(vs)
Finally, you tie it all together. You create an input source from a file, pass it to map_red with your mapper and reducer, and then use run to execute the process.
from mredu.simul import map_red, input_file, run
# assuming mymap and myred are defined as above
if __name__ == '__main__':
process = map_red(input_file('data/quijote.txt'), mymap, myred)
run(process)
This will output the word counts to the console.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mredu-1.0.1.tar.gz.
File metadata
- Download URL: mredu-1.0.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
649fec01650eca7f505e29c9a66dd453d9fea78e48d92f2aaf4cf83a0d28f1dc
|
|
| MD5 |
dbcdadb6e7c66cfd7c53571879ef6d99
|
|
| BLAKE2b-256 |
7aa93a868eb6781fcaa5c99996caa6ac0eb2995f799a5e0932d84ff90e4e78bc
|
File details
Details for the file mredu-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mredu-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db690d7fceb0895e6bcadff02a1d9c57b6691e87d99a12e3cafdb29360cd1060
|
|
| MD5 |
679e7d3b87d960c97e9724837cccbec0
|
|
| BLAKE2b-256 |
3234744c86a0a4d00dde4b9225b0f85859e4d08a7ca05ee7acfda7485a24f4a8
|