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|
+---+-------+
相关推荐
专业工业电源打工人4 分钟前
F0505S-2WR3 适配优选 钡特电源 DF2-05S05LS|2W 隔离 DC-DC 模块电源5V转5V硬件选型参数规格解析
大数据·网络·人工智能
暖和_白开水15 分钟前
数据分析agent(九-2):es启动补充
大数据·elasticsearch·搜索引擎
海兰31 分钟前
基于 ES|QL 的 Kubernetes 监控工具箱:使用 ES|QL 进行 Kubernetes 监控,从内存压力到错误日志
大数据·elasticsearch·kubernetes
想你依然心痛43 分钟前
电子商务行业中的大数据应用案例
大数据·电子商务·用户画像·个性化推荐·库存管理·用户行为分析·供应链优化
愤怒的苹果ext1 小时前
Apache Superset 使用
apache·doris·bi·superset·ai bi
延凡科技1 小时前
告别粗放灌溉!让灌区管理智能化、节水化、精细化
大数据·人工智能·科技·物联网·信息可视化
Summer-Bright1 小时前
AI 芯片简报 07.21-07.24:NVIDIA Vera 亮剑、AMD 2nm GPU、Google 叛逃 CoWoS
大数据·人工智能·ai·自然语言处理·芯片·agi
带娃的IT创业者1 小时前
从印度私营火箭首飞成功看新兴航天架构的技术突围
大数据·架构·架构设计·系统工程·控制系统·火箭发射·商业航天
向夏威夷 梦断明暄1 小时前
基于Tez引擎的 Hive SQL 性能优化
hive·sql·性能优化
向夏威夷 梦断明暄2 小时前
从架构特点到功能缺陷,重新认识分析型分布式数据库
数据库·分布式·架构