你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop之测试KMeans

[日期:2013-05-25] 来源:Linux社区  作者:shizhixin [字体:大 中 小]

KReducer.java

import java.io.IOException;

import org.apache.Hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;


public class KReducer extends Reducer<Text, Text, Text, Text> {
 
 
 public void reduce(Text key,Iterable<Text> value,Context context) throws IOException,InterruptedException
 {
  String outVal = "";
  int count=0;
  String center="";
  int length = key.toString().replace("(", "").replace(")", "").replace(":", "").split(",").length;
  float[] ave = new float[Float.SIZE*length];
  for(int i=0;i<length;i++)
   ave[i]=0;
  for(Text val:value)
  {
   outVal += val.toString()+" ";
   String[] tmp = val.toString().replace("(", "").replace(")", "").split(",");
   for(int i=0;i<tmp.length;i++)
    ave[i] += Float.parseFloat(tmp[i]);
   count ++;
  }
  for(int i=0;i<length;i++)
  {
   ave[i]=ave[i]/count;
   if(i==0)
    center += "("+ave[i]+",";
   else {
    if(i==length-1)
     center += ave[i]+")";
    else {
     center += ave[i]+",";
    }
   }
  }
  System.out.println(center);
  context.write(key, new Text(outVal+center));
 }

}

NewCenter.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


public class NewCenter {
 
 int k = 3;
 float shold=Integer.MIN_VALUE;
 String[] line;
 String newcenter = new String("");
 
 public float run(String[] args) throws IOException,InterruptedException
 {
  Configuration conf = new Configuration();
  conf.set("hadoop.job.ugi", "hadoop,hadoop");
  FileSystem fs = FileSystem.get(URI.create(args[2]+"/part-r-00000"),conf);
  FSDataInputStream in = null;
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try{
         
   in = fs.open( new Path(args[2]+"/part-r-00000"));
   IOUtils.copyBytes(in,out,50,false);
   line = out.toString().split("\n");
   } finally {
    IOUtils.closeStream(in);
   }
  System.out.println(out.toString());
  for(int i=0;i<k;i++)
  {
   String[] l = line[i].replace("\t", " ").split(" ");
   String[] startCenter = l[0].replace("(", "").replace(")", "").split(",");
   String[] finalCenter = l[l.length-1].replace("(", "").replace(")", "").split(",");
   float tmp = 0;
   for(int j=0;j<startCenter.length;j++)
    tmp += Math.pow(Float.parseFloat(startCenter[j])-Float.parseFloat(finalCenter[j]), 2);
   newcenter = newcenter + l[l.length - 1].replace("\t", "") + " ";
   if(shold <= tmp)
    shold = tmp; 
  }
  OutputStream out2 = fs.create(new Path(args[1]+"/center") );
  IOUtils.copyBytes(new ByteArrayInputStream(newcenter.getBytes()), out2, 4096,true);
  System.out.println(newcenter);
  return shold;
 }

}

 

linux
相关资讯       Hadoop测试  KMeans 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款