尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
【trino权威指南】使用trino详解:trino client安装、查询sql、DBeaver连接trino、java通过JDBC连接trino
文章目录一. Trino CLI1. 安装client2. 使用client执行sql二. JDBC driver 连接Trino1. 通过DBeaver用户界面连接2. JDBC Driver in java2.1. 环境配置2.2. 注册和配置driver2.3. 连接参数2.4. 查询例子一. Trino CLI1. 安装clientTrino CLI提供了一个基于终端的交互式shell。你可以通过它运行查询并与Trino服务端交互来检查其元数据。下载地址trino-cli-434-executable.jar# 下载wget-Otrino https://repo1.maven.org/maven2/io/trino/trino-cli/434/trino-cli-434-executable.jar# 添加可执行权限、并将其添加到PATH下方便使用chmodx trinomvtrino ~/binexportPATH~/bin/:$PATH2. 使用client执行sql./trino http://trino.example.com:8080# 帮助文档trinohelpSupported commands: QUIT EXIT CLEAR EXPLAIN[(option[,...])]queryoptions: FORMAT{TEXT|GRAPHVIZ|JSON}TYPE{LOGICAL|DISTRIBUTED|VALIDATE|IO}DESCRIBEtableSHOWCOLUMNSFROMtableSHOW FUNCTIONS SHOW CATALOGS[LIKEpattern]SHOW SCHEMAS[FROMcatalog][LIKEpattern]SHOW TABLES[FROMschema][LIKEpattern]USE[catalog.]schema运行sqltrinoselect* frommysql-1.flinkx_test.flinkx_hdfs_log -;id|ip|stamp|url|refer|useragent|pid|uid|mid|insert_time|create_time|create_date ---------------------------------------------------------------------------------------------------------------------------1|NULL|NULL|NULL|aaaa|NULL|20221212|NULL|NULL|2023-01-3116:32:09|NULL|2023-01-111|NULL|NULL|NULL|aaaa|NULL|20221212|NULL|NULL|2023-01-3118:37:20|NULL|2023-01-11...(18rows)Query 20231206_032803_00000_78prf, FINISHED,1nodeSplits:1total,1done(100.00%)0.67[18rows, 0B][26rows/s, 0B/s]连接trino时可以设置默认的catalog某个连接实例和schema数据库这样可以直接查询表。./trino http://localhost:8080/mysql-1/flinkx_test trino:flinkx_testshow tables;Table ----------------------------------------------------- aaa aaa_csv111_1 aaa_csv111_2...USE默认的catalog和schema直接查询此库下的表trinoUSE tpch.tiny;USE trino:tinydebug功能./trino http://localhost:8080/mysql-1/flinkx_test--debugtrino:flinkx_testselect* frommysql-1.flinkx_test.flinkx_hdfs_log -;id|ip|stamp|url|refer|useragent|pid|uid|mid|insert_time|create_time|create_date ---------------------------------------------------------------------------------------------------------------------------1|NULL|NULL|NULL|aaaa|NULL|20221212|NULL|NULL|2023-01-3116:32:09|NULL|2023-01-111|NULL|NULL|NULL|aaaa|NULL|20221212|NULL|NULL|2023-01-3118:37:20|NULL|2023-01-11... http://localhost:8080/ui/query.html?20231206_033726_00007_78prf Splits:1total,1done(100.00%)CPU Time:0.0s total, 3K rows/s, 0B/s,4% active Per Node:0.0parallelism,30rows/s, 0B/s Parallelism:0.0Peak Memory: 64B0.60[18rows, 0B][30rows/s, 0B/s]--execute选项./trino http://localhost:8080/mysql-1/flinkx_test--executeselect * from mysql-1.flinkx_test.flinkx_hdfs_log limit 3;1,,,,aaaa,,20221212,,,2023-01-31 16:32:09,,2023-01-111,,,,aaaa,,20221212,,,2023-01-31 18:37:20,,2023-01-11,124.239.193.194,,,,,h_home,,,2023-02-10 10:52:01,,避免使用完全限定符# 使用--catalog和--schema选项来./trino http://localhost:8080--catalogmysql-1--schemaflinkx_test\--executeselect * from flinkx_hdfs_log limit 3;# 通过http设限定符./trino http://localhost:8080/mysql-1/flinkx_test\--executeselect * from flinkx_hdfs_log limit 3;执行多个sqltrino http://localhost:8080\--executeuse mysql-1.flinkx_test;select * from flinkx_hdfs_log limit 3;执行sql文件vimtrino.sql usemysql-1.flinkx_test;select* from flinkx_hdfs_log limit3;./trino http://localhost:8080-ftrino.sql输出格式Trino CLI提供了--output-format选项来控制如何在非交互模式下显示输出可用的选项有ALIGNED、VERTICAL、TSV、TSV_HEADER、CSV、CSV_HEADER、CSV_UNQUOTED、CSV_HEADER_UNQUOTED、JSON和NULL默认值是CSV。忽略错误Trino CLI提供了--ignore-error选项来忽略执行文件中的查询时遇到的任何错误。默认行为是在遇到第一个错误时终止执行脚本。二. JDBC driver 连接Trino任何Java应用程序都可以通过Java数据库连接(JDBC)驱动连接到Trino。通过JDBC驱动所有这些应用程序都可以使用Trino。Trino的JDBC驱动允许你连接到Trino并使用SQL语句与Trino交互。如果你熟悉JDBC驱动的不同实现就知道Trino的JDBC驱动是Type 4驱动这仅仅意味着它直接与Trino原生协议通信。1. 通过DBeaver用户界面连接查看表时出现如下错误需要声明用户默认输入admin、密码为空重新连接可以看到注册的catalog了可以像之前使用DBeaver查看表了。2. JDBC Driver in java2.1. 环境配置需要java 8 或更高所有通过JDBC连接Trino的用户必须在system.jdbcschema中赋予权限maven依赖dependencygroupIdio.trino/groupIdartifactIdtrino-jdbc/artifactIdversion434/version/dependency其他版本地址在A list of all available versions can be found in the Maven Central Repository.客户端使用http协议和 Trino client REST API 和Trino进行通讯。2.2. 注册和配置driverJDBC URL支持的格式jdbc:trino://host:port jdbc:trino://host:port/catalog jdbc:trino://host:port/catalog/schema举例jdbc:trino://example.net:8080/hive/sales连接example.net上的trino8080端口并明确了catalog和schema。如果trino的驱动识别不到则可以显式声明io.trino.jdbc.TrinoDriver。2.3. 连接参数声明方式有如下两种// propertiesStringurljdbc:trino://example.net:8080/hive/sales;PropertiespropertiesnewProperties();properties.setProperty(user,test);properties.setProperty(password,secret);properties.setProperty(SSL,true);ConnectionconnectionDriverManager.getConnection(url,properties);// URL parametersStringurljdbc:trino://example.net:8443/hive/sales?usertestpasswordsecretSSLtrue;ConnectionconnectionDriverManager.getConnection(url);当然这两种方式我们可以混用有些参数通过properties设置有些在url中设置。完整的参数见Parameter reference2.4. 查询例子packagecom.gao;importjava.sql.*;publicclassTrinoQueryExample{publicstaticvoidmain(String[]args){// Trino数据库连接信息StringtrinoUrljdbc:trino://xxx:8080;StringtrinoUseradmin;StringtrinoPassword;// SQL查询语句StringsqlQueryselect * from \mysql-1\.flinkx_test.flinkx_hdfs_log limit 3;try{Class.forName(io.trino.jdbc.TrinoDriver);// 建立Trino数据库连接ConnectionconnectionDriverManager.getConnection(trinoUrl,trinoUser,trinoPassword);// 创建Statement对象Statementstatementconnection.createStatement();// 执行查询ResultSetresultSetstatement.executeQuery(sqlQuery);// 处理查询结果while(resultSet.next()){// 根据查询结果的列名或索引获取数据IntegeridresultSet.getInt(id);Timestampcreate_timeresultSet.getTimestamp(create_time);// 处理获取的数据System.out.println(id: id, create_time: create_time);}// 关闭资源resultSet.close();statement.close();connection.close();}catch(SQLExceptione){e.printStackTrace();}catch(ClassNotFoundExceptione){thrownewRuntimeException(e);}}}id:1,create_time:nullid:1,create_time:nullid:0,create_time:null参考《trino权威指南原书第二版》trino官网https://trino.io/docs/current/client.html
RELATED

