@Test public void testMoreThanEightProps() { List<Field> fields = new ArrayList<Field>(30); for (int i = 0; i < 30; i++) { fields.add(Field.create("field" + i, Type.STRING)); } Schema schema = new Schema("bigmammaschema", fields); ITuple tuple = new Tuple(schema); for (int i = 0; i < 30; i++) { // Fill in with some default values tuple.set("field" + i, "defaultValue" + i); } NullableTuple nullableTuple = new NullableTuple(tuple); for (int i = 0; i < 30; i++) { // Assert the default values assertEquals("defaultValue" + i, nullableTuple.get("field" + i)); } // Set fields to null one by one and assert that things go well for (int i = 0; i < 30; i++) { nullableTuple.set("field" + i, null); assertEquals(null, nullableTuple.getNullable("field" + i)); nullableTuple.set("field" + i, "defaultValue" + i); assertEquals("defaultValue" + i, nullableTuple.getNullable("field" + i)); } }
protected static void fillObject(boolean isRandom, ITuple tuple, Field field, int index) { Object instance = ReflectionUtils.newInstance(field.getObjectClass(), null); if (instance instanceof A) { A a = (A) instance; a.setId(isRandom ? random.nextInt() + "" : ""); a.setUrl(isRandom ? random.nextLong() + "" : ""); } tuple.set(index, instance); }
protected static void fillString(boolean isRandom, ITuple tuple, int index) { if (isRandom) { switch (random.nextInt(4)) { case 0: tuple.set(index, ""); break; case 1: tuple.set(index, random.nextLong() + ""); break; case 2: tuple.set(index, new Utf8(random.nextLong() + "")); break; case 3: tuple.set(index, new Text(random.nextLong() + "")); break; } } else { tuple.set(index, ""); } }
@Test public void testNoNullableWrapper() { Schema schema = new Schema( "testSchema", Fields.parse("a:string, b:int, c:double, d:float, e:boolean, f:long")); ITuple tuple = new Tuple(schema); tuple.set(0, "foo"); tuple.set(1, 10); tuple.set(2, 20d); tuple.set(3, 30f); tuple.set(4, false); tuple.set(5, 40l); NullableTuple nullableTuple = new NullableTuple(tuple); assertEquals("foo", nullableTuple.get(0).toString()); assertEquals(10, nullableTuple.get(1)); assertEquals(20d, nullableTuple.get(2)); assertEquals(30f, nullableTuple.get(3)); assertEquals(false, nullableTuple.get(4)); assertEquals(40l, nullableTuple.get(5)); }
@Override public void reduce( ITuple group, Iterable<ITuple> tuples, TupleMRContext context, Collector collector) throws IOException, InterruptedException, TupleMRException { int count = 0; ITuple outputTuple = null; for (ITuple tuple : tuples) { outputTuple = tuple; count += (Integer) tuple.get(1); } outputTuple.set(1, count); collector.write(outputTuple, NullWritable.get()); }
/** Fills the fields specified by the range (minIndex, maxIndex) with random data. */ protected static void fillTuple(boolean isRandom, ITuple tuple, int minIndex, int maxIndex) { try { for (int i = minIndex; i <= maxIndex; i++) { Field field = tuple.getSchema().getField(i); switch (field.getType()) { case INT: tuple.set(i, isRandom ? random.nextInt() : 0); break; case LONG: tuple.set(i, isRandom ? random.nextLong() : 0); break; case BOOLEAN: tuple.set(i, isRandom ? random.nextBoolean() : false); break; case DOUBLE: tuple.set(i, isRandom ? random.nextDouble() : 0.0); break; case FLOAT: tuple.set(i, isRandom ? random.nextFloat() : 0f); break; case STRING: fillString(isRandom, tuple, i); break; case ENUM: fillEnum(isRandom, field, tuple, i); break; case OBJECT: fillObject(isRandom, tuple, field, i); break; default: throw new IllegalArgumentException("Not supported type " + field.getType()); } } } catch (Exception e) { throw new RuntimeException(e); } }
protected static void fillEnum(boolean isRandom, Field field, ITuple tuple, int index) throws Exception { Method method = field.getObjectClass().getMethod("values", (Class[]) null); Enum[] values = (Enum[]) method.invoke(null); tuple.set(index, values[isRandom ? random.nextInt(values.length) : 0]); }
public void createInput(String input, Configuration conf) throws IOException, InterruptedException { Path inPath = new Path(input); FileSystem fs = FileSystem.get(inPath.toUri(), conf); TupleFile.Writer writer = new TupleFile.Writer(fs, conf, inPath, TopicalWordCount.getSchema()); // Topic 1, words: { a, 10 } { b, 1 } , { c, 5 } // Top 2 words = a(10), c(5) ITuple tuple = new Tuple(TopicalWordCount.getSchema()); tuple.set("word", "a"); tuple.set("topic", 1); tuple.set("count", 10); writer.append(tuple); tuple.set("word", "b"); tuple.set("topic", 1); tuple.set("count", 1); writer.append(tuple); tuple.set("word", "c"); tuple.set("topic", 1); tuple.set("count", 5); writer.append(tuple); // Topic 2, words: { a, 10 } { b, 9 } , { c, 5 } // Top 2 words = a(10), b(9) tuple.set("word", "a"); tuple.set("topic", 2); tuple.set("count", 10); writer.append(tuple); tuple.set("word", "b"); tuple.set("topic", 2); tuple.set("count", 9); writer.append(tuple); tuple.set("word", "c"); tuple.set("topic", 2); tuple.set("count", 5); writer.append(tuple); writer.close(); }