hadoop - 使用 Hive 查询计算 Hadoop 中连续记录之间的差异

标签 hadoop hive

我有一个 Hive 表,用于保存客户调用的数据。 为简单起见,假设它有 2 列,第一列包含客户 ID,第二列包含调用的时间戳(unix 时间戳)。

我可以查询此表以查找每个客户的所有调用:

SELECT * FROM mytable SORT BY customer_id, call_time;

结果是:

Customer1    timestamp11
Customer1    timestamp12
Customer1    timestamp13
Customer2    timestamp21
Customer3    timestamp31
Customer3    timestamp32
...

是否可以创建一个 Hive 查询,从第二次调用开始,为每个客户返回两次连续调用之间的时间间隔? 对于上面的例子,查询应该返回:

Customer1    timestamp12-timestamp11
Customer1    timestamp13-timestamp12
Customer3    timestamp32-timestamp31
...

我尝试采用 sql solution 的解决方案,但我受限于 Hive 的限制:it accepts subqueries only in FROMjoins must contain only equalities

谢谢。

编辑 1:

我尝试使用 Hive UDF 函数:

public class DeltaComputerUDF extends UDF {
private String previousCustomerId;
private long previousCallTime;

public String evaluate(String customerId, LongWritable callTime) {
    long callTimeValue = callTime.get();
    String timeDifference = null;

    if (customerId.equals(previousCustomerId)) {
        timeDifference = new Long(callTimeValue - previousCallTime).toString();
    }

    previousCustomerId = customerId;
    previousCallTime = callTimeValue;

    return timeDifference;
}}

并将其与名称“delta”一起使用。

但是(从日志和结果来看)它似乎是在 MAP 时间使用的。 2个问题由此产生:

首先: 在使用此函数之前,必须按客户 ID 和时间戳对表数据进行排序。查询:

 SELECT customer_id, call_time, delta(customer_id, call_time) FROM mytable DISTRIBUTE BY customer_id SORT BY customer_id, call_time;

不起作用,因为排序部分是在 REDUCE 时间执行的,在我的函数被使用很久之后。

我可以在使用该函数之前对表格数据进行排序,但我对此并不满意,因为这是我希望避免的开销。

其次:在分布式 Hadoop 配置的情况下,数据在可用的作业跟踪器之间拆分。所以我相信这个函数会有多个实例,每个映射器一个,所以有可能在 2 个映射器之间拆分相同的客户数据。在这种情况下,我会失去客户来电,这是 Not Acceptable 。

我不知道如何解决这个问题。我知道 DISTRIBUTE BY 确保所有具有特定值的数据都发送到同一个 reducer(从而确保 SORT 按预期工作),有人知道映射器是否有类似的东西吗?

接下来我打算按照 libjack 的建议使用 reduce 脚本。在其他一些 Hive 查询之间需要这种“计算”,所以我想尝试 Hive 提供的所有功能,然后再按照 Balaswamy vaddeman 的建议转向另一个工具。

编辑 2:

我开始研究自定义脚本解决方案。但是,在 Programming Hive 书(本章介绍自定义脚本)第 14 章的第一页中,我发现了以下段落:

Streaming is usually less efficient than coding the comparable UDFs or InputFormat objects. Serializing and deserializing data to pass it in and out of the pipe is relatively inefficient. It is also harder to debug the whole program in a unified manner. However, it is useful for fast prototyping and for leveraging existing code that is not written in Java. For Hive users who don’t want to write Java code, it can be a very effective approach.

很明显,就效率而言,自定义脚本并不是最佳解决方案。

但是我应该如何保留我的 UDF 功能,同时确保它在分布式 Hadoop 配置中按预期工作?我在 Language Manual UDF wiki 页面的 UDF Internals 部分找到了这个问题的答案。如果我写查询:

 SELECT customer_id, call_time, delta(customer_id, call_time) FROM (SELECT customer_id, call_time FROM mytable DISTRIBUTE BY customer_id SORT BY customer_id, call_time) t;

它在 REDUCE 时间执行,DISTRIBUTE BY 和 SORT BY 结构保证来自同一客户的所有记录都由同一个 reducer 按调用顺序处理。

所以上面的 UDF 和这个查询构造解决了我的问题。

(很抱歉没有添加链接,但我不允许这样做,因为我没有足够的声誉点数)

最佳答案

这是一个老问题,但为了以后的引用,我在这里写下另一个命题:

hive Windowing functions允许在查询中使用上一个/下一个值。

类似的代码查询可能是:

SELECT customer_id, call_time - LAG(call_time, 1, 0) OVER (PARTITION BY customer_id ORDER BY call_time) FROM mytable;

关于hadoop - 使用 Hive 查询计算 Hadoop 中连续记录之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14648201/

相关文章:

jdbc - Spark 不支持的方法

hadoop - 将 XML 数据加载到 hive 表时出错

hadoop - 有没有一种方法可以将某种缓存用于Spark中最常用的查询的结果?

hadoop - 容器内存错误:Hadoop

mysql - 如何编写多年来联合表的通用代码?

hadoop - 在 Hive 中授予权限

hadoop - Hive:向现有表添加行

Java & Pig - 是否可以将 pig 脚本的输出放入 Java 变量中?

hadoop - 如何确定映射和归约任务的数量?

macos - java_home 错误 : change version of java or hadoop-env. sh 文件?