HashPartitioner extends Partitioner { @Override public int getPartition(Tuple tuple) { if (numPartitions == 0) return 0; for (int i = 0; i < partitionKeyIds.length; i++) { keyTuple.put(i, tuple.get(partitionKeyIds[i])); } return (keyTuple.hashCode() & Integer.MAX_VALUE) % numPartitions; } HashPartitioner(final int [] keys, final int numPartitions); @Override int getPartition(Tuple tuple);  }
@Test public final void testGetPartition() { Tuple tuple1 = new VTuple(3); tuple1.put(new Datum[] { DatumFactory.createInt4(1), DatumFactory.createInt4(2), DatumFactory.createInt4(3) }); Tuple tuple2 = new VTuple(3); tuple2.put(new Datum[] { DatumFactory.createInt4(1), DatumFactory.createInt4(2), DatumFactory.createInt4(4) }); Tuple tuple3 = new VTuple(3); tuple3.put(new Datum[] { DatumFactory.createInt4(1), DatumFactory.createInt4(2), DatumFactory.createInt4(5) }); Tuple tuple4 = new VTuple(3); tuple4.put(new Datum[] { DatumFactory.createInt4(2), DatumFactory.createInt4(2), DatumFactory.createInt4(3) }); Tuple tuple5 = new VTuple(3); tuple5.put(new Datum[] { DatumFactory.createInt4(2), DatumFactory.createInt4(2), DatumFactory.createInt4(4) }); int [] partKeys = {0,1}; Partitioner p = new HashPartitioner(partKeys, 2); int part1 = p.getPartition(tuple1); assertEquals(part1, p.getPartition(tuple2)); assertEquals(part1, p.getPartition(tuple3)); int part2 = p.getPartition(tuple4); assertEquals(part2, p.getPartition(tuple5)); }
@Test public final void testGetPartition2() { Random rand = new Random(); String[][] data = new String[1000][]; for (int i = 0; i < 1000; i++) { data[i] = new String[]{ String.valueOf(rand.nextInt(1000)), String.valueOf(rand.nextInt(1000)), String.valueOf(rand.nextInt(1000))}; } int[] testNumPartitions = new int[]{31, 62, 124, 32, 63, 125}; for (int index = 0; index < testNumPartitions.length; index++) { Partitioner p = new HashPartitioner(new int[]{0, 1, 2}, testNumPartitions[index]); Set<Integer> ids = new TreeSet<Integer>(); for (int i = 0; i < data.length; i++) { Tuple tuple = new VTuple( new Datum[]{new TextDatum(data[i][0]), new TextDatum(data[i][1]), new TextDatum(data[i][2])}); ids.add(p.getPartition(tuple)); } assertTrue(ids.size() + 5 >= testNumPartitions[index]); } }