zoukankan      html  css  js  c++  java
  • 微博推荐 第三个map 源码

    package com.laoxiao.mr.tf;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.StringReader;
    import java.net.URI;
    import java.text.NumberFormat;
    import java.util.HashMap;
    import java.util.Map;

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.lib.input.FileSplit;
    import org.wltea.analyzer.core.IKSegmenter;
    import org.wltea.analyzer.core.Lexeme;

    /**
     * 最后计算
     * @author root
     *
     */
    public class LastMapper extends Mapper<LongWritable, Text, Text, Text> {
        //存放微博总数
        public static Map<String, Integer> cmap = null;
        //存放df
        public static Map<String, Integer> df = null;

        // 在map方法执行之前 ,每个maptask调用一次(每个map任务对应一个LastMapper对象,一个对象回调一次setup方法)
        protected void setup(Context context) throws IOException,
                InterruptedException {
            System.out.println("******************");
            if (cmap == null || cmap.size() == 0 || df == null || df.size() == 0) {

                URI[] ss = context.getCacheFiles();
                if (ss != null) {
                    for (int i = 0; i < ss.length; i++) {
                        URI uri = ss[i];
                        if (uri.getPath().endsWith("part-r-00003")) {//微博总数
                            Path path =new Path(uri.getPath());
                            BufferedReader br = new BufferedReader(new FileReader(path.getName()));
                            String line = br.readLine();
                            if (line.startsWith("weibo.count")) {
                                String[] ls = line.split(" ");
                                cmap = new HashMap<String, Integer>();
                                cmap.put(ls[0], Integer.parseInt(ls[1].trim()));
                            }
                            br.close();
                        } else if (uri.getPath().endsWith("part-r-00000")) {//词条的DF
                            df = new HashMap<String, Integer>();
                            Path path =new Path(uri.getPath());
                            BufferedReader br = new BufferedReader(new FileReader(path.getName()));
                            String line;
                            while ((line = br.readLine()) != null) {
                                String[] ls = line.split(" ");
                                df.put(ls[0], Integer.parseInt(ls[1].trim()));
                            }
                            br.close();
                        }
                    }
                }
            }
        }

        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            FileSplit fs = (FileSplit) context.getInputSplit();
    //        System.out.println("--------------------");
            if (!fs.getPath().getName().contains("part-r-00003")) {
                
                String[] v = value.toString().trim().split(" ");
                if (v.length >= 2) {
                    double tf =Double.parseDouble(v[1].trim());//tf值
                    String[] ss = v[0].split("_");
                    if (ss.length >= 2) {
                        String w = ss[0];
                        String id=ss[1];
                        
                        double s=tf * Math.log(cmap.get("weibo.count")/df.get(w));
                        NumberFormat nf =NumberFormat.getInstance();
                        nf.setMaximumFractionDigits(5);
                        context.write(new Text(id), new Text(w+":"+nf.format(s)));
                    }
                } else {
                    System.out.println(value.toString() + "-------------");
                }
            }
        }

    }









  • 相关阅读:
    总公司路由排错?
    黄永成-thinkphp讲解-个人博客讲解26集
    fedora配置网络
    只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs
    痛苦的事, 伤心的事, 一生只需要一次, 别折腾, 别忘痛- 人生, 软件, 所有人皆如是!
    我再也不-或许永远不-用zend studio-受够了!
    linux安装-版本选择-终极决定
    黄永成-thinkphp讲解-个人博客讲解25集
    linux的多媒体 播放 软件版权问题
    如何解决winows启动后出现grub?
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501446.html
Copyright © 2011-2022 走看看