一. 配置maven:
1. 下载 maven , http://maven.apache.org/download.cgi , apache-maven-3.1.1-bin.tar.gz
2. 保存到/usr/local,
tar -xzfapache-maven-3.1.1*
mv apache-maven-3.1.1 maven
3. Vi /etc/profile, vi ~/.bashrc,添加如下:
export MAVEN_HOME=/usr/local/maven
export PATH=…..;$MAVEN_HOME/bin
然后执行: source /etc/profile , source ~/.bashrc
4.检查是否配置成功:输入mvn –v,显示版本。
二.配置mahout:
(之前hadoop都已经搭建好,能正常启动hadoop)
1.下载mahout,http://mirror.bit.edu.cn/apache/mahout/,mahout-distribution-0.8.tar.gz
2.保存到/usr/local,tar –xzf mahout* ,mv mahout*0.8 mahout
3. Vi /etc/profile , vi ~/.bashrc,添加如下:
export MAHOUT_HOME=/usr/local/mahout
export PATH=…..;$MAHOUT_HOME/bin
然后执行: source /etc/profile , source ~/.bashrc
4.mahout –help,列出各种算法 也可对其中某种算法: mahout kmeans –help
三.聚类测试(kmeans)
聚类的思路:首先从n个数据对象任意选择 k 个对象作为初始聚类中心;而对于所剩下其它对象,则根据它们与这些聚类中心的相似度(距离),分别将它们分配给与其最相似的(聚类中心所代表的)聚类;然后再计算每个所获新聚类的聚类中心(该聚类中所有对象的均值);不断重复这一过程直到标准测度函数开始收敛为止。
1.下载测试数数据synthetic_control.data,下载地址http://download.youkuaiyun.com/detail/flyinf_guo/5327321
2.在HDFS上创建文件夹testdata,并把数据上传到testdata里:
hadoop fs –mkdir testdata
hadoop fs –put /usr/local/synthetic_control.data testdata
3.执行命令:
hadoop jar /usr/local/mahout/mahout-examples-0.8-job.jarorg.apache.mahout.clustering.syntheticcontrol.kmeans.Job
或者mahout org.apache.mahout.clustering.syntheticcontrol.kmeans.Job
4.查看运行后结果:hadoop fs -ls output
5.分析结果:
clusteredPoints:存放的是最后聚类的结果,将cluster-id和documents-id都展示出来了,用
mahout seqdumper读clusteredPoints结果的key-value类型是(IntWritable,WeightedVectorWritable)
clusters-N:是第N次聚类的结果,其中n为某类的样本数目,c为各类各属性的中心,r为各类属性的半径。 clusters-N结果类型是(Text,Cluster)
data:存放的是原始数据,这个文件夹下的文件可以用mahout vectordump来读取,
原始数据是向量形式的,其它的都只能用mahout seqdumper来读取,向量文件也可以用mahout seqdumper来 读 取。只是用vectordump读取出来的是数字结果,没有对应的key,用seqdumper读出来的可以看到key,即对 应的url, 而value读出来的是一个类描述,而不是数组向量。
mahout seqdumper:将SequenceFile文件转成可读的文本形式,对应的源文件是org.apache.mahout.utils.
SequenceFileDumper.java
mahout vectordump:将向量文件转成可读的文本形式,对应的源文件是org.apache.mahout.utils.vectors .
VectorDumper.java
mahout clusterdump:分析最后聚类的输出结果,对应的源文件是org.apache.mahout.utils.clustering.
ClusterDumper.java
用clusterdump查看,命令如下:
mahout clusterdump --input /user/root/output/clusters-10-final --pointsDir /user/root/output/clusteredPoints --output $MAHOUT_HOME/examples/output/clusteranalyze.txt
VL-52代表这是一个cluster,n=161代表该cluster有161个点,c=[...]代表该cluster的中心向量点,r=[...]代表cluster的半径。
用seqdumper查看,命令如下:mahout seqdumper -i /user/root/output/clusteredPoints/part-m-00000–o /usr/local/result.txt
6.注意:
mahout下处理的文件必须是SequenceFile格式的,所以需要把txtfile转换成sequenceFile。
SequenceFile是hadoop中的一个类。
MAHOUT_HOME/conf/driver.classes.props文件中查看各个算法的入口,如果要添加新的算法,也可以在这个文件中注册。