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

}









posted @ 2016-06-05 21:42  yuerspring  阅读(181)  评论(0编辑  收藏  举报