Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public class RoundRobinTableInputFormat extends TableInputFormat {
@Override
public List<InputSplit> getSplits(JobContext context) throws IOException {
List<InputSplit> inputSplits = super.getSplits(context); // Get splits from HBase TableInputFormat gives
List<InputSplit> roundRobinInputSplits = new ArrayList<InputSplit>();
Map<String, List<InputSplit>> regionServersInputSplits = new HashMap<String, List<InputSplit>>();
// Prepare a hashmap with each region server as key and list of Input Splits as value
if (inputSplits != null && inputSplits.size() != 0) {
for (InputSplit inputSplit : inputSplits) {
if (inputSplit instanceof TableSplit) {
String regionServer = ((TableSplit) inputSplit).getRegionLocation();
if (!Strings.isNullOrEmpty(regionServer)) {
List<InputSplit> splitList = regionServersInputSplits.get(regionServer);
if (splitList == null) {
splitList = new LinkedList<InputSplit>();
regionServersInputSplits.put(regionServer, splitList);
}
splitList.add(inputSplit);
continue;
}
}
// If TableSplit or region server not found, add it.
roundRobinInputSplits.add(inputSplit);
}
}
while (true) { //For each regionServer keep adding one split, till all are added
if (regionServersInputSplits.isEmpty()) {
break;
}
Iterator<String> iterator = regionServersInputSplits.keySet().iterator();
while (iterator.hasNext()){
String regionServer = iterator.next();
List<InputSplit> inputSplitListForRegion = regionServersInputSplits.get(regionServer);
if(! inputSplitListForRegion.isEmpty()) {
roundRobinInputSplits.add(inputSplitListForRegion.remove(0));
}
if(inputSplitListForRegion.isEmpty()) {
iterator.remove();
}
}
}
return roundRobinInputSplits;
}

while (true) { //For each regionServer keep adding one split, till all are added
if (regionServersInputSplits.isEmpty()) {
break;
}
...
}

could be replaced with

while (!regionServersInputSplits.isEmpty()) {
...
}

"inputSplits.size() != 0" VS "inputSplits.isEmpty()"

http://pmd.sourceforge.net/pmd-4.3.0/rules/design.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment