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() + "-------------");
                }
            }
        }

    }









  • 相关阅读:
    HDOJ 1207 汉诺塔II
    [转]写代码的小女孩
    POJ Subway tree systems
    HDOJ 3555 Bomb (数位DP)
    POJ 1636 Prison rearrangement (DP)
    POJ 1015 Jury Compromise (DP)
    UVA 10003
    UVA 103 Stacking Boxes
    HDOJ 3530 Subsequence
    第三百六十二、三天 how can I 坚持
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501446.html
Copyright © 2011-2022 走看看