Working with MapReduce jobs
MapReduce
In this article, we use a simple example to show how MapReduce works in Yandex Data Processing. We will use MapReduce to compute the population of the world's 500 largest cities based on the cities dataset.
To run MapReduce on Hadoop, we use the Streaming interface. At the same time, the data preprocessing (map) and the final output computation (reduce) stages use programs that read data from a standard program input (stdin) and write their result to a standard output (stdout).
To execute a MapReduce job:
If you no longer need the resources you created, delete them.
Getting started
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can create or select a folder for your infrastructure on the cloud page
Learn more about clouds and folders here.
Required paid resources
- Yandex Data Processing cluster: use of computing resources with a Yandex Data Processing markup, use of network drives, retrieval and storage of logs, amount of outgoing traffic (see Yandex Data Processing pricing).
- Public IP addresses if public access is enabled for cluster hosts (see Yandex Virtual Private Cloud pricing).
- Yandex Object Storage buckets: use of storage, data operations (see Object Storage pricing).
Set up your infrastructure
-
Create a service account with the
dataproc.agentanddataproc.provisionerroles. -
In Object Storage, create buckets and configure access to them:
- Create a bucket for the input data and grant the
READpermission for this bucket to the cluster service account. - Create a bucket for the processing output and grant the cluster service account
READ and WRITEpermissions for this bucket.
- Create a bucket for the input data and grant the
-
Create a Yandex Data Processing cluster with the following settings:
- Environment:
PRODUCTION. - Services:
HDFSMAPREDUCEYARN
- Service account: Select the service account you created earlier.
- Bucket name: Select a bucket for the processing results.
- Environment:
Create a MapReduce job
-
Download
an archived CSV file with the cities dataset and upload it to the input data bucket. -
Upload Python files to the input data bucket:
mapper.py, which contains the code for data preprocessing (map stage), andreducer.py, which contains the code for the final computations (reduce stage):mapper.py#!/usr/bin/python import sys population = sum(int(line.split('\t')[14]) for line in sys.stdin) print(population)reducer.py#!/usr/bin/python import sys population = sum(int(value) for value in sys.stdin) print(population) -
Create a MapReduce job with the following parameters:
- Main class:
org.apache.hadoop.streaming.HadoopStreaming - Arguments:
-mappermapper.py-reducerreducer.py-numReduceTasks1-inputs3a://<input_data_bucket_name>/cities500.txt-outputs3a://<output_bucket_name>/<output_directory>
- Files:
s3a://<input_data_bucket_name>/mapper.pys3a://<input_data_bucket_name>/reducer.py
- Properties:
mapreduce.job.maps: 6yarn.app.mapreduce.am.resource.mb: 2048yarn.app.mapreduce.am.command-opts: -Xmx2048m
- Main class:
-
Wait for the job status to change to
Done. -
Download the file with the result from the bucket and review it:
part-000003157107417
Note
You can view the job logs and search data in them using Yandex Cloud Logging. For more information, see Working with logs.
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need: