HIVE 中的 DISTRIBUTE BY 子句

DISTRIBUTE BY clause in HIVE

我无法理解此 DISTRIBUTE BY 子句在 Hive 中的作用。我知道定义是这样说的,如果我们有 DISTRIBUTE BY (city),这会将每个城市发送到不同的 reducer,但我没有得到相同的结果。让我们考虑如下数据:

假设我们有一个名为 data 的 table,其中包含列 usernameamount:

+----------+--------+
| username | amount |
+----------+--------+
| user_1   | 25     |
+----------+--------+
| user_1   | 53     |
+----------+--------+
| user_1   | 28     |
+----------+--------+
| user_1   | 50     |
+----------+--------+
| user_2   | 20     |
+----------+--------+
| user_2   | 50     |
+----------+--------+
| user_2   | 10     |
+----------+--------+
| user_2   | 5      |
+----------+--------+

现在如果我说-

SELECT username, SUM(amount) FROM data DISTRIBUTE BY (username)

不应该 运行 2 个单独的减速器吗?它仍然是 运行 单个减速器,我不知道为什么。我认为这可能与 聚类到桶 分区 有关,但我尝试了所有方法,它仍然 运行 是一个单一的减速器。谁能解释一下为什么?

DISTRIBUTE BY (city) 唯一说明的是具有相同 city 的记录将进入相同的 reducer。没有别的。

Hive uses the columns in Distribute By to distribute the rows among reducers. All rows with the same Distribute By columns will go to the same reducer

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy


OP 的问题:

Then what is the point of this DISTRIBUTE BY ? There's no guarantee that each (city) would go to a different reducer then why use it ?


有两个原因:

  1. 在配置单元 DISTRIBUTE BYSORT BYCLUSTER BY 的开头,用于以今天自动完成的方式处理数据(例如分析函数https://oren.lederman.name/?p=32)

  2. 您可能希望通过脚本 (Hive "Transform") 流式传输数据,并且您希望脚本按特定组和顺序处理数据。为此,您可以使用 DISTRIBUTE BY + SORT BYCLUSTER BY。使用 DISTRIBUTE BY 可以保证您将整个组都放在同一个减速器中。使用SORT BY可以连续获取一个组的所有记录

除了@Dudu 的回答之外,Distribute By 仅根据输入大小在 reducer 之间分配行。

用于 Hive 作业的 reducer 数量将由这个 属性 hive.exec.reducers.bytes.per.reducer 决定,这取决于输入。

从 Hive 0.14 开始,如果输入 < 256MB,将只使用一个减速器(每 256MB 输入一个减速器),除非减速器的数量被 hive.exec.reducers.maxmapred.reduce.tasks 属性。