请给出一条 beeline 命令,开启 CBO+Vectorized+LLAP 并打印执行计划成本。
解读
面试官通过这条命令考察三件事:
- 是否熟悉 Hive 3.x 在 Tez+LLAP 架构下的性能三板斧(CBO、Vectorized、LLAP);
- 能否在 beeline 单条命令 里一次性把开关、执行计划、成本估算全部打出来,体现“脚本化”与“可观测性”意识;
- 是否知道国内主流发行版(华为 FusionInsight、阿里云 EMR、腾讯云 TBDS、新华三 DataEngine)的 安全鉴权参数(krb5、zk-quorum、ssl-truststore)必须一次性带齐,否则现场演示会炸。
一句话:既要“功能对”,也要“能跑通”,还要“一眼看出成本”。
知识点
- CBO:Hive 3 默认已开,但显式加
set hive.cbo.enable=true可防管理员关闭。 - Vectorized:
set hive.vectorized.execution.enabled=true开启列批执行,减少 JNI 调用。 - LLAP:需通过
set hive.execution.engine=tez+set hive.llap.execution.mode=all强制走 LLAP;同时必须给出 Zookeeper quorum 让 beeline 找到 LLAP daemon。 - 成本打印:
explain formatted会输出 “Statistics” 与 “CBO cost: {rowCount, cpuCost, memoryCost…}”,比explain extended更机器友好。 - 国内安全:Kerberos 集群下必须
--principal+--keytab;非 Kerberos 的阿里云 EMR 也要--hiveconf hive.server2.zookeeper.namespace=hiveserver2才能连上 HS2 的 HA。 - 单命令:beeline 支持
-e直接执行多行 SQL,用分号分隔;所有 set 与 explain 写在一对双引号里即可。
答案
下面给出一条在 Kerberos 环境 可直接落地的示范命令,若集群无安全,可去掉 --principal 与 --keytab 两行即可。
beeline \
--principal hive/_HOST@HADOOP.COM \
--keytab /etc/security/keytabs/hive.service.keytab \
-u "jdbc:hive2://zk1:2181,zk2:2181,zk3:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2;sslTrustStore=/opt/hadoop/conf/hs2.truststore;trustStorePassword=changeit" \
--hiveconf hive.execution.engine=tez \
--hiveconf hive.llap.execution.mode=all \
--hiveconf hive.cbo.enable=true \
--hiveconf hive.vectorized.execution.enabled=true \
--hiveconf hive.vectorized.execution.reduce.enabled=true \
-e "set hive.execution.engine; set hive.llap.execution.mode; set hive.cbo.enable; set hive.vectorized.execution.enabled; explain formatted select count(*) from tpcds.store_sales where ss_sold_date_sk = 2451545;"
执行后控制台会打印:
- 四个 set 语句的确认值,证明开关已生效;
explain formatted输出的 JSON 块里包含 “CboCost” 字段,形如
"cboCost":{"rowCount":1.23E8,"cpuCost":2.45E10,"memoryCost":1.0E9},
即为面试官想看到的“成本”。
拓展思考
- 如果面试官追问“为什么开了 CBO 却没有统计信息”,可答:
“先analyze table tpcds.store_sales compute statistics for columns;若表太大,可analyze table store_sales partition(ss_sold_date_sk) compute statistics做分区级采样,防止全表扫描元数据。” - 若集群资源紧张,LLAP daemon 未拉起,explain 会回退到 “LLAP not available” 警告,此时应:
- 检查
llapstatus --name default是否存活; - 确认 Zookeeper 的
/hiveserver2节点里包含activeEndpoint; - 必要时
set hive.llap.execution.mode=auto让 Hive 自动降级,避免作业直接失败。
- 检查
- 面试现场常让你“把成本降一个数量级”,可继续补充:
“把store_sales按ss_sold_date_sk做 ORC + bloom filter + zone map,再开PREFETCH与CACHEhint,CBO 估算的rowCount会从亿级降到十万级,cpuCost 下降 95%。”