Spark SQL----CLUSTER BY子句

Spark SQL----CLUSTER BY子句

一、描述

CLUSTER BY子句首先根据输入表达式对数据进行重新分区,然后对每个分区内的数据进行排序。这在语义上等同于执行一个DISTRIBUTE BY,然后执行一个SORT BY。此子句仅确保结果行在每个分区内排序,而不保证输出的总顺序。

二、语法

CLUSTER BY { expression [ , ... ] }

三、参数

  • expression
    指定产生由一个或多个值、运算符和SQL函数的组成的组合。

四、例子

CREATE TABLE person (name STRING, age INT);
INSERT INTO person VALUES
    ('Zen Hui', 25),
    ('Anil B', 18),
    ('Shone S', 16),
    ('Mike A', 25),
    ('John A', 18),
    ('Jack N', 16);

-- Reduce the number of shuffle partitions to 2 to illustrate the behavior of `CLUSTER BY`.
-- It's easier to see the clustering and sorting behavior with less number of partitions.
SET spark.sql.shuffle.partitions = 2;

-- Select the rows with no ordering. Please note that without any sort directive, the results
-- of the query is not deterministic. It's included here to show the difference in behavior
-- of a query when `CLUSTER BY` is not used vs when it's used. The query below produces rows
-- where age column is not sorted.
SELECT age, name FROM person;
+---+-------+
|age|   name|
+---+-------+
| 16|Shone S|
| 25|Zen Hui|
| 16| Jack N|
| 25| Mike A|
| 18| John A|
| 18| Anil B|
+---+-------+

-- Produces rows clustered by age. Persons with same age are clustered together.
-- In the query below, persons with age 18 and 25 are in first partition and the
-- persons with age 16 are in the second partition. The rows are sorted based
-- on age within each partition.
SELECT age, name FROM person CLUSTER BY age;
+---+-------+
|age|   name|
+---+-------+
| 18| John A|
| 18| Anil B|
| 25|Zen Hui|
| 25| Mike A|
| 16|Shone S|
| 16| Jack N|
+---+-------+

相关推荐

  1. SparkSql Join Types详解

    2024-07-19 12:58:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 12:58:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 12:58:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 12:58:01       58 阅读
  4. Python语言-面向对象

    2024-07-19 12:58:01       69 阅读

热门阅读

  1. Solana的账户模型

    2024-07-19 12:58:01       22 阅读
  2. 自然语言处理技术的发展过程

    2024-07-19 12:58:01       22 阅读
  3. pandas排名函数rank()的参数

    2024-07-19 12:58:01       19 阅读
  4. 智能结合:信息推送与供需发布机器人

    2024-07-19 12:58:01       21 阅读
  5. 2、SystemC基础语法

    2024-07-19 12:58:01       20 阅读
  6. 基于深度学习的水果识别系统

    2024-07-19 12:58:01       19 阅读
  7. C语言 条件编译

    2024-07-19 12:58:01       18 阅读
  8. 利用 PHP 解锁 1688 详情 API 接口的秘密

    2024-07-19 12:58:01       21 阅读
  9. Odoo创建一个自定义UI视图

    2024-07-19 12:58:01       23 阅读
  10. 代码随想录算法训练营第16天|二叉树part 04

    2024-07-19 12:58:01       23 阅读
  11. 华中师范大学学报人文社会科学版

    2024-07-19 12:58:01       25 阅读
  12. 动态规划练习题(2024/7/18)

    2024-07-19 12:58:01       20 阅读