HAR file
DistributedCache
mapreduce
data processing
Hadoop

Reading HAR file from DistributedCache in mapreduce

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

MapReduce, an integral component of Apache Hadoop, allows for distributed processing of large data sets across clusters of computers. One of the common tasks in MapReduce jobs is to share some read-only data across all nodes in a cluster to avoid the redundancy of storing multiple copies of data locally on every node. This is effectively managed by Hadoop’s DistributedCache, which can significantly optimize the performance and resource management of a MapReduce job.

The DistributedCache is a facility provided by the Hadoop framework that lets you cache files (text, archives, jars, etc.) needed by applications. Once you cache a file for your job, Hadoop framework will make it available on each and every DataNode where your Map/Reduce tasks are running, thereby reducing the latency or network calls to fetch data from a central server.

How DistributedCache Works with .HAR Files

Hadoop Archives, known as HAR files, are special types of compressed archive files that improve the efficiency of file storage by combining smaller files into a single file, which reduces the nameNode's memory overhead, as it has to keep track of fewer files. In essence, .HAR files can be seen as a simple filesystem layered on top of HDFS.

Working with HAR files in DistributedCache can be beneficial in scenarios where you have to use a large number of small files across all tasks of a MapReduce job. Instead of transferring every small file separately, you can package them into a HAR file and add it to the DistributedCache.

Reading a HAR file in MapReduce Job

To leverage HAR files within a MapReduce job, you must first create a HAR archive and then add it to the DistributedCache. Below is a detailed explanation and an example of how to achieve this:

  1. Creating a Hadoop Archive: Use the command below to create a HAR file:
bash
   hadoop archive -archiveName name.har -p /parent/directory input_directory har_output_directory
  1. Adding HAR file to DistributedCache: Before running the job, you add the HAR file to DistributedCache:
java
   job.addCacheArchive(new URI("/path/to/har/file.har#alias"));
  1. Using Files in HAR in MapReduce: You can access the files stored in the HAR within your map and reduce tasks as normal files:
java
   Path cacheFiles = DistributedCache.getLocalCacheArchives(conf);
   Path myFile = new Path("alias/path/inside/har/file.txt");
   BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(myFile.toString()))));
  1. Example Code Snippet: Here’s how part of your Mapper class might read an input file from a HAR archive:
java
1   public class CacheFileMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
2       
3       public void setup(Context context) throws IOException {
4           Path[] cacheFiles = DistributedCache.getLocalCacheArchives(context.getConfiguration());
5           FileSystem fs = FileSystem.get(context.getConfiguration());
6           FSDataInputStream in = fs.open(new Path("alias/path/inside/har/file.txt"));
7           // Continue with reading and processing the file
8       }
9   }

Benefits of Using DistributedCache with HAR files

The use of HAR files and DistributedCache in MapReduce jobs leads to a number of performance and efficiency benefits:

BenefitDescription
Reduced Load on NameNodeStoring fewer metadata entries for many small files in archives.
Network EfficiencyReduces the amount of data transported across the network as files are locally cached.
Improved Job PerformanceFaster job execution due to local access to required files.

Conclusion

Integrating HAR files with DistributedCache is a powerful technique for optimizing MapReduce jobs, especially ones that require access to a significant number of small files. It enhances job performance, minimizes network congestion, and manages HDFS more effectively. By archiving files and using DistributedCache, Hadoop users can ensure that their MapReduce tasks are optimal in terms of both speed and resource utilization.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.