• 微博推荐 第三个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() + "-------------");
                }
            }
        }

    }









  • 相关阅读:
    关于数据源跟事件封装实例
    IOS开发基础知识--碎片27
    IOS开发基础知识--碎片26
    IOS开发基础知识--碎片25
    iOS菜单滚动联动内容区域功能实现
    IOS开发基础知识--碎片24
    iOS仿京东分类菜单之UICollectionView内容
    iOS仿京东分类菜单实例实现
    IOS开发基础知识--碎片23
    通用性好的win2003序列号: (推荐先用这个里面的)
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501446.html
Copyright © 2020-2023  润新知