@ConstantFieldsExcept(1) public static class FilterDocs extends MapFunction implements Serializable { private static final long serialVersionUID = 1L;
/** * MapFunction that filters for records of the visits relation where the year * (from the date string) is equal to a certain value. */ @ConstantFieldsExcept(1) public static class FilterVisits extends MapFunction implements Serializable { private static final long serialVersionUID = 1L; private static final int YEARFILTER = 2010; /** * Filters for records of the visits relation where the year of visit is equal to a * specified value. The URL of all visit records passing the filter is emitted. * * Output Format: * 0: URL */ @Override public void map(Record record, Collector<Record> out) throws Exception { // Parse date string with the format YYYY-MM-DD and extract the year String dateString = record.getField(1, StringValue.class).getValue(); int year = Integer.parseInt(dateString.substring(0,4)); if (year == YEARFILTER) { record.setNull(1); out.collect(record); } } }
/** * MapFunction that filters for records where the rank exceeds a certain threshold. */ @ConstantFieldsExcept({}) public static class FilterRanks extends MapFunction implements Serializable { private static final long serialVersionUID = 1L; private static final int RANKFILTER = 50; /** * Filters for records of the rank relation where the rank is greater * than the given threshold. * * Output Format: * 0: URL * 1: RANK * 2: AVG_DURATION */ @Override public void map(Record record, Collector<Record> out) throws Exception { if (record.getField(1, IntValue.class).getValue() > RANKFILTER) { out.collect(record); } } }
/** * Increments the first field of the first record of the reduce group by 100 and emits it. * Then all remaining records of the group are emitted. * */ @ConstantFieldsExcept(0) public static class IdentityReducer extends ReduceFunction implements Serializable { private static final long serialVersionUID = 1L; @Override public void reduce(Iterator<Record> records, Collector<Record> out) { Record next = records.next(); // Increments the first field of the first record of the reduce group by 100 and emit it IntValue incrVal = next.getField(0, IntValue.class); incrVal.setValue(incrVal.getValue() + 100); next.setField(0, incrVal); out.collect(next); // emit all remaining records while (records.hasNext()) { out.collect(records.next()); } } }
@ConstantFieldsExcept({}) public static class DummyReduce extends ReduceFunction { private static final long serialVersionUID = 1L; @Override public void reduce(Iterator<Record> values, Collector<Record> out) { while (values.hasNext()) { out.collect(values.next()); } } }