To implement custom writable class, a custom partitioner, and sorting the output using the sample data provided, follow the steps below:
- Create a new class named
PointCoordinate.java to represent the GPS coordinates (source_point and target_point):
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PointCoordinate implements Writable {
private double lat;
private double lon;
public PointCoordinate() {}
public PointCoordinate(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public void readFields(DataInput in) throws IOException {
lat = in.readDouble();
lon = in.readDouble();
}
public void write(DataOutput out) throws IOException {
out.writeDouble(lat);
out.writeDouble(lon);
}
public static PointCoordinate fromString(String value) {
Pattern pattern = Pattern.compile("POINT\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)");
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
return new PointCoordinate(Double.parseDouble(matcher.group(2)), Double.parseDouble(matcher.group(1)));
} else {
return null;
}
}
@Override
public String toString() {
return "(" + lat + "," + lon + ")";
}
}
- Create a custom writable class named
TaxiTrajectoryWritable.java:
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class TaxiTrajectoryWritable implements Writable {
private long taxiId;
private long trajectoryId;
private PointCoordinate sourcePoint;
private PointCoordinate targetPoint;
public TaxiTrajectoryWritable() {
sourcePoint = new PointCoordinate();
targetPoint = new PointCoordinate();
}
public void readFields(DataInput in) throws IOException {
taxiId = in.readLong();
trajectoryId = in.readLong();
sourcePoint.readFields(in);
targetPoint.readFields(in);
}
public void write(DataOutput out) throws IOException {
out.writeLong(taxiId);
out.writeLong(trajectoryId);
sourcePoint.write(out);
targetPoint.write(out);
}
public void set(long taxiId, long trajectoryId, PointCoordinate sourcePoint, PointCoordinate targetPoint) {
this.taxiId = taxiId;
this.trajectoryId = trajectoryId;
this.sourcePoint = sourcePoint;
this.targetPoint = targetPoint;
}
@Override
public String toString() {
return taxiId + "\t" + trajectoryId + "\t" + sourcePoint.toString() + "\t" + targetPoint.toString();
}
}
- Create a custom partitioner named
SourcePointPartitioner.java to partition the data by source_point (based on the latitude):
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class SourcePointPartitioner extends Partitioner<Text, TaxiTrajectoryWritable> {
@Override
public int getPartition(Text key, TaxiTrajectoryWritable value, int numPartitions) {
return Math.abs((int) (value.getSourcePoint().getLat() * 1000) % numPartitions);
}
}
- Update the TaxiMapper and TaxiReducer classes to work with the custom writable:
public static class TaxiMapper extends Mapper<LongWritable, Text, Text, TaxiTrajectoryWritable> {
private TaxiTrajectoryWritable taxiTrajectoryWritable = new TaxiTrajectoryWritable();
private Text sourcePoint = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split("\t");
if (fields.length >= 5) {
long taxiId = Long.parseLong(fields[0]);
long trajectoryId = Long.parseLong(fields[1]);
PointCoordinate sourceCoord = PointCoordinate.fromString(fields[2]);
PointCoordinate targetCoord = PointCoordinate.fromString(fields[3]);
if (sourceCoord != null && targetCoord != null) {
taxiTrajectoryWritable.set(taxiId, trajectoryId, sourceCoord, targetCoord);
sourcePoint.set(sourceCoord.toString());
context.write(sourcePoint, taxiTrajectoryWritable);
}
}
}
}
public static class TaxiReducer extends Reducer<Text, TaxiTrajectoryWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<TaxiTrajectoryWritable> values, Context context) {
int count = 0;
for (TaxiTrajectoryWritable value : values) {
count++;
}
context.write(key, new IntWritable(count));
}
}
- Update the main class to use the custom writable, partitioner, and sorting:
// ...
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(TaxiTrajectoryWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setPartitionerClass(SourcePointPartitioner.class);
// Add this line to sort the output by source point latitude
job.setSortComparatorClass(Text.Comparator.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// ...
After implementing the custom writable and partitioner, compile the project, create an executable jar file and run it on the Cloudera VM as before. The output will now be sorted by latitude and include the count for each source point.