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|
+---+-------+
相关推荐
longxibo16 分钟前
【Ubuntu datasophon1.2.1 二开之八:验证实时数据入湖】
大数据·linux·clickhouse·ubuntu·linq
一只努力的微服务22 分钟前
【Calcite 系列】深入理解 Calcite 的 AggregateFilterTransposeRule
大数据·数据库·calcite·优化规则
小堃学编程24 分钟前
【项目实战】基于protobuf的发布订阅式消息队列(1)—— 准备工作
java·大数据·开发语言
lifewange27 分钟前
SQL中的聚合函数有哪些
android·数据库·sql
无忧智库28 分钟前
破局与重构:大型集团财务共享业财一体化的数字基因革命(PPT)
大数据·架构
zxm85131 小时前
UV使用及UV与Anaconda的区别
大数据·学习·机器学习·uv
贺小涛1 小时前
Git代码提交规范和踩坑排水明沟
大数据·git·elasticsearch
T06205141 小时前
【数据集】285个地级市邻接矩阵、经济地理矩阵等8个矩阵数据(2003-2023年)
大数据
逸Y 仙X1 小时前
文章十一:ElasticSearch Dynamic Template详解
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
短剑重铸之日1 小时前
《ShardingSphere解读》12 解析引擎:SQL 解析流程应该包括哪些核心阶段?(下)
数据库·后端·sql·架构·shardingsphere·分库分表