千家信息网

mapreduce中怎么实现K-M类聚

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,mapreduce中怎么实现K-M类聚,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先是mappublic static clas
千家信息网最后更新 2025年02月08日mapreduce中怎么实现K-M类聚

mapreduce中怎么实现K-M类聚,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

首先是map

public static class KMmap extends Mapper{        //中心集合        //这里的聚簇集合是自己设定的    centersPath就是集合在hdfs中存放的路径        ArrayList> centers = null;        //用k个中心        int k = 0;        //读取中心        protected void setup(Context context)throws IOException, InterruptedException {            //getCentersFromHDFS方法就是传入一个Path,得到一个ArrayList>集合             centers = Utils.getCentersFromHDFS(context.getConfiguration().get("centersPath"),false);             k = centers.size();        }         /**          * 1.每次读取一条要分类的条记录与中心做对比,归类到对应的中心          * 2.以中心ID为key,中心包含的记录为value输出(例如: 1 0.2 。  1为聚类中心的ID,0.2为靠近聚类中心的某个值)          */        @Override        protected void map(LongWritable key, Text value,Context context)                throws IOException, InterruptedException {            ArrayList fileds = Utils.textToArray(value);            //textToArray方法将map进来的一行value根据","分割后转化为ArrayList的集合            int sizeOfFileds = fileds.size();            double minDistance = 99999999;            int centerIndex = 0;            //依次取出k个中心点与当前读取的记录做计算            for(int i=0;i

reduce

    //利用reduce的归并功能以中心为Key将记录归并到一起    public static class KMreduce extends Reducer{          /**            * 1.Key为聚类中心的ID value为该中心的记录集合            * 2.计数所有记录元素的平均值,求出新的中心            */                protected void reduce(IntWritable key, Iterable values,    Context context)throws IOException, InterruptedException {             ArrayList> filedsList = new ArrayList>();            //依次读取记录集,每行为一个ArrayList             for(Iterator it = values.iterator();it.hasNext();){                 ArrayList tempList = Utils.textToArray(it.next());                 filedsList.add(tempList);             }             //计算新的中心             //每行的元素个数             int filedSize = filedsList.get(0).size();             double[] avg = new double[filedSize];             for(int i=0;i

最后是其中所用到的util类,主要是提供一些读取文件和操作字符串的方法

public class Utils {        //读取中心文件的数据    public static ArrayList> getCentersFromHDFS(String centersPath,boolean isDirectory)                    throws IOException{        ArrayList> result = new ArrayList>();        Path path = new Path(centersPath);        Configuration conf = new Configuration();                          FileSystem fileSystem = path.getFileSystem(conf);                if(isDirectory){                FileStatus[] listFile = fileSystem.listStatus(path);            for (int i = 0; i < listFile.length; i++) {                result.addAll(getCentersFromHDFS(listFile[i].getPath().toString(),false));                }            return result;        }        FSDataInputStream fsis = fileSystem.open(path);        LineReader lineReader = new LineReader(fsis, conf);        Text line = new Text();          while(lineReader.readLine(line) > 0){                      ArrayList tempList = textToArray(line);                          result.add(tempList);                      }                      lineReader.close();            return result;    }        //删掉文件     public static void deletePath(String pathStr) throws IOException{                Configuration conf = new Configuration();                Path path = new Path(pathStr);                FileSystem hdfs = path.getFileSystem(conf);                hdfs.delete(path ,true);              }               public static ArrayList textToArray(Text text){          ArrayList list = new ArrayList();          String[] fileds = text.toString().split("\t");          for(int i=0;i> oldCenters = Utils.getCentersFromHDFS(centerPath,false);                  List> newCenters = Utils.getCentersFromHDFS(newPath,true);                                      int size = oldCenters.size();                    int fildSize = oldCenters.get(0).size();                    double distance = 0;                    for(int i=0;i

关于mapreduce中怎么实现K-M类聚问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

0