Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Database Model for Mahout
import java.util.List;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.model.PreferenceArray;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
class DbDataModel {
private DbDataModel() {
}
public static void main(String[] args) {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setUser("root");
dataSource.setPassword("password");
dataSource.setDatabaseName("mia01");
JDBCDataModel model = new MySQLJDBCDataModel(dataSource,
"taste_preferences", "user_id", "item_id", "preference", null);
Recommender recommender = null;
try {
recommender = new SlopeOneRecommender(model);
} catch (TasteException e1) {
e1.printStackTrace();
System.exit(-1);
}
recommender.refresh(null);
List<RecommendedItem> r = null;
try {
r = recommender.recommend(1, 5);
} catch (TasteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(r);
System.out.println(model);
try {
PreferenceArray v = model.getPreferencesFromUser(2);
v.sortByValue();
System.out.println(v);
} catch (TasteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@bcc829

This comment has been minimized.

Copy link

bcc829 commented May 5, 2017

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.