澄清 sortby 与 order by 在配置单元中

Clarification on sortby vs order by in hive

我正在阅读下面的 Hive 手册,对文档中解释的细节感到困惑 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy

首先是

Hive uses the columns in SORT BY to sort the rows before feeding the rows to a reducer.

然后它说

Hive supports SORT BY which sorts the data per reducer. The difference between "order by" and "sort by" is that the former guarantees total order in the output while the latter only guarantees ordering of the rows within a reducer. If there are more than one reducer, "sort by" may give partially ordered final results.

如果它在发送到 reducer 之前已经对记录进行了排序,那么如何不保证最终输出是排序的呢?是 运行 对偶排序吗?

sort byorder by 的大部分逻辑非常相似。您可以将 order by 视为 sort by 的更受限制的情况。假设底层执行引擎是 MapReduce。

这两种情况都依赖 MR 的 Shuffle 阶段来对项目进行排序。而 shuffle 操作可以分为两部分,分别由 MR 作业的 map 端和 reduce 端处理。前者用于本地排序,后者用于合并来自不同映射器的部分结果。

当医生说:

Hive uses the columns in SORT BY to sort the rows before feeding the rows to a reducer.

这意味着行是通过洗牌阶段的映射端操作排序的。实际上,sort byorder by 都是如此。

那么两者有什么区别呢?这是减速器的并行性。 对于order by,为了得到全局有序的结果集,Hive 强制reducer 的数量为1,导致所有数据都发送到单个reducer。在这个单一的 reducer 中,shuffle 的合并部分保证所有数据都在全局排序。

而对于 sort by,则没有此类强制执行。所以减速器的数量可以是任何数量。这导致数据仅在每个 reducer 中排序。但是不能保证全局排序。当 reducer 的数量设置为 1 时,无论是显式还是隐式,sort byorder by 都具有相同的行为。