Mahout连接Mysql以及Mysql的基本操作

这两天又开始看Mahout了,前面看了下 HBase,也都是一些浅尝辄止,因为要面试一个公司,所以就什么都看下。

今天看到了用Mysql数据库的数据作为Mahout的输入文件,觉得很高深的样子,所以就试试;

先贴代码:

package org.fansy.date902jdbc;

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.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class MahoutJdbcOne {

	/**
	 * use the MYSQL database as the input for MAHOUT  
	 *
	 */
	public static  void main(String[] args) throws TasteException {
		// TODO Auto-generated method stub
		long t1=System.currentTimeMillis();
		MysqlDataSource dataSource=new MysqlDataSource();
		dataSource.setServerName("localhost");
		dataSource.setUser("fansy");
		dataSource.setPassword("fansy");
		dataSource.setDatabaseName("bookstore");
		JDBCDataModel dataModel=new MySQLJDBCDataModel(dataSource,"mytable","uid","iid","val", "time");
	//	JDBCDataModel dataModel=new MySQLJDBCDataModel(dataSource,"mytable01","uid","iid","val",null);
		DataModel model=dataModel;
		UserSimilarity similarity=new PearsonCorrelationSimilarity(model);
		UserNeighborhood neighborhood=new NearestNUserNeighborhood(2,similarity,model);
		Recommender recommender=new GenericUserBasedRecommender(model,neighborhood,similarity);
		// the Recommender.recommend() method's arguments: first one is the user id;
		//     the second one is the number recommended
		List<RecommendedItem> recommendations=recommender.recommend(1, 2);
		for(RecommendedItem recommendation:recommendations){
			System.out.println(recommendation);
		}
		System.out.println("done and time spend:"+(System.currentTimeMillis()-t1));
	}

}

首先先在Mysql上建立一个表,见表sql脚本如下:

create table mytable(
	uid varchar(50) not null ,
	iid varchar(50) not null,
	val varchar(50) not null,
	time varchar(50) default null
)

导入的数据如下:

其实看 Mahout in Action上面看的不是很明白,上面说uid 和iid 都要定义为primary key, 但是在mysql中应该是只能一个为primary key 的,同时如果该字段被定义为primary key 的话,那么就不能重复了,不符合实际情况。

还有一点就是Mahout In Action 上面说的是

MySQLJDBCDataModel(dataSource,String tablename,String userid,String itemid,String value);
目前我用的版本是这样的:

MySQLJDBCDataModel(dataSource,String tablename,String userid,String itemid,String value,String timestamp);
应该是更新过了,那本书也是比较旧了,好像是09年的。

和刚开始用数据文件输入做了下对比,发现结果都是一样的,但是运行的时间使用数据库的要显劣势,这点和书上讲的一样。最后才发现这个简直和用java 连接数据库获得数据操作一样的,都是按照一定的格式来的,基本格式都一样,所以也不是那么高深了。

同时在操作Mysql时也复习了以前看的一些内容:

source <filename>  -- 执行一个.sql文件
load data local infile '/home/fansy/mahout/data/intro.csv' replace into table mytable fields terminated by ',' lines terminated by '\n' (@col1,@col2,@col3) set uid=@col1,iid=@col2,val=@col3; -- 把一个文件中的数据导入到Mysql的表中


最近看的还是有点慢,英文版本的有些看的也不是很明白,只能是慢慢看了。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值