我希望在 Pig 中实现以下功能。我有一组这样的示例记录。
请注意,EffectiveDate 列有时为空,并且对于同一 CustomerID 也不同。
现在,作为输出,我希望每个 CustomerID 有一个记录,其中 EffectiveDate 是最大值。因此,对于上面的示例,我希望记录突出显示如下所示。
我目前使用 PIG 的方式是这样的:
customerdata = LOAD 'customerdata' AS (CustomerID:chararray, CustomerName:chararray, Age:int, Gender:chararray, EffectiveDate:chararray);
--Group customer data by CustomerID
customerdata_grpd = GROUP customerdata BY CustomerID;
--From the grouped data, generate one record per CustomerID that has the maximum EffectiveDate.
customerdata_maxdate = FOREACH customerdata_grpd GENERATE group as CustID, MAX(customerdata.EffectiveDate) as MaxDate;
--Join the above with the original data so that we get the other details like CustomerName, Age etc.
joinwithoriginal = JOIN customerdata by (CustomerID, EffectiveDate), customerdata_maxdate by (CustID, MaxDate);
finaloutput = FOREACH joinwithoriginal GENERATE customerdata::CustomerID as CustomerID, CustomerName as CustomerName, Age as Age, Gender as gender, EffectiveDate as EffectiveDate;
我基本上是对原始数据进行分组,以找到具有最大 EffectiveDate 的记录。然后,我再次将这些“分组”记录与原始数据集连接起来,以获得具有最大生效日期的相同记录,但这次我还将获得其他数据,如 CustomerName、Age 和 Gender。这个数据集很大,所以这种方法要花很多时间。有没有更好的方法?
最佳答案
输入:
1,John,28,M,1-Jan-15
1,John,28,M,1-Feb-15
1,John,28,M,
1,John,28,M,1-Mar-14
2,Jane,25,F,5-Mar-14
2,Jane,25,F,5-Jun-15
2,Jane,25,F,3-Feb-14
pig 脚本:
customer_data = LOAD 'customer_data.csv' USING PigStorage(',') AS (id:int,name:chararray,age:int,gender:chararray,effective_date:chararray);
customer_data_fmt = FOREACH customer_data GENERATE id..gender,ToDate(effective_date,'dd-MMM-yy') AS date, effective_date;
customer_data_grp_id = GROUP customer_data_fmt BY id;
req_data = FOREACH customer_data_grp_id {
customer_data_ordered = ORDER customer_data_fmt BY date DESC;
req_customer_data = LIMIT customer_data_ordered 1;
GENERATE FLATTEN(req_customer_data.id) AS id,
FLATTEN(req_customer_data.name) AS name,
FLATTEN(req_customer_data.gender) AS gender,
FLATTEN(req_customer_data.effective_date) AS effective_date;
};
输出:
(1,John,M,1-Feb-15)
(2,Jane,F,5-Jun-15)
关于hadoop - Apache PIG - 分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898336/
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
1.1.1 YARN的介绍 为克服Hadoop1.0中HDFS和MapReduce存在的各种问题⽽提出的,针对Hadoop1.0中的MapReduce在扩展性和多框架⽀持⽅⾯的不⾜,提出了全新的资源管理框架YARN. ApacheYARN(YetanotherResourceNegotiator的缩写)是Hadoop集群的资源管理系统,负责为计算程序提供服务器计算资源,相当于⼀个分布式的操作系统平台,⽽MapReduce等计算程序则相当于运⾏于操作系统之上的应⽤程序。 YARN被引⼊Hadoop2,最初是为了改善MapReduce的实现,但是因为具有⾜够的通⽤性,同样可以⽀持其他的分布式计算模
假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解
我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_
如果至少有两个相邻的数字相同,格式为,我需要打包.这是我的输入:[2,2,2,3,4,3,3,2,4,4,5]以及预期的输出:"2:3,3,4,3:2,2,4:2,5"到目前为止我试过:a=[1,1,1,2,2,3,2,3,4,4,5]a.each_cons(2).any?do|s,t|ifs==t如果相等,也许可以尝试计数器,但那是行不通的。 最佳答案 您可以使用Enumerable#chunk_while(如果你使用的是Ruby>=2.3):a.chunk_while{|a,b|a==b}.flat_map{|chunk|chu
我有一个文件,每一行都有数字:010110101311010113114311010431420我想要一个包含每个数字出现次数的散列,在这种情况下:{0101=>2,1010=>2,1311=>2,431=>2,420=>1}我该怎么做? 最佳答案 简单的一行代码,给定一个数组items:items.inject(Hash.new(0)){|hash,item|hash[item]+=1;hash}工作原理:Hash.new(0)创建一个新的Hash,其中访问未定义的键返回0。inject(foo)使用给定的block遍历数组。对于
对于按日期分组,我使用了group_by方法。Example:Product.all.group_by{|d|d.created_at}#returnHash但是kaminari不支持Hash。我使用Mongoid,我需要通过页面导航(kaminari)按日期分组。怎么做到的? 最佳答案 Kaminari只支持数组分页例如Kaminari.paginate_array(an_array).page(1).per(10)group_by不是mongoid方法,它是Array的方法,它是对内存中的所有数据进行分组。为了使用mongoid
我有一个字符串数组,数量不多(可能几百个)但通常很长(几百个字符)。这些字符串通常是无意义的,并且彼此不同。但是在一组这样的字符串中,可能300个中有5个具有很大的相似性。事实上,它们是相同的字符串,不同的是格式、标点符号和一些单词..我怎样才能算出那组字符串?顺便说一句,我正在用ruby编写,但如果没有别的,伪代码算法就可以了。谢谢 最佳答案 假设您不担心每个单词的拼写错误或其他错误,您可以执行以下操作:构建一个倒排索引,它基本上是一个以单词为键的散列,指向包含该单词的字符串的指针列表(如何处理重复出现由您决定)。要确定与给定
简单地说,我如何使用Sequel执行此查询?selecta.id,count(t.id)fromalbumsarightjointrackstont.album_id=a.idgroupbya.id 最佳答案 DB[:albums___a].right_join(:tracks___t,:album_id=>:id).select_group(:a__id).select_more{count(:t__id)} 关于ruby-续集:如何使用分组和计数,我们在StackOverflow上找
我想按多个键分组:订单、idx、帐户等。下面的代码是RubyonRails-HashofArrays,groupbyandsumbycolumnname的修改版本。.谁能推荐一种对多个键进行分组并对多个值求和的方法?例如,在下面的代码中,我只对“订单”进行分组。我想对订单、idx和帐户进行分组。group_hashessome_array,["order","idx","account"]["money","amt"]对比group_hashessome_array,"order","money","amt"代码:some_array=[{"idx"=>"1234","account"