sum,avg,在pig中不起作用

qncylg1j  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(335)

我正在pig中使用以下代码分析群集用户日志文件:

t_data = load 'log_flies/*' using PigStorage(',');
    A = foreach t_data generate $0 as (jobid:int), 
$1 as (indexid:int), $2 as (clusterid:int), $6 as (user:chararray),
 $7 as (stat:chararray), $13 as (queue:chararray), $32 as (projectName:chararray), $52 as (cpu_used:float), $55 as (efficiency:float),  $59 as (numThreads:int), 

$61 as (numNodes:int),  $62 as (numCPU:int),$72 as (comTime:int),
 $73 as (penTime:int),  $75 as (runTime:int), $52/($62*$75) as (allEff: float), SUBSTRING($68, 0, 11) as (endTime: chararray);
    ---describe A;
    A = foreach A generate jobid, indexid, clusterid, user, cpu_used, numThreads, runTime, allEff, endTime;
    B = group A by user;
    f_data = foreach B {
           grp = group;
           count = COUNT(A);
          avg = AVG(A.cpu_used);
          generate FLATTEN(grp), count, avg;
       };
    f_data = limit f_data 10;
    dump f_data;

代码适用于 group and COUNT 但当我包括平均值和总和时,它会显示错误:
error org.apache.pig.tools.grunt.grunt-错误1066:无法打开alias f\ U数据的迭代器
我检查了数据类型。一切都很好。你对我错过的地方有什么建议吗?。事先谢谢你的帮助。

djmepvbi

djmepvbi1#

你的Pig全是错误
在=;的两边不要使用相同的别名;
使用pigloader()作为(适当地提及您的模式);

A = foreach A generate jobid, indexid, clusterid, user, cpu_used, numThreads, runTime, allEff, endTime;

将其更改为f=foreach a generate jobid,indexid,clusterid,user,cpu\u used,numthreads,runtime,alleff,endtime;
f\数据=限制f\数据10;用其他名称更改左fèU数据。
别让你的生活复杂化了。调试pigscript的一般规则
在本地模式下运行
在everyline之后转储
写了一个样本Pig来模仿你的Pig:(工作)
t_data=使用pigstorage(',')作为加载“./file”(jobid:int,中央处理器_used:float);

C = foreach t_data generate jobid, cpu_used ;
        B = group C by jobid ;
        f_data = foreach B {
               count = COUNT(C);
              sum = SUM(C.cpu_used);
              avg = AVG(C.cpu_used);
              generate FLATTEN(group), count,sum,avg;
           };
        never_f_data = limit f_data 10;

    dump never_f_data;
lp0sw83n

lp0sw83n2#

这是一个语法错误。阅读http://chimera.labs.oreilly.com/books/1234000001811/ch06.html#more_on_foreach (部分:嵌套foreach)了解详细信息。
Pig手稿

A = LOAD 'a.csv' USING  PigStorage(',') AS (user:chararray,    cpu_used:float);
   B = GROUP A BY user;
   C = FOREACH B {
    cpu_used_bag = A.cpu_used;
    GENERATE group AS user, AVG(cpu_used_bag) AS avg_cpu_used, SUM(cpu_used_bag) AS total_cpu_used;
    };

输入:a.csv

a,3
a,4
b,5

输出:

(a,3.5,7.0)
(b,5.0,5.0)

相关问题