【trino权威指南】使用trino详解:trino client安装、查询sql、DBeaver连接trino、java通过JDBC连接trino

文章目录

  • 一. Trino CLI
    • 1. 安装client
    • 2. 使用client执行sql
  • 二. JDBC driver 连接Trino
    • 1. 通过DBeaver用户界面连接
    • 2. JDBC Driver in java
    • 2.1. 环境配置
    • 2.2. 注册和配置driver
    • 2.3. 连接参数
    • 2.4. 查询例子

一. Trino CLI

1. 安装client

Trino CLI提供了一个基于终端的交互式shell。你可以通过它运行查询并与Trino服务端交互来检查其元数据

下载地址:trino-cli-434-executable.jar

# 下载wget -O trino https://repo1.maven.org/maven2/io/trino/trino-cli/434/trino-cli-434-executable.jar# 添加可执行权限、并将其添加到PATH下,方便使用
chmod +x trino
mv trino ~/bin
export PATH=~/bin/:$PATH

 

2. 使用client执行sql

./trino http://trino.example.com:8080# 帮助文档
trino> helpSupported commands:
QUIT
EXIT
CLEAR
EXPLAIN [ ( option [, ...] ) ] <query>options: FORMAT { TEXT | GRAPHVIZ | JSON }TYPE { LOGICAL | DISTRIBUTED | VALIDATE | IO }
DESCRIBE <table>
SHOW COLUMNS FROM <table>
SHOW FUNCTIONS
SHOW CATALOGS [LIKE <pattern>]
SHOW SCHEMAS [FROM <catalog>] [LIKE <pattern>]
SHOW TABLES [FROM <schema>] [LIKE <pattern>]
USE [<catalog>.]<schema>

 
运行sql

trino> select *  from "mysql-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-31 16:32:09 | NULL        | 2023-01-111 | NULL            | NULL  | NULL | aaaa  | NULL      | 20221212 | NULL | NULL | 2023-01-31 18:37:20 | NULL        | 2023-01-11  ...
(18 rows)Query 20231206_032803_00000_78prf, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
0.67 [18 rows, 0B] [26 rows/s, 0B/s]

 
连接trino时可以设置默认的catalog(某个连接实例)和schema(数据库),这样可以直接查询表。

 ./trino http://localhost:8080/mysql-1/flinkx_test
trino:flinkx_test> show tables;Table
-----------------------------------------------------aaaaaa_csv111_1aaa_csv111_2     
...

 

USE默认的catalog和schema,直接查询此库下的表

trino> USE tpch.tiny;
USE
trino:tiny>

 

debug功能

./trino http://localhost:8080/mysql-1/flinkx_test --debug trino:flinkx_test> select *  from "mysql-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-31 16:32:09 | NULL        | 2023-01-111 | NULL            | NULL  | NULL | aaaa  | NULL      | 20221212 | NULL | NULL | 2023-01-31 18:37:20 | NULL        | 2023-01-11  
...http://localhost:8080/ui/query.html?20231206_033726_00007_78prf
Splits: 1 total, 1 done (100.00%)
CPU Time: 0.0s total,    3K rows/s,     0B/s, 4% active
Per Node: 0.0 parallelism,    30 rows/s,     0B/s
Parallelism: 0.0
Peak Memory: 64B
0.60 [18 rows, 0B] [30 rows/s, 0B/s]

 
--execute选项

 ./trino http://localhost:8080/mysql-1/flinkx_test --execute 'select *  from "mysql-1".flinkx_test.flinkx_hdfs_log limit 3;'"1","","","","aaaa","","20221212","","","2023-01-31 16:32:09","","2023-01-11"
"1","","","","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 --catalog "mysql-1" --schema flinkx_test \--execute 'select *  from flinkx_hdfs_log limit 3;'# 通过http设限定符
./trino http://localhost:8080/mysql-1/flinkx_test  \--execute 'select *  from flinkx_hdfs_log limit 3;'

 
执行多个sql

trino http://localhost:8080  \--execute 'use "mysql-1".flinkx_test;select *  from flinkx_hdfs_log limit 3;'

 
执行sql文件

vim trino.sql
use "mysql-1".flinkx_test;
select *  from flinkx_hdfs_log limit 3;./trino http://localhost:8080  -f trino.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 java

2.1. 环境配置

需要java 8 或更高
所有通过JDBC连接Trino的用户,必须在system.jdbc schema中赋予权限

maven依赖:

<dependency><groupId>io.trino</groupId><artifactId>trino-jdbc</artifactId><version>434</version>
</dependency>

其他版本地址在:

A list of all available versions can be found in the Maven Central Repository.

客户端使用http协议和 Trino client REST API 和Trino进行通讯。

 

2.2. 注册和配置driver

JDBC 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上的trino(8080端口),并明确了catalog和schema。

如果trino的驱动识别不到,则可以显式声明:io.trino.jdbc.TrinoDriver

 

2.3. 连接参数

声明方式有如下两种

// properties
String url = "jdbc:trino://example.net:8080/hive/sales";
Properties properties = new Properties();
properties.setProperty("user", "test");
properties.setProperty("password", "secret");
properties.setProperty("SSL", "true");
Connection connection = DriverManager.getConnection(url, properties);// URL parameters
String url = "jdbc:trino://example.net:8443/hive/sales?user=test&password=secret&SSL=true";
Connection connection = DriverManager.getConnection(url);

当然这两种方式我们可以混用,有些参数通过properties设置,有些在url中设置。

完整的参数见:Parameter reference

 

2.4. 查询例子

