代码片段如下
String driver = "com.mysql.jdbc.Driver";
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setServerName("");
mysqlDataSource.setUser("");
mysqlDataSource.setPassword("");
mysqlDataSource.setDatabaseName("");
Class.forName(driver);
JDBCDataModel jdbcDataModel = new MySQLJDBCDataModel(mysqlDataSource,
preferenceTable,
userIDColumn,
itemIDColumn,
preferenceColumn,
timestampColumn);
ReloadFromJDBCDataModel dataModel = new ReloadFromJDBCDataModel(jdbcDataModel);
// 构造数据模型,计算内容相似度
ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(dataModel);
// 构造推荐引擎
Recommender recommender = new GenericItemBasedRecommender(dataModel, itemSimilarity);
// 得到推荐的结果
List<RecommendedItem> recommendedItemList = recommender.recommend(userId, recommendNum);
for (RecommendedItem recommendedItem : recommendedItemList) {
long itemID = recommendedItem.getItemID();
float value = recommendedItem.getValue();
System.out.println("itemID:" + itemID + " -------------- " + "value:" + value);
}