String input2 = TestTupleMRJob.class.getCanonicalName() + "-input2";
String output = TestTupleMRJob.class.getCanonicalName() + "-output";
final Schema schemaNoNulls = new Schema("NoNulls", Fields.parse("f1:int,f2:string"));
final Schema schemaNulls = new Schema("Nulls", Fields.parse("f1:int?,f2:string?"));
Tuple t1 = new Tuple(schemaNoNulls);
Tuple t2 = new Tuple(schemaNulls);
t1.set(0, 0);
t1.set(1, "nn");
withTupleInput(input1, t1);
Object tuples[][] = new Object[][] { new Object[] { 0, null }, new Object[] { 0, "n1" },
new Object[] { null, "n2" } };
for(Object[] tuple : tuples) {
t2.set(0, tuple[0]);
t2.set(1, tuple[1]);
withTupleInput(input2, t2);
}
TupleMRBuilder builder = new TupleMRBuilder(getConf(), "test");
builder.addTupleInput(new Path(input1), new IdentityTupleMapper());
builder.addTupleInput(new Path(input2), new IdentityTupleMapper());
builder.setTupleReducer(new TupleReducer<ITuple, NullWritable>() {
@Override
public void reduce(ITuple group, Iterable<ITuple> tuples, TupleMRContext context,
Collector collector) throws IOException, InterruptedException, TupleMRException {
int count = 0;
for(ITuple tuple : tuples) {
Tuple t = new Tuple(schemaNulls);
t.set(0, tuple.get(0));
t.set(1, tuple.get(1));
collector.write(t, NullWritable.get());
count++;
}
if(group.get(0) == null) {
assertEquals(1, count);
} else if(((Integer) group.get(0)) == 0) {
assertEquals(3, count);
}
}
});
builder.addIntermediateSchema(schemaNoNulls);
builder.addIntermediateSchema(schemaNulls);
builder.setGroupByFields("f1");
builder.setOrderBy(OrderBy.parse("f1:desc|null_smallest").addSchemaOrder(Criteria.Order.ASC));
builder.setSpecificOrderBy("NoNulls", OrderBy.parse("f2:asc|null_biggest"));
builder.setSpecificOrderBy("Nulls", OrderBy.parse("f2:asc|null_biggest"));
builder.setTupleOutput(new Path(output), schemaNulls);
Job job = builder.createJob();
job.setNumReduceTasks(1);
try {
assertRun(job);
} finally {
builder.cleanUpInstanceFiles();
}
final Object expectedOutput[][] = new Object[][] { new Object[] { 0, "nn" },
new Object[] { 0, "n1" }, new Object[] { 0, null }, new Object[] { null, "n2" } };
boolean debug = false;
if(debug) {
readTuples(new Path(output + "/part-r-00000"), getConf(), new TupleVisitor() {
@Override
public void onTuple(ITuple t) {
System.out.println(t);
}
});
}
readTuples(new Path(output + "/part-r-00000"), getConf(), new TupleVisitor() {
int i = 0;
@Override
public void onTuple(ITuple t) {
assertEqualsNull(expectedOutput[i][0], t.get(0));
Object f2 = t.get(1);
f2 = (f2 != null) ? f2.toString() : f2;
assertEqualsNull(expectedOutput[i][1], f2);
i++;
}
});