java—计算map reduce中所有字母的出现概率

txu3uszq  于 2021-07-13  发布在  Hadoop
关注(0)|答案(1)|浏览(379)

我试图用map reduce程序计算文件中每个字母出现的概率。
我使用下面的框架进行map reduce。1个Map器Map所有字符,例如('a',1)。一个组合器,用于计算每个字符的总出现次数。1个减速机来计算平均值。
但是,我无法计算减速器中的平均值。因此,我添加了一个虚拟字符,每当Map器Map一个新字符时,它将被写入一次。
这个伪字符表示字符总数,我不知道如何在reducer中访问相同的字符,并用所有其他值除以总数。
对于ex,以下是组合器的输出。


# 10

a 2
b 2
c 2
d 4

我试过用一个减速机,它没有输出。
我特别需要知道必须写在减速机中的逻辑。

public void reduce(Text key, Iterable<DoubleWritable> values, Context context)
        throws IOException, InterruptedException {

    int wordCount = 0;
    double total = 1;
    System.out.println("In Reducer now!");
    double avg = 0;
    total = values.iterator().next().get();

    avg = values.get() / total;
    context.write(key, new DoubleWritable(avg));
}

上面的代码不会在输出上写入任何内容。
制图器

public void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {

    String s = value.toString();

    char[] arrayofChar = s.toCharArray();
    for (char ch : arrayofChar) {
        System.out.print(ch);
        if (Character.isLetter(ch)) {
            context.write(new Text(String.valueOf(ch)), new DoubleWritable(1));
            context.write(new Text("#"), new DoubleWritable(1));

        }
    }
}

合路器

public void reduce(Text key, Iterable<DoubleWritable> values, Context context)
        throws IOException, InterruptedException {

    double total = 0;
    System.out.println("In Combiner now!");
    for (DoubleWritable value : values) {
        total += value.get();
    }
    context.write(key, new DoubleWritable(total));
}
11dmarpk

11dmarpk1#

为了解决这个问题,我分别写了:

public class Test {
    public static void main(String[] arg) throws Exception{
        String s= "aabccdaaab"; 
        Averager av = s.chars().collect(Averager::new,Averager::accept, Averager::combine);
        System.out.println(av.averages());
    }
}

class Averager implements IntConsumer  {
    private int count = 0;
    private Map<Character,Integer> data = new HashMap<>();

    public  Map<Character, Double>averages() {
        Map<Character, Double> result = new HashMap<>();
        for(Character c : data.keySet()) {
            double d = (double)data.get(c)/count;
            result.put(c,d);
        }
        return result;
    }

    public void accept(int c) { 
        Character cc = new Character((char)c);
        count++;
        if(data.containsKey(cc)) {
            int i = data.get(cc);
            i++;
            data.put(cc, i);
        } else {
            data.put(cc, 1);
        }
    }

    public void combine(Averager other) {
        for(Character c : data.keySet()) {
            if(other.data.containsKey(c)) {
                int sum = data.get(c) + other.data.get(c);
                other.data.put(c, sum);
            } else {
                other.data.put(c, data.get(c));
            }
        }
        data = other.data;
    }
}

输出:

{a=0.5, b=0.2, c=0.2, d=0.1}

相关问题