@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testUDF() throws IOException { StringMapsMerger udf = new StringMapsMerger(); TupleFactory tupleFactory = TupleFactory.getInstance(); Map<String, String> emptyMap = new HashMap<String, String>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("12", "12"); map1.put("3", "bag"); map1.put("1", ""); Map<String, String> map2 = new HashMap<String, String>(); map2.put("12", "null"); map2.put("3", "data"); map2.put("35", "empty"); Map<String, String> map3 = new HashMap<String, String>(); map3.put("1", "notempty"); Map<String, String> map4 = new HashMap<String, String>(); map4.put("1", ""); map4.put("3", "bag"); map4.put("12", "12"); map4.put("35", "empty"); assertNull(udf.exec(null)); assertNull(udf.exec(tupleFactory.newTuple())); assertNull(udf.exec(tupleFactory.newTuple((Map)null))); assertNull(udf.exec(tupleFactory.newTuple(emptyMap))); assertEquals(map1, udf.exec(tupleFactory.newTuple(map1))); assertEquals(map4, udf.exec(tupleFactory.newTuple(Lists.newArrayList(map1, emptyMap, map2, null, map3)))); }
@Override public Map<String, String> exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() == 0) { return null; } Map<String, String> merged = new HashMap<String, String>(); for (int i = 0; i < tuple.size(); i++) { if (tuple.get(i) != null) { @SuppressWarnings("unchecked") Map<String, String> element = (Map<String, String>) tuple.get(i); for (Map.Entry<String, String> entry : element.entrySet()) { if (merged.get(entry.getKey()) == null) { merged.put(entry.getKey(), entry.getValue()); } } } } if (merged.isEmpty()) { return null; } return merged; }
StringMapsMerger extends EvalFunc<Map<String, String>> { @Override public Map<String, String> exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() == 0) { return null; } Map<String, String> merged = new HashMap<String, String>(); for (int i = 0; i < tuple.size(); i++) { if (tuple.get(i) != null) { @SuppressWarnings("unchecked") Map<String, String> element = (Map<String, String>) tuple.get(i); for (Map.Entry<String, String> entry : element.entrySet()) { if (merged.get(entry.getKey()) == null) { merged.put(entry.getKey(), entry.getValue()); } } } } if (merged.isEmpty()) { return null; } return merged; } }
StringMapsMerger extends EvalFunc<Map<String, String>> { @Override public Map<String, String> exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() == 0) { return null; } Map<String, String> merged = new HashMap<String, String>(); for (int i = 0; i < tuple.size(); i++) { if (tuple.get(i) != null) { @SuppressWarnings("unchecked") Map<String, String> element = (Map<String, String>) tuple.get(i); for (Map.Entry<String, String> entry : element.entrySet()) { if (merged.get(entry.getKey()) == null) { merged.put(entry.getKey(), entry.getValue()); } } } } if (merged.isEmpty()) { return null; } return merged; }  }
StringMapsMerger extends EvalFunc<Map<String, String>> { @Override public Map<String, String> exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() == 0) { return null; } Map<String, String> merged = new HashMap<String, String>(); for (int i = 0; i < tuple.size(); i++) { if (tuple.get(i) != null) { @SuppressWarnings("unchecked") Map<String, String> element = (Map<String, String>) tuple.get(i); for (Map.Entry<String, String> entry : element.entrySet()) { if (merged.get(entry.getKey()) == null) { merged.put(entry.getKey(), entry.getValue()); } } } } if (merged.isEmpty()) { return null; } return merged; }  @Override Map<String, String> exec(Tuple tuple); @Override Schema outputSchema(Schema input); }
StringMapsMerger extends EvalFunc<Map<String, String>> { @Override public Map<String, String> exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() == 0) { return null; } Map<String, String> merged = new HashMap<String, String>(); for (int i = 0; i < tuple.size(); i++) { if (tuple.get(i) != null) { @SuppressWarnings("unchecked") Map<String, String> element = (Map<String, String>) tuple.get(i); for (Map.Entry<String, String> entry : element.entrySet()) { if (merged.get(entry.getKey()) == null) { merged.put(entry.getKey(), entry.getValue()); } } } } if (merged.isEmpty()) { return null; } return merged; }  @Override Map<String, String> exec(Tuple tuple); @Override Schema outputSchema(Schema input);  }