【OLAP】trino客户端访问Trino CLI和JDBC
目录
一. Trino CLI
二. JDBC driver 连接Trino
一. Trino CLI
安装client
Trino CLI提供了一个基于终端的交互式shell。你可以通过它运行查询并与Trino服务端交互来检查其元数据。
下载地址:trino-cli-367.jar
下载客户端
cd /opt/trino-367
wget https://repo1.maven.org/maven2/io/trino/trino-cli/367/trino-cli-367.jar
mv trino-cli-367.jar ./bin/trino-cli.jar
chmod +x trino-cli.jar
查看help
[root@hdp-node3 trino-367]# ./bin/trino-cli.jar --help
Trino command line interfaceUSAGE:trino [-h] [--debug] [--disable-compression] [--external-authentication] [--ignore-errors] [--insecure][--krb5-disable-remote-service-hostname-canonicalization] [--password] [--progress] [--version] [--access-token=<token>][--catalog=<catalog>] [--client-info=<info>] [--client-request-timeout=<timeout>] [--client-tags=<tags>] [--execute=<execute>][-f=<file>] [--http-proxy=<proxy>] [--keystore-password=<password>] [--keystore-path=<path>] [--keystore-type=<type>][--krb5-config-path=<path>] [--krb5-credential-cache-path=<path>] [--krb5-keytab-path=<path>] [--krb5-principal=<principal>][--krb5-remote-service-name=<name>] [--krb5-service-principal-pattern=<pattern>] [--network-logging=<level>][--output-format=<format>] [--schema=<schema>] [--server=<server>] [--session-user=<user>] [--socks-proxy=<proxy>][--source=<source>] [--timezone=<timezone>] [--trace-token=<token>] [--truststore-password=<password>][--truststore-path=<path>] [--truststore-type=<type>] [--user=<user>] [--extra-credential=<credential>]...[--resource-estimate=<estimate>]... [--session=<session>]...OPTIONS:--access-token=<token> Access token--catalog=<catalog> Default catalog--client-info=<info> Extra information about client making query--client-request-timeout=<timeout>Client request timeout (default: 2m)--client-tags=<tags> Client tags--debug Enable debug information--disable-compression Disable compression of query results--execute=<execute> Execute specified statements and exit--external-authenticationEnable external authentication--extra-credential=<credential>Extra credentials (property can be used multiple times; format is key=value)-f, --file=<file> Execute statements from file and exit-h, --help Show this help message and exit--http-proxy=<proxy> HTTP proxy to use for server connections--ignore-errors Continue processing in batch mode when an error occurs (default is to exit immediately)--insecure Skip validation of HTTP server certificates (should only be used for debugging)--keystore-password=<password>Keystore password--keystore-path=<path> Keystore path--keystore-type=<type> Keystore type--krb5-config-path=<path>Kerberos config file path (default: /etc/krb5.conf)--krb5-credential-cache-path=<path>Kerberos credential cache path--krb5-disable-remote-service-hostname-canonicalizationDisable service hostname canonicalization using the DNS reverse lookup--krb5-keytab-path=<path>Kerberos key table path (default: /etc/krb5.keytab)--krb5-principal=<principal>Kerberos principal to be used--krb5-remote-service-name=<name>Remote peer's kerberos service name--krb5-service-principal-pattern=<pattern>Remote kerberos service principal pattern (default: ${SERVICE}@${HOST})--network-logging=<level>Network logging level [NONE, BASIC, HEADERS, BODY] (default: NONE)--output-format=<format>Output format for batch mode [ALIGNED, VERTICAL, TSV, TSV_HEADER, CSV, CSV_HEADER, CSV_UNQUOTED,CSV_HEADER_UNQUOTED, JSON, NULL] (default: CSV)--password Prompt for password--progress Show query progress in batch mode--resource-estimate=<estimate>Resource estimate (property can be used multiple times; format is key=value)--schema=<schema> Default schema--server=<server> Trino server location (default: localhost:8080)--session=<session> Session property (property can be used multiple times; format is key=value; use 'SHOW SESSION' to seeavailable properties)--session-user=<user> Username to impersonate--socks-proxy=<proxy> SOCKS proxy to use for server connections--source=<source> Name of source making query (default: trino-cli)--timezone=<timezone> Session time zone (default: Asia/Shanghai)--trace-token=<token> Trace token--truststore-password=<password>Truststore password--truststore-path=<path>Truststore path--truststore-type=<type>Truststore type--user=<user> Username (default: root)--version Print version information and exit
--debug 参数说明
./bin/trino-cli.jar \
--server https://hdp-node3:8443 \ # https 访问地址
--krb5-config-path /etc/krb5.conf \ # kerberos配置文件
--krb5-principal trino/hdp-node3@WINNER.COM \ # kerberos主体
--krb5-keytab-path /etc/security/keytabs/trino.service.keytab \ # keytab 文件路径
--krb5-remote-service-name trino \ # Trino 服务的 Kerberos 主体名称
--keystore-path /opt/trino-367/tls/trino.keystore \ # 信任库路径
--keystore-password password \ # 信任库密码
--user trino/hdp-node3@WINNER.COM \--debug
debug 使用
[root@hdp-node3 trino-367]# ./bin/trino-cli.jar \
> --server https://hdp-node3:8443 \
> --krb5-config-path /etc/krb5.conf \
> --krb5-principal trino/hdp-node3@WINNER.COM \
> --krb5-keytab-path /etc/security/keytabs/trino.service.keytab \
> --krb5-remote-service-name trino \
> --keystore-path /opt/trino-367/tls/trino.keystore \
> --keystore-password password \
> --user trino/hdp-node3@WINNER.COM \
> --catalog hive \
> --debug
trino> use winconfig;
USE
trino:winconfig> select count(*) from site_deviceprobe_map_history;_col0
---------6996123
(1 row)Query 20250811_090854_00023_yxfh3, FINISHED, 1 node
https://hdp-node3:8443/ui/query.html?20250811_090854_00023_yxfh3
Splits: 105 total, 105 done (100.00%)
CPU Time: 4.3s total, 1.62M rows/s, 324MB/s, 35% active
Per Node: 2.2 parallelism, 3.61M rows/s, 722MB/s
Parallelism: 2.2
Peak Memory: 24B
1.94 [7M rows, 1.37GB] [3.61M rows/s, 722MB/s]
查询的性能和执行详情(--debug
参数会输出更详细的统计信息):
- Query ID:
20250811_090854_00023_yxfh3
唯一标识该查询,可通过后面的 URL 在 Trino Web UI 中查看更详细的执行计划、阶段分解等。 - 状态:
FINISHED, 1 node
查询已成功完成,且仅使用了 1 个 Trino Worker 节点(若数据量更大,会自动分配更多节点并行处理)。 - Splits:
105 total, 105 done
Trino 将查询拆分为 105 个任务片段(split),每个 split 对应一部分数据,全部完成。Split 数量通常与 Hive 表的文件数量、分区数相关。 - 性能指标:
-
CPU Time: 4.3s total
:查询总消耗的CPU时间为 4.3 秒。1.62M rows/s, 324MB/s
:平均每秒处理 162 万行数据,数据吞吐量为 324MB/s。Parallelism: 2.2
:平均并行度(同时处理的 split 数量)约为 2.2。Peak Memory: 24B
:查询峰值内存消耗极小(仅 24 字节),因为count(*)
是轻量聚合,无需缓存大量数据。1.94 [7M rows, 1.37GB]
:总耗时 1.94 秒,处理了约 700 万行数据,总数据量 1.37GB。
常用查询:
SHOW CATALOGS;
show schemas from hive;
show tables from hive.database;
select * from hive.datasophon.table
--execute选项
--execute
避免使用完全限定符
#避免使用完全限定符./bin/trino-cli.jar \
--server https://hdp-node3:8443 \
--krb5-config-path /etc/krb5.conf \
--krb5-principal trino/hdp-node3@WINNER.COM \
--krb5-keytab-path /etc/security/keytabs/trino.service.keytab \
--krb5-remote-service-name trino \
--keystore-path /opt/trino-367/tls/trino.keystore \
--keystore-password password \
--user trino/hdp-node3@WINNER.COM \
--catalog hive \
--schema winconfig \
--execute "select count(*) from site_deviceprobe_map_history;"
执行sql文件
./bin/trino-cli.jar \
--server https://hdp-node3:8443 \
--krb5-config-path /etc/krb5.conf \
--krb5-principal trino/hdp-node3@WINNER.COM \
--krb5-keytab-path /etc/security/keytabs/trino.service.keytab \
--krb5-remote-service-name trino \
--keystore-path /opt/trino-367/tls/trino.keystore \
--keystore-password password \
--user trino/hdp-node3@WINNER.COM \
--catalog hive -f trino.sql
如下可以看到执行信息
写入数据到 hive外部表报错
由于写入的数据表是外表 我们需要修改 hive配置
# 如下常用的配置可以修改
# 通过 Trino执行DROP TABLE 语句删除Hive 表(包括内部表和外部表)。 默认值:false
hive.allow-drop-table=true# 通过 Trino 执行 ALTER TABLE RENAME TO 语句重命名 Hive表。 默认值:false
hive.allow-rename-table=false# 控制 Trino 读取 Hive 表数据时是否递归扫描子目录。 默认值:false
hive.recursive-directories=true # 查询 Hive 分区表时,若指定的分区不存在,是否忽略该分区。默认值:false(若查询的分区不存在,直接报错)
hive.ignore-absent-partitions=false# 允许向 Hive 外部表(非管理表)写入数据(INSERT 操作)。默认值:false
hive.non-managed-table-writes-enabled=true
insert数据正常
二. JDBC driver 连接Trino
官网文档地址: JDBC driver — Trino 476 Documentation
pom
<!-- Trino JDBC驱动 --><dependency><groupId>io.trino</groupId><artifactId>trino-jdbc</artifactId><version>367</version> <!-- 使用最新版本 --></dependency>
测试类TrinoHiveCatalogKerberosExample
package com.kangna.trino;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/***********************************************************************@Author kangll*@Date 2025/8/12 14:53 *@Desc*********************************************************************/
public class TrinoHiveCatalogKerberosExample {public static void main(String[] args) {// 1. Kerberos配置(需提前设置系统属性或在这里设置)System.setProperty("java.security.krb5.conf", "D:\\work\\krb5.conf");// System.setProperty("sun.security.krb5.debug", "true"); // 调试模式// 2. Trino JDBC连接参数String trinoUrl = "jdbc:trino://hdp-node3:8443/hive/winconfig";Properties properties = new Properties();properties.setProperty("user", "trino/hdp-node3@WINNER.COM"); // Trino用户名(需与Kerberos主体映射)properties.setProperty("KerberosRemoteServiceName", "trino");properties.setProperty("KerberosPrincipal", "trino/hdp-node3@WINNER.COM");properties.setProperty("KerberosKeytabPath", "D:\\work\\trino\\trino.service.keytab");properties.setProperty("SSL", "true");properties.setProperty("SSLTrustStorePath", "D:\\work\\trino\\trino.keystore");properties.setProperty("SSLTrustStorePassword", "password");// 3. 加载Trino JDBC驱动(0.292+)try {Class.forName("io.trino.jdbc.TrinoDriver");// 4. 建立连接try (Connection connection = DriverManager.getConnection(trinoUrl, properties);Statement statement = connection.createStatement();ResultSet rs = statement.executeQuery("select * from hive.winconfig.site_deviceprobe_map_history limit 10")) {// 5. 处理结果集int columnCount = rs.getMetaData().getColumnCount();while (rs.next()) {for (int i = 1; i <= columnCount; i++) {System.out.print(rs.getString(i) + "\t");}System.out.println();}}} catch (Exception e) {e.printStackTrace();}}
}
执行查询