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|
+---+-------+
相关推荐
码农阿豪5 分钟前
告别兼容焦虑:电科金仓 KES 如何把 Oracle 的 PL/SQL 和 JSON 业务“接住”
数据库·sql·oracle·json·金仓数据库
曹牧11 分钟前
Oracle SQL 中,& 字符
数据库·sql·oracle
java_logo28 分钟前
Apache IoTDB Docker 容器化部署指南:从入门到生产环境实践
docker·容器·apache·iotdb·iotdb部署教程·iotdb部署文档·docker部署iotdb
火山引擎开发者社区1 小时前
两大模型发布!豆包大模型日均使用量突破 50 万亿 Tokens
大数据·人工智能
小小测试开发2 小时前
实战派SQL性能优化:从语法层面攻克项目中的性能瓶颈
android·sql·性能优化
2 小时前
TIDB——TIKV——raft
数据库·分布式·tidb
Hello.Reader2 小时前
Flink SQL 的 UNLOAD MODULE 模块卸载、会话隔离与常见坑
大数据·sql·flink
禾高网络2 小时前
互联网医院系统,互联网医院系统核心功能及技术
java·大数据·人工智能·小程序
Bug.ink2 小时前
BUUCTF——WEB(2)
数据库·sql·网络安全·buuctf
AI营销实验室2 小时前
原圈科技AI CRM系统:数据闭环与可视化革新的行业突破
大数据·人工智能