一. Trino CLI
1. 安裝client
Trino CLI提供了一個(gè)基于終端的交互式shell。你可以通過(guò)它運(yùn)行查詢并與Trino服務(wù)端交互來(lái)檢查其元數(shù)據(jù)。
下載地址:trino-cli-434-executable.jar
# 下載
wget -O trino https://repo1.maven.org/maven2/io/trino/trino-cli/434/trino-cli-434-executable.jar
# 添加可執(zhí)行權(quán)限、并將其添加到PATH下,方便使用
chmod +x trino
mv trino ~/bin
export PATH=~/bin/:$PATH
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-756853.html
2. 使用client執(zhí)行sql
./trino http://trino.example.com:8080
# 幫助文檔
trino> help
Supported 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>
?
運(yùn)行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-11
1 | 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時(shí)可以設(shè)置默認(rèn)的catalog(某個(gè)連接實(shí)例)和schema(數(shù)據(jù)庫(kù)),這樣可以直接查詢表。
./trino http://localhost:8080/mysql-1/flinkx_test
trino:flinkx_test> show tables;
Table
-----------------------------------------------------
aaa
aaa_csv111_1
aaa_csv111_2
...
?
USE默認(rèn)的catalog和schema,直接查詢此庫(kù)下的表
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-11
1 | 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選項(xiàng)
./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`選項(xiàng)來(lái)
./trino http://localhost:8080 --catalog "mysql-1" --schema flinkx_test \
--execute 'select * from flinkx_hdfs_log limit 3;'
# 通過(guò)http設(shè)限定符
./trino http://localhost:8080/mysql-1/flinkx_test \
--execute 'select * from flinkx_hdfs_log limit 3;'
?
執(zhí)行多個(gè)sql
trino http://localhost:8080 \
--execute 'use "mysql-1".flinkx_test;select * from flinkx_hdfs_log limit 3;'
?
執(zhí)行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
選項(xiàng)來(lái)控制如何在非交互模式下顯示輸出,可用的選項(xiàng)有ALIGNED、VERTICAL、TSV、TSV_HEADER、CSV、CSV_HEADER、CSV_UNQUOTED、CSV_HEADER_UNQUOTED、JSON和NULL,默認(rèn)值是CSV。
?
忽略錯(cuò)誤
Trino CLI提供了
--ignore-error
選項(xiàng)來(lái)忽略執(zhí)行文件中的查詢時(shí)遇到的任何錯(cuò)誤。默認(rèn)行為是在遇到第一個(gè)錯(cuò)誤時(shí)終止執(zhí)行腳本。
?
二. JDBC driver 連接Trino
任何Java應(yīng)用程序都可以通過(guò)Java數(shù)據(jù)庫(kù)連接(JDBC)驅(qū)動(dòng)連接到Trino。通過(guò)JDBC驅(qū)動(dòng),所有這些應(yīng)用程序都可以使用Trino。
Trino的JDBC驅(qū)動(dòng)允許你連接到Trino并使用SQL語(yǔ)句與Trino交互。
如果你熟悉JDBC驅(qū)動(dòng)的不同實(shí)現(xiàn),就知道Trino的JDBC驅(qū)動(dòng)是Type 4驅(qū)動(dòng),這僅僅意味著它直接與Trino原生協(xié)議通信。
?
1. 通過(guò)DBeaver用戶界面連接
查看表時(shí)出現(xiàn)如下錯(cuò)誤:
需要聲明用戶,默認(rèn)輸入admin、密碼為空
重新連接可以看到注冊(cè)的catalog了
可以像之前使用DBeaver查看表了。
?
2. JDBC Driver in java
2.1. 環(huán)境配置
需要java 8 或更高
所有通過(guò)JDBC連接Trino的用戶,必須在system.jdbc
schema中賦予權(quán)限
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協(xié)議和 Trino client REST API 和Trino進(jìn)行通訊。
?
2.2. 注冊(cè)和配置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的驅(qū)動(dòng)識(shí)別不到,則可以顯式聲明:
io.trino.jdbc.TrinoDriver
。
?
2.3. 連接參數(shù)
聲明方式有如下兩種
// 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);
當(dāng)然這兩種方式我們可以混用,有些參數(shù)通過(guò)properties設(shè)置,有些在url中設(shè)置。
完整的參數(shù)見:Parameter reference
?
2.4. 查詢例子
package com.gao;
import java.sql.*;
public class TrinoQueryExample {
public static void main(String[] args) {
// Trino數(shù)據(jù)庫(kù)連接信息
String trinoUrl = "jdbc:trino://xxx:8080";
String trinoUser = "admin";
String trinoPassword = "";
// SQL查詢語(yǔ)句
String sqlQuery = "select * from \"mysql-1\".flinkx_test.flinkx_hdfs_log limit 3";
try {
Class.forName("io.trino.jdbc.TrinoDriver");
// 建立Trino數(shù)據(jù)庫(kù)連接
Connection connection = DriverManager.getConnection(trinoUrl, trinoUser, trinoPassword);
// 創(chuàng)建Statement對(duì)象
Statement statement = connection.createStatement();
// 執(zhí)行查詢
ResultSet resultSet = statement.executeQuery(sqlQuery);
// 處理查詢結(jié)果
while (resultSet.next()) {
// 根據(jù)查詢結(jié)果的列名或索引獲取數(shù)據(jù)
Integer id = resultSet.getInt("id");
Timestamp create_time = resultSet.getTimestamp("create_time");
// 處理獲取的數(shù)據(jù)
System.out.println("id: " + id + ", create_time: " + create_time);
}
// 關(guān)閉資源
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權(quán)威指南:原書第二版》
trino官網(wǎng):https://trino.io/docs/current/client.html文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-756853.html
到了這里,關(guān)于【trino權(quán)威指南】使用trino詳解:trino client安裝、查詢sql、DBeaver連接trino、java通過(guò)JDBC連接trino的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!