Spark SQL----DISTRIBUTE BY子句

Spark SQL----DISTRIBUTE BY子句

一、描述

DISTRIBUTE BY子句用于根据输入表达式对数据进行重新分区。与CLUSTER BY子句不同,这不会对每个分区内的数据进行排序。

二、语法

DISTRIBUTE 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 `DISTRIBUTE 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 result
-- of the query is not deterministic. It's included here to just contrast it with the
-- behavior of `DISTRIBUTE BY`. The query below produces rows where age columns are not
-- clustered together.
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.
-- Unlike `CLUSTER BY` clause, the rows are not sorted within a partition.
SELECT age, name FROM person DISTRIBUTE BY age;
+---+-------+
|age|   name|
+---+-------+
| 25|Zen Hui|
| 25| Mike A|
| 18| John A|
| 18| Anil B|
| 16|Shone S|
| 16| Jack N|
+---+-------+

相关推荐

  1. SparkSql Join Types详解

    2024-07-20 08:28:03       46 阅读

最近更新

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

    2024-07-20 08:28:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 08:28:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 08:28:03       45 阅读
  4. Python语言-面向对象

    2024-07-20 08:28:03       55 阅读

热门阅读

  1. AI学习指南机器学习篇-t-SNE模型应用与Python实践

    2024-07-20 08:28:03       14 阅读
  2. 使用中转API进行大模型调用及PDF解析

    2024-07-20 08:28:03       20 阅读
  3. Apache Flink

    2024-07-20 08:28:03       16 阅读
  4. 在Linux(CentOS、Ubuntu等等)中安装Erlang和Elixir

    2024-07-20 08:28:03       18 阅读
  5. Ubuntu22.04版本的YOLOv8TensorRT模型部署

    2024-07-20 08:28:03       17 阅读
  6. MQTT 报文类型

    2024-07-20 08:28:03       15 阅读
  7. 探索WebKit的CSS列表与标记:美化列表的艺术

    2024-07-20 08:28:03       20 阅读
  8. 类与对象-多态-虚析构和纯虚析构

    2024-07-20 08:28:03       19 阅读
  9. 解决前端和后端时间不一致问题的实践指南

    2024-07-20 08:28:03       15 阅读
  10. PostgreSQL如何在windows/linux开启归档

    2024-07-20 08:28:03       14 阅读