How do I access DistributedCache in Hadoop Map/Reduce jobs?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Hadoop is an open-source framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. One of the key components of Hadoop is the Map/Reduce job model, which splits the input data into independent chunks processed by the map tasks in a completely parallel manner. The intermediate output is then processed by reduce tasks to produce the final result.
DistributedCache is a facility provided by the Apache Hadoop framework to cache files (text, archives, jars, etc.) needed by applications. Once you cache a file for your Hadoop job, Hadoop makes it available on each data node where your map/reduce tasks are running, providing a significant performance boost.
Why Use DistributedCache?
- Efficiency: Caching files on each node rather than each task fetching them from a central location saves bandwidth and reduces network congestion.
- Speed: Access to local files is significantly faster than pulling them over the network.
- Convenience: It abstracts file management from the user, simplifying the coding process.
How to Use DistributedCache in Map/Reduce Jobs
To leverage DistributedCache, you generally follow these steps:
Adding Files to the DistributedCache
- Identify the files you want to share across all nodes.
- Add these files to the cache at the time of job configuration:
Here, #alias is an optional way to provide an alias by which you will reference the file in the Map/Reduce code. If an alias is used, the file will appear in the task working directory with the given alias.
Accessing Cached Files in a Map/Reduce Program
- In the Mapper or Reducer setup method: Access the file through the local file system API. The file is located in the directory where the task runner is executed.
Practical Example:
Suppose your Map/Reduce job requires a lookup file called lookup.dat stored in HDFS, and you want to use this file in your map tasks. Below is how you would add it to your setup:
Summary Table
| Feature | Description |
| Utility | Provides a mechanism to cache files required by your Hadoop job across all nodes. |
| Performance Boost | Reduces network congestion and saves bandwidth by sharing files locally rather than over the network. |
| Ease of Use | Simplifies job configuration by abstracting complex file management tasks. |
| Integration Point | Implements through the Job configuration and accessible within the Map and Reduce tasks. |
| Accessibility | Cached files can be accessed as local files within the task runs. |
By efficiently using DistributedCache, developers can enhance the performance of their Hadoop jobs and manage data sharing in a more effective and controlled manner.
Related reading
- How do I add files to distributed cache in an oozie job
- How do I call prediction function in pyspark?
- How do I configure Tensorflow Serving to serve models from HDFS?
- How do I connect to a Kerberos-secured Kafka cluster with Spark Structured Streaming?
- How do I parallelize writing a list of Pyspark dataframes across all worker nodes?
- How does Cassandra Partitioning actually work?
- How does Consumer.endOffsets work in Kafka?
- How does HBase guarantee row level atomicity?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.