I'm creating an application to process video in a distributed way in hadoop using the map-reduce paradigm. I have several videos stored in the cluster and I wish from them to create a single video file.
To do this, I'm extracting the frames of each video in the map phase and sending them to the phase stage. Each video is numbered from 1 to 9, and the beginning of the key of each frame corresponds to the video that originated it, for example the key 11788847 corresponds to a frame of the video 1, on the other hand the key 57845 corresponds to the video of number 5. Note that even the 11788847 key being larger than the 57845 key, it must come before the frames are grouped according to the video.
For this, I've implemented the SortingCustomComparator
class that inherits from WiritableComparator
. Here is the code below:
public class SortingCustomComparator extends WritableComparator{
protected SortingCustomComparator(){
super(Text.class, true);
}
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2){
// Converte a string em um inteiro
String key1 = null;
String key2 = null;
try {
key1 = Text.decode(b1, s1, l1);
key2 = Text.decode(b2, s2, l2);
} catch (CharacterCodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int keyInt = Integer.valueOf(key1.trim());
int key2Int = Integer.valueOf(key2.trim());
return (keyInt >key2Int) ? 1 : -1;
} }
The problem is that the above code does not have the expected behavior. The created video contains some frames that are placed in a random position. I would like the keys to be grouped by blocks. The keys to the first video must be sorted in ascending order and must belong to the set of the first video as well as the second belonging to the second and so on. Example: {11, 12, 13, 14, ..., 111, 112}, {2, 21, 22, ...}. At the end these key blocks should be ordered {11, 12, 13, 14, ..., 2, 21, 22}. So the frames will be grouped in the correct order, the video will not be buggy. Would anyone know how to implement this correctly?
Thank you in advance.