Example usage for org.apache.mahout.cf.taste.hadoop TopItemsQueue getTopItems

List of usage examples for org.apache.mahout.cf.taste.hadoop TopItemsQueue getTopItems

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.hadoop TopItemsQueue getTopItems.

Prototype

public List<RecommendedItem> getTopItems() 

Source Link

Usage

From source file:hadoop.api.AggregateAndRecommendReducer.java

License:Apache License

/**
 * find the top entries in recommendationVector, map them to the real itemIDs and write back the result
 *///from  www.  j  a  v a 2s . co  m
private void writeRecommendedItems(VarLongWritable userID, Vector recommendationVector, Context context)
        throws IOException, InterruptedException {

    TopItemsQueue topKItems = new TopItemsQueue(recommendationsPerUser);

    for (Element element : recommendationVector.nonZeroes()) {
        int index = element.index();
        long itemID;
        if (indexItemIDMap != null && !indexItemIDMap.isEmpty()) {
            itemID = indexItemIDMap.get(index);
        } else { //we don't have any mappings, so just use the original
            itemID = index;
        }
        if (itemsToRecommendFor == null || itemsToRecommendFor.contains(itemID)) {
            float value = (float) element.get();
            if (!Float.isNaN(value)) {

                MutableRecommendedItem topItem = topKItems.top();
                if (value > topItem.getValue()) {
                    topItem.set(itemID, value);
                    topKItems.updateTop();
                }
            }
        }
    }

    List<RecommendedItem> topItems = topKItems.getTopItems();
    if (!topItems.isEmpty()) {
        recommendedItems.set(topItems);
        context.write(userID, recommendedItems);
    }
}