3. Hive catalog 配置与外部表查询

Hive catalog 配置与外部表查询 学习卡片

Hive Connector 配置要点

Trino 通过 Hive connector 访问已有 Hive Metastore(HMS),元数据由 HMS 提供,数据文件仍存放在 HDFS 或对象存储(S3/OSS 等)上。配置的核心是让 Trino 能同时连通 HMS 与底层存储,并正确声明存储路径与认证方式。

在 etc/catalog/hive.properties 中至少需要以下属性:

connector.name=hive
hive.metastore.uri=thrift://hms-host:9083
hive.config.resources=/etc/hadoop/conf/core-site.xml,/etc/hadoop/conf/hdfs-site.xml
# 若使用对象存储,按需补充:
# hive.s3.endpoint=https://oss-cn-hangzhou.aliyuncs.com
# hive.s3.aws-access-key=xxx
# hive.s3.aws-secret-key=xxx

关键点:hive.metastore.uri 指向已有 HMS 的 Thrift 端口;hive.config.resources 让 Trino 读取 Hadoop 客户端配置,从而解析 hdfs:// 路径;对象存储则通过 hive.s3.* 系列属性接入。配置完成后重启 coordinator 与 worker,使 catalog 生效。

外部表创建与路径映射

Hive 外部表只登记元数据与数据路径,删除表不会删除数据文件。创建时需保证 LOCATION 指向真实存在的数据目录,且列顺序、分隔符与文件内容一致。

CREATE TABLE hive.sales.orders_ext (
  order_id     bigint,
  customer_id  bigint,
  amount       decimal(12,2),
  order_date   date
)
WITH (
  external_location = 'hdfs:///warehouse/sales/orders/',
  format = 'PARQUET'
);

-- 对象存储路径示例
-- external_location = 's3a://bucket/sales/orders/'

创建后可用 SELECT * FROM hive.sales.orders_ext LIMIT 10; 验证数据可读。若报路径无法访问,优先检查 hive.config.resources 是否包含正确的 HDFS 配置,或对象存储 endpoint/密钥是否正确。

分区裁剪验证

分区裁剪是 Hive connector 的核心性能特性:当查询条件命中分区列时,Trino 只扫描匹配分区,而非全表。验证方法是创建带分区的外部表,插入多分区数据,再用 EXPLAIN 观察实际扫描的分区。

CREATE TABLE hive.sales.orders_part (
  order_id     bigint,
  amount       decimal(12,2)
)
WITH (
  external_location = 'hdfs:///warehouse/sales/orders_part/',
  format = 'PARQUET',
  partitioned_by = ARRAY['dt']
);

-- 查询时命中分区列
EXPLAIN (TYPE DISTRIBUTED)
SELECT * FROM hive.sales.orders_part WHERE dt = '2024-06-01';

在 EXPLAIN 输出的 TableScan 节点中,若 constraint on dt 显示为 dt = '2024-06-01' 且 partitions 只列出该分区,说明裁剪生效;若列出全部分区,则裁剪未生效,需检查分区列类型是否与过滤值匹配、是否使用了函数包裹分区列(如 WHERE date(dt) = ... 会阻止裁剪)。

Trino Coordinator hive.properties catalog: hive Hive Metastore thrift://hms:9083 表元数据 / 分区信息 HDFS / 对象存储 hdfs:///warehouse/... s3a://bucket/... Thrift 元数据 路径解析 外部表 orders_part external_location + partitioned_by 列: order_id, amount, dt 注册元数据 数据文件 分区裁剪验证:EXPLAIN (TYPE DISTRIBUTED) ✅ 裁剪生效 constraint on dt = '2024-06-01' partitions: [dt=2024-06-01] ❌ 裁剪未生效 partitions: 全部分区 原因: 函数包裹分区列 / 类型不匹配 查询
验证分区裁剪时,优先使用 EXPLAIN (TYPE DISTRIBUTED) 查看 TableScan 节点的 partitions 字段。若分区列被函数包裹(如 WHERE dt = '2024-06-01' 正确,WHERE date(dt) = ... 错误),裁剪会失效。
外部表的 external_location 必须指向已存在的数据目录,且目录下文件格式需与 format 声明一致。若路径不存在或格式不匹配,查询会报错或返回空结果。删除外部表仅删除元数据,不会删除底层数据文件。