Spark SQL----DISTRIBUTE BY子句

Spark SQL----DISTRIBUTE BY子句

一、描述

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

二、语法

sql 复制代码
DISTRIBUTE BY { expression [ , ... ] }

三、参数

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

四、例子

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|
+---+-------+
相关推荐
许彰午16 小时前
CacheSQL(三):双 HTTP 引擎与 SQL 查询——接口抽象的价值
java·数据库·sql·缓存
空中海18 小时前
Kafka Streams、Connect 与生态
分布式·kafka·linq
小王毕业啦1 天前
2005-2024年 省级-总抚养比、儿童抚养比、老年人抚养比数据(xlsx)
大数据·人工智能·数据挖掘·数据分析·社科数据·实证分析·经管数据
2501_927283581 天前
荣联汇智助力天津艺虹打造“软硬一体”智慧工厂,全流程自动化引领印刷包装行业数智变革
大数据·运维·数据仓库·人工智能·低代码·自动化
还是奇怪1 天前
AI 提示词工程入门:用好的语言与模型高效对话
大数据·人工智能·语言模型·自然语言处理·transformer
Data_Journal1 天前
如何使用cURL更改User Agent
大数据·服务器·前端·javascript·数据库
weixin_446260851 天前
城市智能化的底层基石:基于腾讯地图服务生态的移动定位与导航架构指引
大数据·人工智能·架构
qq_283720051 天前
Vibe Coding 氛围编程入门教程:AI 时代的全新开发范式(零基础到实战)
大数据·人工智能
Volunteer Technology1 天前
ES并发控制
大数据·elasticsearch·搜索引擎
小飞象—木兮1 天前
《销售数据分析标准实践手册》:核心内涵与关键指标、落地销售数据分析的全流程···(附相关材料下载)
大数据·人工智能·数据挖掘·数据分析