To implement secondary sort in Hadoop MapReduce, we'll use Java to create a MapReduce job that sorts temperature data based on time for each key. We will use 3 reducers and ensure that all records with the same key (e.g., 'z', 'p', 'x', 'y') are sent to the same reducer.
Here's the complete code along with the necessary commands to execute the job.
1. Mapper Class (SecondarySortMapper.java):
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class SecondarySortMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text keyOut = new Text();
private Text valueOut = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] tokens = line.split(",");
if (tokens.length == 3) {
String mainKey = tokens[0];
String time = tokens[1];
String val = tokens[2];
keyOut.set(mainKey);
valueOut.set(time + "," + val);
context.write(keyOut, valueOut);
}
}
}
2. Reducer Class (SecondarySortReducer.java):
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.*;
public class SecondarySortReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
// Collect all values for this key
List<String> valueList = new ArrayList<>();
for (Text val : values) {
valueList.add(val.toString());
}
// Sort the list by time
valueList.sort((s1, s2) -> {
String[] tokens1 = s1.split(",");
String[] tokens2 = s2.split(",");
int time1 = Integer.parseInt(tokens1[0]);
int time2 = Integer.parseInt(tokens2[0]);
return Integer.compare(time1, time2);
});
// Create output as {time1=value1, time2=value2, ...}
Map<Integer, Integer> timeValueMap = new LinkedHashMap<>();
for (String s : valueList) {
String[] tokens = s.split(",");
int time = Integer.parseInt(tokens[0]);
int value = Integer.parseInt(tokens[1]);
timeValueMap.put(time, value);
}
// Output result
context.write(key, new Text(timeValueMap.toString()));
}
}
3. Driver Class (SecondarySortDriver.java):
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class SecondarySortDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
if (args.length != 2) {
System.err.println("Usage: SecondarySortDriver <input path> <output path>");
System.exit(-1);
}
Job job = Job.getInstance(conf, "Secondary Sort");
job.setJarByClass(SecondarySortDriver.class);
job.setMapperClass(SecondarySortMapper.class);
job.setReducerClass(SecondarySortReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// Set number of reducers to 3
job.setNumReduceTasks(3);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
4. Commands to Compile and Execute the MapReduce Job:
First, make sure you have Hadoop set up and the hadoop command is available.
Compile the Java code and create a JAR file:
# Compile Mapper class
javac -classpath $(hadoop classpath) -d . SecondarySortMapper.java
# Compile Reducer class
javac -classpath $(hadoop classpath) -d . SecondarySortReducer.java
# Compile Driver class
javac -classpath $(hadoop classpath) -d . SecondarySortDriver.java
# Create JAR file
jar -cvf secondarysort.jar *.class
Prepare the input data and upload it to HDFS:
Assuming your input data is in a file named time_series.txt and it's located in your current directory.
# Create input directory in HDFS
hdfs dfs -mkdir -p /user/your_username/input
# Upload the input file to HDFS
hdfs dfs -put time_series.txt /user/your_username/input/
Run the MapReduce job:
hadoop jar secondarysort.jar SecondarySortDriver /user/your_username/input/time_series.txt /user/your_username/output/
Replace your_username with your actual Hadoop username.
View the output:
hdfs dfs -cat /user/your_username/output/part-*
Expected Output:
The output should be similar to:
p {1=10, 3=60, 4=40, 6=20}
x {1=3, 2=9, 3=6}
y {1=7, 2=5, 3=1}
z {1=4, 2=8, 3=7, 4=0}
Explanation:
-
The Mapper reads each line of the input file, splits it into key, time, and value, and emits the key as the output key and a concatenation of time and value as the output value.
-
The Reducer receives all values for a given key (e.g., all records for 'z'), collects them into a list, sorts them by time, and then constructs a map of time to value in the sorted order.
-
By using 3 reducers and ensuring that all records with the same key go to the same reducer, we meet the requirement that all records for a given key are processed together.
Note:
-
Since we are sorting within the reducer, this approach is suitable for smaller datasets. For larger datasets, you may need to implement a custom partitioner and grouping comparator to leverage Hadoop's shuffle and sort mechanisms more effectively.
-
The provided code is a complete solution that can be compiled and run as-is, given that your Hadoop setup is correctly configured.
This solution provides the full code to implement secondary sort in Hadoop using Java, along with commands to execute it. It reads input from a file and writes the output to a file, handling the data as specified in your example.