Example usage for org.apache.mahout.cf.taste.impl.model.mongodb MongoDBDataModel fromLongToId

List of usage examples for org.apache.mahout.cf.taste.impl.model.mongodb MongoDBDataModel fromLongToId

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.model.mongodb MongoDBDataModel fromLongToId.

Prototype

public String fromLongToId(long id) 

Source Link

Document

Translates the Mahout/MongoDBDataModel's internal identifier to MongoDB identifier, if required.

Usage

From source file:com.aguin.stock.recommender.StockRecommender.java

License:Apache License

public static void run(DataModel model, Recommender rec) throws TasteException {
    if (!(model instanceof MongoDBDataModel)) {
        throw new ClassCastException("Data Model must be a-Mongo!");
    }//from  w  ww .j a  va 2 s . c  om
    MongoDBDataModel mgmodel = (MongoDBDataModel) model;
    System.out.println("Start recommender\n");
    LongPrimitiveIterator it = mgmodel.getUserIDs();
    while (it.hasNext()) {
        long userId = it.nextLong();

        // get the recommendations for the user
        List<RecommendedItem> recommendations = rec.recommend(userId, 10);

        // if empty write something
        if (recommendations.size() == 0) {
            System.out.print("User ");
            System.out.print(mgmodel.fromLongToId(userId));
            System.out.println(": no recommendations");
        }

        // print the list of recommendations for each 
        for (RecommendedItem recommendedItem : recommendations) {
            System.out.print("User ");
            System.out.print(mgmodel.fromLongToId(userId));
            System.out.print(": ");
            System.out.println(recommendedItem);
        }
    }
}

From source file:com.aguin.stock.recommender.WriteUserInfo.java

License:Apache License

public void readFromDB() {
    MongoDBDataModel md = MongoDBUserModel.instance();
    if (!MongoDBUserModel.registered(user)) {
        System.out.format("User %s not registered. Please use -ip or -f options to add preferences first",
                user);//w w w .  j  av a2s .c  o  m
        return;
    }
    try {
        FastIDSet idset = md.getItemIDsFromUser(Long.parseLong(md.fromIdToLong(user, true)));
        if (idset.contains(Long.parseLong(md.fromIdToLong(item, false)))) {
            System.out.format("Found in db: user=%s,item=%s\n", user, item);
        } else {
            System.out.format("Not found: user=%s,item=%s\n", user, item);
        }
        LongPrimitiveIterator fi = idset.iterator();
        StringBuilder sb = new StringBuilder();
        while (fi.hasNext()) {
            sb.append("Item ");
            sb.append(md.fromLongToId(fi.next()));
            sb.append("\n");
        }
        System.out.println(sb);
    } catch (TasteException e) {
        e.printStackTrace();
    }
}