TupleHashPartitioner extends Partitioner<DatumWrapper<ITuple>, NullWritable> implements Configurable { public int partialHashCode(ITuple tuple, int[] fields) { int result = 0; for(int field : fields) { Object o = tuple.get(field); if(o == null) { continue; } int hashCode; if(o instanceof String) { HELPER_UTF8.set((String) o); hashCode = HELPER_UTF8.hashCode(); } else if(o instanceof Text) { HELPER_UTF8.set((Text) o); hashCode = HELPER_UTF8.hashCode(); } else if(o instanceof byte[]){ hashCode = hashBytes((byte[])o,0,((byte[]) o).length); } else if(o instanceof ByteBuffer){ ByteBuffer buffer = (ByteBuffer)o; int offset = buffer.arrayOffset() + buffer.position(); int length = buffer.limit() - buffer.position(); hashCode = hashBytes(buffer.array(),offset,length); } else { hashCode = o.hashCode(); } result = result * 31 + hashCode; } return result; }  @Override int getPartition(DatumWrapper<ITuple> key, NullWritable value, int numPartitions); @Override Configuration getConf(); @Override void setConf(Configuration conf); int partialHashCode(ITuple tuple, int[] fields); static int hashBytes(byte[] array,int offset,int length);  }
@Test public void testNulls() throws TupleMRException, IOException { TupleHashPartitioner partitioner = new TupleHashPartitioner(); Schema schema = new Schema("schema", Fields.parse("a:string, b:int, c:long, d:double")); Tuple tuple = new Tuple(schema); assertEquals(0, partitioner.partialHashCode(tuple, new int[] { 0, 1, 2, 3})); tuple.set("b", 100); assertEquals(new Integer(100).hashCode(), partitioner.partialHashCode(tuple, new int[] { 0, 1, 2, 3})); }