hadoop下的Kmeans算法实现三
来源:互联网 发布:淘宝店童装 编辑:程序博客网 时间:2024/05/26 14:10
初始化中心点CenterInitial.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 CenterInitial {
- public void run(String[] args) throws IOException
- {
- String[] clist;//用于保存中心点
- int k = 2;//中心点选取个数
- String string = "";//保存各个中心点在同一个字符串string中
- String inpath = args[0]+"/4.txt"; //cluster数据集放在2.txt中
- String outpath = args[1]+"/input2/3.txt"; //center新选取的中心点放进3.txt中保存
- Configuration conf1 = new Configuration(); //读取hadoop文件系统的配置
- conf1.set("hadoop.job.ugi", "hadoop,hadoop"); //配置信息设置
- FileSystem fs = FileSystem.get(URI.create(inpath),conf1); //FileSystem是用户操作HDFS的核心类,它获得URI对应的HDFS文件系统
- FSDataInputStream in = null;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- try{
- in = fs.open( new Path(inpath) );
- IOUtils.copyBytes(in,out,50,false); //用Hadoop的IOUtils工具方法来让这个文件的指定字节复制到标准输出流上
- //把in读到的数据 复制到out上
- clist = out.toString().split(" ");//将out以空格为分割符转换成数组在clist中保存
- } finally {
- IOUtils.closeStream(in);
- }
- FileSystem filesystem = FileSystem.get(URI.create(outpath), conf1); //获得URI对应的HDFS文件系统
- for(int i=0;i<k;i++)
- {
- int j=(int) (Math.random()*100) % clist.length;//选取0到clist.lenth-1的随机数
- if(string.contains(clist[j])) // 如果选取的是同一个随机数
- {
- k++;
- continue;
- }
- string = string + clist[j].replace(" ", "") + " ";//将得到的k个随机点的坐标用一个字符串保存
- }
- OutputStream out2 = filesystem.create(new Path(outpath) );
- IOUtils.copyBytes(new ByteArrayInputStream(string.getBytes()), out2, 4096,true); //把随机点坐标字符串out2中
- System.out.println("初始化过程:"+string);
- }
- }
0 0
- hadoop下的Kmeans算法实现三
- hadoop下的Kmeans算法实现三
- hadoop下的Kmeans算法实现一
- hadoop下的Kmeans算法实现二
- hadoop下的Kmeans算法实现
- hadoop下的Kmeans算法实现一
- hadoop下的Kmeans算法实现
- mahout下的Hadoop平台上的Kmeans算法实现
- hadoop下kmeans算法实现四
- hadoop下kmeans算法实现四
- Hadoop 实现kmeans 算法
- 基于Hadoop的Kmeans算法实现
- hadoop下实现kmeans算法——一个mapreduce的实现方法
- hadoop下实现kmeans算法——一个mapreduce的实现方法
- hadoop下实现kmeans算法——一个mapreduce的实现方法
- 用Hadoop实现KMeans算法
- hadoop下实现kmeans一
- KMeans算法的实现
- ActiveMQ学习总结(4)——业界消息队列简介
- 特大喜讯,View and Data API 现在支持中文界面了
- Node.js开发者必须了解的4个JS要点
- Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法
- jQuery -- 判断属性是否存在
- hadoop下的Kmeans算法实现三
- STM32系列单片机命名规则
- smarty截取字符串与过滤HTML标签
- 图片预加载与图片懒加载(缓载)的区别与实现
- android 设计模式笔记
- ❀自我唠嗑OC-省市区排序
- Html5判断app是否安装
- cin详解(cin.get()、cin.getline()、cin.clear()、cin.sync())
- codeigniter 入门项目