package com.gao;import java.sql.*;public class TrinoQueryExample {public static void main(String[] args) {// Trino数据库连接信息String trinoUrl = "jdbc:trino://xxx:8080";String trinoUser = "admin";String trinoPassword = "";// SQL查询语句String sqlQuery = "select *  from  \"mysql-1\".flinkx_test.flinkx_hdfs_log limit 3";try {Class.forName("io.trino.jdbc.TrinoDriver");// 建立Trino数据库连接Connection connection = DriverManager.getConnection(trinoUrl, trinoUser, trinoPassword);// 创建Statement对象Statement statement = connection.createStatement();// 执行查询ResultSet resultSet = statement.executeQuery(sqlQuery);// 处理查询结果while (resultSet.next()) {// 根据查询结果的列名或索引获取数据Integer id = resultSet.getInt("id");Timestamp create_time = resultSet.getTimestamp("create_time");// 处理获取的数据System.out.println("id: " + id + ", create_time: " + create_time);}// 关闭资源resultSet.close();statement.close();connection.close();} catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}
}id: 1, create_time: null
id: 1, create_time: null
id: 0, create_time: null

 
 
参考:
《trino权威指南:原书第二版》
trino官网:https://trino.io/docs/current/client.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/263061.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大模型专题报告:AI大模型如何赋能智能座舱

今天分享的AI系列深度研究报告&#xff1a;《大模型专题报告&#xff1a;AI大模型如何赋能智能座舱》。 &#xff08;报告出品方&#xff1a;华泰证券&#xff09; 报告共计&#xff1a;32页 智能座舱: 4.0至5.0&#xff0c;从多模态到大模型 问题 1: 座舱智能化演进处于什么…

【MySQL进阶】索引使用

一、索引使用 1.验证索引效率 tb_sku 这张表中准备了 1000w 的记录。 我用夸克网盘分享了「1000w的模拟数据」链接&#xff1a;https://pan.quark.cn/s/15cf665202b2 这张表中id为主键&#xff0c;有主键索引&#xff0c;而其他字段是没有建立索引的。 我们先来查询其中的…

Grounding DINO、TAG2TEXT、RAM、RAM++论文解读

提示&#xff1a;Grounding DINO、TAG2TEXT、RAM、RAM论文解读 文章目录 前言一、Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection1、摘要2、背景3、部分文献翻译4、贡献5、模型结构解读a.模型整体结构b.特征增强结构c.解码结构 6、实…

Java线程概念详解

线程 概念 1.程序:未解决某种问题,使用计算机语言编写的一些列指令(代码)的集合 2.进程:正在运行的程序(被加载到内存中),是操作系统进行资源分配的最小单位 3.线程:进程可以进一步细化为线程(比进程更小)且线程是隶属于进程的,是操作系统执行的最小的执行单元 也是cpu进行任…

uniapp实战 —— 分类导航【详解】

效果预览 组件封装 src\pages\index\components\CategoryPanel.vue <script setup lang"ts"> import type { CategoryItem } from /types/index defineProps<{list: CategoryItem[] }>() </script><template><view class"category&…

法律服务网站建设效果如何

律师事务所及法律知识咨询机构等往往是众多人群需求的服务&#xff0c;服务多样化及内容多元化&#xff0c;市场中也有大量品牌&#xff0c;在实际消费服务中大多以本地事务所为主&#xff0c;而线上咨询服务则一般没有区域限制&#xff0c;同行增多及人们知识获取渠道增加&…

Flutter笔记:滑块及其实现分析1

Flutter笔记 滑块分析1 作者&#xff1a;李俊才 &#xff08;jcLee95&#xff09;&#xff1a;https://blog.csdn.net/qq_28550263 邮箱 &#xff1a;291148484163.com 本文地址&#xff1a;https://blog.csdn.net/qq_28550263/article/details/134900784 本文从设计角度&#…

【计算机网络实验】实验三 IP网络规划与路由设计(头歌)

目录 一、知识点 二、实验任务 三、头歌测试 一、知识点 IP子网掩码的两种表示方法 32位IP子网掩码&#xff0c;特点是从高位开始连续都是1&#xff0c;后面是连续的0&#xff0c;它有以下两种表示方法&#xff1a; 传统表示法&#xff0c;如&#xff1a;255.255.255.0IP前…

Mybatis中的查询操作

单表查询 单表查询在《初始Mybatis》中已经介绍过&#xff0c;这里就不在介绍了。咱们这里只说单表查询中的“like查询”。like查询单独使用#{}报错 <select id"selectByKeyword" resultType"com.example.demo.entity.Userinfo">select * from use…

飞天使-linux操作的一些技巧与知识点4

文章目录 ansible配置文件的优先级尝试开始进行操作ansible常用模块ansible 的playbook示例安装phpplaybook中变量的引用 ansible yum install -y ansible 测试是否可用 ansible localhost -m ping /etc/ansible/ansible.cfg &#xff1a;主配置文件&#xff0c;配置 ansible…

JS基础之原型原型链

JS基础之原型&原型链 原型&原型链构造函数创建对象prototypeprotoconstructor实例与原型原型的原型原型链其他constructorproto继承 原型&原型链 构造函数创建对象 我们先使用构造函数创建一个对象&#xff1a; function Person(){ } var person new Person();…

Notes数据直接在Excel中统计

大家好&#xff0c;才是真的好。 我希望你看过前面两篇内容《Domino REST API安装和运行》和《Domino REST API安装和运行》&#xff0c;因为今天我们正是使用REST API方式在Excel中查询和统计Notes数据。 不过首先你得知道一个OData协议&#xff0c;全名Open Data Protocol(…