相关推荐

【工具】hadoop国内下载镜像

【工具】hadoop国内下载镜像

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/3 9:11:19
027-时间同步服务(chronydNTP)

027-时间同步服务(chronydNTP)

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/3 9:11:19
基于STM32与HX711的智能体重秤设计:从原理图到代码的完整实战

基于STM32与HX711的智能体重秤设计:从原理图到代码的完整实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/3 9:06:11
MORE NEWS

更多资讯

📰

【Agent-01】理解 Agent:目标、执行与证据

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

给 QGIS 装个 AI,让它自己画地图

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

4步装出macOS虚拟机:从官方镜像到可启动macOS虚拟机ISO的完整流程

4步装出macOS虚拟机:从官方镜像到可启动macOS虚拟机ISO的完整流程 【免费下载链接】macos-virtualbox Push-button installer of macOS Catalina, Mojave, and High Sierra guests in Virtualbox on x86 CPUs for Windows, Linux, and macOS 项目地址: https://gi…

📰

AiSOC Syslog 监听器实战:用 UDP/TCP 直收 RFC 5424、RFC 3164、CEF 与 LEEF

人工智能AI Agent网络安全后端前端 【免费下载链接】AiSOC Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, M…

📰

一键安装fcitx5皮肤:fcitx5-themes-candlelight最简单install.sh安装教程

【免费下载链接】fcitx5-themes-candlelight fcitx5的简约风格皮肤——烛光。 项目地址: https://gitcode.com/gh_mirrors/fc/fcitx5-themes-candlelight 点击查看 免费下载 fcitx5-themes-candlelight(烛光)是专为 fcitx5 输入法打造的一套…

📰

如何使用BBDown:哔哩哔哩视频下载器完整使用指南

如何使用BBDown:哔哩哔哩视频下载器完整使用指南 【免费下载链接】BBDown Bilibili Downloader. 一个命令行式哔哩哔哩下载器. 项目地址: https://gitcode.com/gh_mirrors/bb/BBDown BBDown 是一个基于 .NET 的命令行式哔哩哔哩视频下载器,覆盖普…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