本文整理汇总了Java中org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel类的典型用法代码示例。如果您正苦于以下问题:Java MySQLJDBCDataModel类的具体用法?Java MySQLJDBCDataModel怎么用?Java MySQLJDBCDataModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MySQLJDBCDataModel类属于org.apache.mahout.cf.taste.impl.model.jdbc包,在下文中一共展示了MySQLJDBCDataModel类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMahoutDatabaseModel
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel; //导入依赖的package包/类
/**
* Builds a new Mahout Database model that connects to the MySQL
* database using the credentials set-up in the WebDSL configuration.
* @return The Mahoud JDBC Data Model instance
*/
private MySQLJDBCDataModel getMahoutDatabaseModel(){
/*
* Give Mahout this instance as the data source, Mahout will be able
* to call the getConnection method to obtain the database jdbc connection
* with the database that Hibernate is connected to. This has been implemented
* like this so the H2 database could also be integrated. However, the function
* used to obtain the datasource (Session.connection() of Hibernate is set to
* deprecate in version 4. However, from what I read in their internal docs
* they are still not convinced to remove it completely, because a lot of
* customers need this function for their products.
* Look at rev: 4809 for the version that used the properties of Hibernat
* to set-up the database connection if you need to replace this later on.
* ~ Siem
*/
return new MySQLJDBCDataModel(this, "__"+this.name+"_Taste", "user_id", "item_id", "preference", null);
}
示例2: main
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel; //导入依赖的package包/类
public static void main(String[] args) {
try {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setDatabaseName("rec");
JDBCDataModel dm = new MySQLJDBCDataModel(dataSource,"ratings","userid","itemid","rating","");
UserSimilarity similarity = new PearsonCorrelationSimilarity(dm);
UserNeighborhood neighbor = new NearestNUserNeighborhood(2,similarity, dm);
Recommender recommender = new GenericUserBasedRecommender(dm, neighbor, similarity);
List<RecommendedItem> list = recommender.recommend(1, 3);// recommend
// one item
// to user
// 1
for (RecommendedItem ri : list) {
System.out.println(ri);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: loadFromDB
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel; //导入依赖的package包/类
public static DataModel loadFromDB() throws Exception {
// Database-based DataModel - MySQLJDBCDataModel
/*
* A JDBCDataModel backed by a PostgreSQL database and accessed via
* JDBC. It may work with other JDBC databases. By default, this class
* assumes that there is a DataSource available under the JNDI name
* "jdbc/taste", which gives access to a database with a
* "taste_preferences" table with the following schema: CREATE TABLE
* taste_preferences ( user_id BIGINT NOT NULL, item_id BIGINT NOT NULL,
* preference REAL NOT NULL, PRIMARY KEY (user_id, item_id) ) CREATE
* INDEX taste_preferences_user_id_index ON taste_preferences (user_id);
* CREATE INDEX taste_preferences_item_id_index ON taste_preferences
* (item_id);
*/
MysqlDataSource dbsource = new MysqlDataSource();
dbsource.setUser("user");
dbsource.setPassword("pass");
dbsource.setServerName("localhost");
dbsource.setDatabaseName("my_db");
DataModel dataModelDB = new MySQLJDBCDataModel(dbsource,
"taste_preferences", "user_id", "item_id", "preference",
"timestamp");
return dataModelDB;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:28,代码来源:BookRecommender.java
示例4: getRecommenderItem
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel; //导入依赖的package包/类
public static String getRecommenderItem(int id) throws IOException, TasteException {
//DB연동
MysqlDataSource datasource = new MysqlDataSource();
datasource.setServerName("localhost");
datasource.setUser("root");
datasource.setPassword("465651");
datasource.setDatabaseName("tourOfAll2");
DataModel model = new ReloadFromJDBCDataModel(new MySQLJDBCDataModel(datasource, "evaluations", "user_id", "item_id", "score", null));
// DataModel model = new FileDataModel(
// new
// File("C:/Users/Administrator/git/RestfulMahoutRecommender/RestfulRecommenderApi/src/main/resources/ddd.csv"));
//유사도 측정을 캐쉬로 저장
UserSimilarity similarity = new CachingUserSimilarity(new EuclideanDistanceSimilarity(model),model);
// new SpearmanCorrelationSimilarity(model);
//유저 이웃 계산 결과를 캐쉬로 저장
UserNeighborhood neighborhood = new CachingUserNeighborhood(new ThresholdUserNeighborhood(0.75, similarity, model),model);
// new NearestNUserNeighborhood(5,similarity,model);
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
LoadEvaluator.runLoad(recommender);
String json = "{" + "\"Items\"" + ":" + "[";
IDRescorer testRescorer = new GenreRescorer(id);
List<RecommendedItem> recommendations = recommender.recommend(id, 20, testRescorer);
// List<RecommendedItem> recommendations = recommender.recommend(id, 10);
// String Parsing 아이디값만 찾음
Iterator<RecommendedItem> itr = recommendations.iterator();
while (itr.hasNext()) {
RecommendedItem item = itr.next();
String str = item.toString();
String ItemId = str.substring(str.indexOf(":") + 1, str.indexOf(","));
String value = str.substring(str.indexOf("value:") + 6, str.indexOf("value:") + 9);
getPlaceURL url = new getPlaceURL(Integer.parseInt(ItemId));
GetPlaceTitle title = new GetPlaceTitle(Integer.parseInt(ItemId));
if (itr.hasNext())
json = json + "{" + "\"ID\"" + ":" + "\"" + ItemId + "\""
+ ", " + "\"Value\"" + ":" + "\"" + value + "\""
+ ", " + "\"URL\"" + ":" + "\"" + url.getURL() + "\""
+ ", " + "\"Title\"" + ":" + "\"" + title.getTitle() + "\""
+ "}" + ", ";
else
json = json + "{" + "\"ID\"" + ":" + "\"" + ItemId + "\""
+ ", " + "\"Value\"" + ":" + "\"" + value + "\""
+ ", " + "\"URL\"" + ":" + "\"" + url.getURL() + "\""
+ ", " + "\"Title\"" + ":" + "\"" + title.getTitle() + "\""
+ "}";
}
json = json + "]" + "}";
return (json);
}
示例5: reconstructRecommendationCache
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel; //导入依赖的package包/类
/**
* This function will (re)construct all the recommendations using
* Mahout.
* NOTE: Be aware that this is a very expensive function in terms of
* processing time, therefore this function is called on a regular
* interval (say every x hours) to update the recommendation cache
* according to the latest data available in the database.
*/
public void reconstructRecommendationCache(){
long startTime = System.currentTimeMillis();
try {
/* Open the database and update the dictionary to Mahout style */
convertUIDToBigIntInDB();
/*
* Database is converted, generate the recommendations seperately
* from the currently cached recommendations if there are any.
*/
MySQLJDBCDataModel model = getMahoutDatabaseModel();
/*
* Build the recommender itself.
*/
Recommender userRecommender = null;
Recommender itemRecommender = null;
if(this.type == null || this.type.equalsIgnoreCase(T_USER) || this.type.equalsIgnoreCase(T_BOTH)){
userRecommender = buildRecommenderInstance(model, RecFieldType.USER);
}
if(this.type == null || this.type.equalsIgnoreCase(T_ITEM) || this.type.equalsIgnoreCase(T_BOTH)){
itemRecommender = buildRecommenderInstance(model, RecFieldType.ITEM);
}
/*
* The recommendation process has finished, replace the cached
* recommendations with the new updated version.
*/
if(this.type == null || this.type.equalsIgnoreCase(T_USER) || this.type.equalsIgnoreCase(T_BOTH)){
this.userRecommenderCache = new CachingRecommender(userRecommender);
}
if(this.type == null || this.type.equalsIgnoreCase(T_ITEM) || this.type.equalsIgnoreCase(T_BOTH)){
this.itemRecommenderCache = new CachingRecommender(itemRecommender);
}
this.lastExecutionTime = System.currentTimeMillis() - startTime;
org.webdsl.logging.Logger.info("Building the Mahout Index took: " + this.lastExecutionTime);
} catch (Exception e){
org.webdsl.logging.Logger.error("\n\n************ SEVERE ERROR ************\nThere was an exception while reconstructing the recommendations!\n" + e);
org.webdsl.logging.Logger.error("EXCEPTION",e);
org.webdsl.logging.Logger.error("************ END OF ERROR ************\n\n");
}
}