猪Mapreduce连续计数字母

时间:2012-11-06 10:15:27

标签: hadoop mapreduce apache-pig

我不需要计算单词,而是需要计算字母数。 但是我使用Apache Pig版本0.8.1-cdh3u1

实现这个问题

给出以下输入:

989;850;abcccc
29;395;aabbcc

输出应该是:

989;850;a;1
989;850;b;1
989;850;c;4
29;395;a;2
29;395;b;2
29;395;c;2

以下是我的尝试:

A = LOAD 'input' using PigStorage(';') as (x:int, y:int, content:chararray);
B = foreach A generate x, y, FLATTEN(STRSPLIT(content, '(?<=.)(?=.)', 6)) as letters;
C = foreach B generate x, y, FLATTEN(TOBAG(*)) as letters;
D = foreach C generate x, y, letters.letters as letter;
E = GROUP D BY (x,y,letter);
F = foreach E generate group.x as x, group.y as y, group.letter as letter, COUNT(D.letter) as count;

可以转储A,B和C,但是“dump D”会导致“ERROR 2997:无法从支持的错误重新创建异常:java.lang.ClassCastException:java.lang.Integer无法转换为org.apache。 pig.data.Tuple“

转储C显示(尽管第三个值是一个奇怪的元组):

(989,850,a)
(989,850,b)
(989,850,c)
(989,850,c)
(989,850,c)
(989,850,c)
(29,395,a)
(29,395,a)
(29,395,b)
(29,395,b)
(29,395,c)
(29,395,c)

以下是架构:

grunt> describe A; describe B; describe C; describe D; describe E; describe F;
A: {x: int,y: int,content: chararray}
B: {x: int,y: int,letters: bytearray}
C: {x: int,y: int,letters: (x: int,y: int,letters: bytearray)}
D: {x: int,y: int,letter: bytearray}
E: {group: (x: int,y: int,letter: bytearray),D: {x: int,y: int,letter: bytearray}}
F: {x: int,y: int,letter: bytearray,count: long}

这个猪版本似乎不支持TOBAG($ 2 .. $ 8),因此TOBAG(*)也包括x和y,但可以在稍后整理出来...... 我想避免编写UDF,否则我只是直接使用Java API。

但我真的没有得到施法错误。有人可以解释一下。

3 个答案:

答案 0 :(得分:1)

我建议改为写custom UDF。快速,原始的实现看起来像这样:

package com.example;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.pig.EvalFunc;
import org.apache.pig.data.BagFactory;
import org.apache.pig.data.DataBag;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
import org.apache.pig.impl.logicalLayer.schema.Schema;

public class CharacterCount extends EvalFunc<DataBag> {

    private static final BagFactory bagFactory = BagFactory.getInstance();
    private static final TupleFactory tupleFactory = TupleFactory.getInstance();

    @Override
    public DataBag exec(Tuple input) throws IOException {
        try {

            Map<Character, Integer> charMap = new HashMap<Character, Integer>();

            DataBag result = bagFactory.newDefaultBag();
            int x = (Integer) input.get(0);
            int y = (Integer) input.get(1);
            String content = (String) input.get(2);

            for (int i = 0; i < content.length(); i++){
                char c = content.charAt(i);        
                Integer count = charMap.get(c);
                count = (count == null) ? 1 : count + 1;
                charMap.put(c, count);
            }

            for (Map.Entry<Character, Integer> entry : charMap.entrySet()) {
                Tuple res = tupleFactory.newTuple(4);
                res.set(0, x);
                res.set(1, y);
                res.set(2, String.valueOf(entry.getKey()));
                res.set(3, entry.getValue());
                result.add(res);
            }

            return result;

        } catch (Exception e) {
            throw new RuntimeException("CharacterCount error", e);
        }
    }

}

将它装在一个罐子里并执行它:

register '/home/user/test/myjar.jar';
A = LOAD '/user/hadoop/store/sample/charcount.txt' using PigStorage(';') 
      as (x:int, y:int, content:chararray);

B = foreach A generate flatten(com.example.CharacterCount(x,y,content)) 
      as (x:int, y:int, letter:chararray, count:int);

dump B;
(989,850,b,1)
(989,850,c,4)
(989,850,a,1)
(29,395,b,2)
(29,395,c,2)
(29,395,a,2)

答案 1 :(得分:0)

我没有0.8版本,但你可以试试这个:

A = LOAD 'input' using PigStorage(';') as (x:int, y:int, content:chararray);
B = foreach A generate x, y, FLATTEN(STRSPLIT(content, '(?<=.)(?=.)', 6));
C = foreach B generate $0 as x, $1 as y, FLATTEN(TOBAG(*)) as letter;
E = GROUP C BY (x,y,letter);
F = foreach E generate group.x as x, group.y as y, group.letter as letter, COUNT(C.letter) as count;

答案 2 :(得分:0)

你可以试试这个

grunt> a = load 'inputfile.txt' using PigStorage(';') as (c1:chararray, c2:chararray, c3:chararray);
grunt> b = foreach a generate c1,c2,FLATTEN(TOKENIZE(REPLACE(c3,'','^'),'^')) as split_char;
grunt> c = group b by (c1,c2,split_char);
grunt> d = foreach c generate group, COUNT(b);
grunt> dump d;

输出如下:

((29,395,a),2)
((29,395,b),2)
((29,395,c),2)
((989,850,a),1)
((989,850,b),1)
((989,850,c),4)