上面就是全部的代码了,下面贴出结果:
- 0 0.0 0.2 0.4
- 1 0.3 0.2 0.4
- 1 0.4 0.2 0.4
- 1 0.5 0.2 0.4
- 2 5.0 5.2 5.4
- 2 6.0 5.2 6.4
- 2 4.0 5.2 4.4
- 2 10.3 10.4 10.5
- 2 10.3 10.4 10.5
- 2 10.3 10.4 10.5
由最终的结果可以看出分类的结果不是很好,所以说初始的数据中心点一定要选好才行。
下面说下我遇到的问题,刚开始的时候我获取数据维度的方法不是使用读入文件然后再取得相应的信息,而是按照下面的方法:
WrongCombine:
- package org.fansy.date927;
- import java.io.IOException;
- import java.util.Iterator;
- import org.apache.Hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class KmeansC extends Reducer<IntWritable,DataPro,IntWritable,DataPro> {
- public void reduce(IntWritable key,Iterable<DataPro> values,Context context)throws InterruptedException, IOException{
- // get dimension first
- Iterator<DataPro> iter=values.iterator();
- int dimension=0;
- for(DataPro val:values){
- String[] datastr=val.getCenter().toString().split("\t");
- dimension=datastr.length;
- break;
- }
- double[] sum=new double[dimension];
- int sumCount=0;
- // operation one
- while(iter.hasNext()){
- DataPro val=iter.next();
- String[] datastr=val.getCenter().toString().split("\t");
- sumCount+=val.getCount().get();
- for(int i=0;i<dimension;i++){
- sum[i]+=Double.parseDouble(datastr[i]);
- }
- }
- // operation two
- /*for(DataPro val:values){
- String[] datastr=val.getCenter().toString().split("\t");
- sumCount+=val.getCount().get();
- for(int i=0;i<dimension;i++){
- sum[i]+=Double.parseDouble(datastr[i]);
- }
- }*/
- // calculate the new centers
- // double[] newcenter=new double[dimension];
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<dimension;i++){
- sb.append(sum[i]+"\t");
- }
- System.out.println("combine text:"+sb.toString());
- System.out.println("combine sumCount:"+sumCount);
- DataPro newvalue=new DataPro();
- newvalue.set(new Text(sb.toString()), new IntWritable(sumCount));
- context.write(key, newvalue);
- }
- }
从第16到20行是我获得数据维度的方法,但是虽然维度获得了,但是,后面的操作就出问题了,从我加入的调试提示信息可以看出是出了什么问题了:
- 12/09/28 14:40:40 INFO mapred.Task: Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@435e331b
- 12/09/28 14:40:40 INFO mapred.MapTask: io.sort.mb = 100
- 12/09/28 14:40:40 INFO mapred.MapTask: data buffer = 79691776/99614720
- 12/09/28 14:40:40 INFO mapred.MapTask: record buffer = 262144/327680
- 0.0,0.0,0.0,
- 5.0,5.0,5.0,
- 10.0,10.0,10.0,
- the map out:0,0.0 0.2 0.4
- the map out:0,0.3 0.2 0.4
- the map out:0,0.4 0.2 0.4
- the map out:0,0.5 0.2 0.4
- the map out:1,5.0 5.2 5.4
- the map out:1,6.0 5.2 6.4
- the map out:1,4.0 5.2 4.4
- the map out:2,10.3 10.4 10.5
- the map out:2,10.3 10.4 10.5
- the map out:2,10.3 10.4 10.5
- 12/09/28 14:40:40 INFO mapred.MapTask: Starting flush of map output
- combine text:1.2 0.6000000000000001 1.2000000000000002
- combine sumCount:3
- combine text:10.0 10.4 10.8
- combine sumCount:2
- combine text:20.6 20.8 21.0
- combine sumCount:2
- 12/09/28 14:40:40 INFO mapred.MapTask: Finished spill 0
- 12/09/28 14:40:40 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
有上面的信息可以看出首先map操作没有问题,但是combine操作时计算相同的index的个数不对,每个都少了1个,而且计算的总和也不对,所以我这样猜想,是不是通过这样的操作:
- for(DataPro val:values){
- String[] datastr=val.getCenter().toString().split("\t");
- dimension=datastr.length;
- break;
- }
可能改变了values的指针或者其他的什么东西之类的,导致后面我再使用这样的操作读取的时候就会从第二个开始读了。这个就不知到了。
