class FirstPartitioner extends Partitioner<Text, MapWritable> {
@Override
public int getPartition(Text key, MapWritable value, int numPartitions) {
String []allKey = key.toString().split("\t");
if(allKey.length<2){
throw new RuntimeException("key's indicator does not exist.");
}
String url = allKey[0];
return Math.abs(url.hashCode()) % numPartitions;
}
}
class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text ip1 = (Text) w1;
Text ip2 = (Text) w2;
int cmp = ip1.toString().split("\t")[0].compareTo(ip2.toString().split("\t")[0]);
if (cmp != 0) {
return cmp;
}
return -ip1.toString().split("\t")[1].compareTo(ip2.toString().split("\t")[1]); //reverse
}
}
class GroupComparator extends WritableComparator {
protected GroupComparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text ip1 = (Text) w1;
Text ip2 = (Text) w2;
return ip1.toString().split("\t")[0].compareTo(ip2.toString().split("\t")[0]);
}
}
@Override
public int getPartition(Text key, MapWritable value, int numPartitions) {
String []allKey = key.toString().split("\t");
if(allKey.length<2){
throw new RuntimeException("key's indicator does not exist.");
}
String url = allKey[0];
return Math.abs(url.hashCode()) % numPartitions;
}
}
class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text ip1 = (Text) w1;
Text ip2 = (Text) w2;
int cmp = ip1.toString().split("\t")[0].compareTo(ip2.toString().split("\t")[0]);
if (cmp != 0) {
return cmp;
}
return -ip1.toString().split("\t")[1].compareTo(ip2.toString().split("\t")[1]); //reverse
}
}
class GroupComparator extends WritableComparator {
protected GroupComparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text ip1 = (Text) w1;
Text ip2 = (Text) w2;
return ip1.toString().split("\t")[0].compareTo(ip2.toString().split("\t")[0]);
}
}