下面是我的表(MyTable)
ID TotalCount ErrorCount DT
----------------------------------------------
1345653 5 3 20120709
534140349 5 2 20120709
601806615 5 1 20120709
682527813 4 3 20120709
687612723 3 2 20120709
704318001 5 4 20120709
1345653 5 2 20120710
704318001 1 0 20120710
1120784094 3 2 20120711
因此,如果我需要使用HiveQL计算特定日期的Hive中的错误百分比,那么我将这样做 -
SELECT 100 * sum(ErrorCount*1.0) / sum(TotalCount) FROM MyTable
where dt = '20120709';
但我需要使用Java MapReduce
做同样的事情。我们有什么方法可以使用MapReduce in Java code
做同样的事情。首先,每当我们用Java编写任何MapReduce作业时,我都会感到困惑,我们读取了该日期分区的相应文件?或者我们读了桌子?
更新: - 下面是将包含上述方案的表名
create table lipy
( buyer_id bigint,
total_chkout bigint,
total_errpds bigint
)
partitioned by (dt string)
row format delimited fields terminated by '\t'
stored as sequencefile
location '/apps/hdmi-technology/lipy'
;
答案 0 :(得分:1)
这很简单 - 让我试一下伪代码。
SELECT 100 * sum(ErrorCount*1.0) / sum(TotalCount) FROM MyTable
where dt = '20120709';
地图阶段:
dt
列是否等于20120709
Key/Value
:-1 / totalcount
和0 / error counter
减少阶段: (您获得密钥-1的总计数,错误计数器为密钥0)
有几点需要注意:
<IntWritable, IntWritable>
或<IntWritable,LongWritable>
。我相信这是需要注意的一切,这里很早就没有咖啡,所以如果你发现问题,请随时告诉我;)
答案 1 :(得分:0)
您可以这样做,但实施将取决于:
如何格式化数据 - 行格式,分隔符,...
http://hive.apache.org/docs/r0.9.0/language_manual/data-manipulation-statements.html
您希望如何执行MapReduce。一个非常直接的选择是运行你的 Java MapReduce代码作为重用HiveQL函数的用户定义函数(UDF):
https://cwiki.apache.org/Hive/tutorial.html#Tutorial-Custommap%252Freducescripts
或者只是在HDFS中对Hive表数据运行自定义mapreduce。