Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom partitioner to streaming tutorial #78

Merged
merged 4 commits into from
Mar 31, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README_Hadoop_Streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,40 @@ if __name__ == "__main__":
main()
```

## Custom partitioner
noah-weingarden marked this conversation as resolved.
Show resolved Hide resolved
If you need to specify which key-value pairs are sent to which reducers, you can create a custom partitioner. Here's a sample which works with our word count example.
```python
#!/usr/bin/env -S python3 -u
"""Word count partitioner."""
import sys


num_reducers = int(sys.argv[1])


for line in sys.stdin:
key, value = line.split("\t")
if key[0] <= "G":
print(0 % num_reducers)
else:
print(1 % num_reducers)
```

Each line of output from the mappers is streamed to this partition file, and the number of reducers is set to `sys.argv[1]`. For each line, the partitioner checks whether the first letter of the key is less than or greater than "G". If it's less than "G", the line is sent to the first reducer, and if it's greater, the line is sent to the second reducer.

Use the `-partitioner` command-line argument to tell Madoop to use this partitioner.

```console
$ madoop \
-input example/input \
-output example/output \
-mapper example/map.py \
-reducer example/reduce.py \
-partitioner example/partition.py
```

This feature works similarly to Hadoop's [`Partitioner` class](https://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapreduce/Partitioner.html). The main difference is that Hadoop only allows partitioners to be a Java class, while Madoop allows any executable that reads from `stdin` and writes to `stdout`.
noah-weingarden marked this conversation as resolved.
Show resolved Hide resolved

## Tips and tricks
These are some pro-tips for working with MapReduce programs written in Python for the Hadoop Streaming interface.

Expand Down
Loading