ToUserVectorsReducer extends
    Reducer<VarLongWritable,VarLongWritable,VarLongWritable,VectorWritable> { @Override protected void reduce(VarLongWritable userID, Iterable<VarLongWritable> itemPrefs, Context context) throws IOException, InterruptedException { Vector userVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 100); for (VarLongWritable itemPref : itemPrefs) { int index = TasteHadoopUtils.idToIndex(itemPref.get()); float value = itemPref instanceof EntityPrefWritable ? ((EntityPrefWritable) itemPref).getPrefValue() : 1.0f; userVector.set(index, value); } if (userVector.getNumNondefaultElements() >= minPreferences) { userVectorWritable.set(userVector); userVectorWritable.setWritesLaxPrecision(true); context.getCounter(Counters.USERS).increment(1); context.write(userID, userVectorWritable); } }   static final String MIN_PREFERENCES_PER_USER; }
@Test public void testToUsersReducerMinPreferencesUserIgnored() throws Exception { Reducer<VarLongWritable,VarLongWritable,VarLongWritable,VectorWritable>.Context context = EasyMock.createMock(Reducer.Context.class); ToUserVectorsReducer reducer = new ToUserVectorsReducer(); setField(reducer, "minPreferences", 2); EasyMock.replay(context); reducer.reduce(new VarLongWritable(123), Collections.singletonList(new VarLongWritable(456)), context); EasyMock.verify(context); }
@Test public void testToUsersReducerMinPreferencesUserPasses() throws Exception { Reducer<VarLongWritable,VarLongWritable,VarLongWritable,VectorWritable>.Context context = EasyMock.createMock(Reducer.Context.class); Counter userCounters = EasyMock.createMock(Counter.class); ToUserVectorsReducer reducer = new ToUserVectorsReducer(); setField(reducer, "minPreferences", 2); EasyMock.expect(context.getCounter(ToUserVectorsReducer.Counters.USERS)).andReturn(userCounters); userCounters.increment(1); context.write(EasyMock.eq(new VarLongWritable(123)), MathHelper.vectorMatches( MathHelper.elem(TasteHadoopUtils.idToIndex(456L), 1.0), MathHelper.elem(TasteHadoopUtils.idToIndex(789L), 1.0))); EasyMock.replay(context, userCounters); reducer.reduce(new VarLongWritable(123), Arrays.asList(new VarLongWritable(456), new VarLongWritable(789)), context); EasyMock.verify(context, userCounters); }