- Apache Presto 教程
- Apache Presto - 主頁
- Apache Presto - 概覽
- Apache Presto - 架構
- Apache Presto - 安裝
- Apache Presto - 配置
- Apache Presto - 管理
- Apache Presto - SQL 操作
- Apache Presto - SQL 函式
- Apache Presto - MySQL 聯結器
- Apache Presto - JMX 聯結器
- Apache Presto - HIVE 聯結器
- Apache Presto - KAFKA 聯結器
- Apache Presto - JDBC 介面
- 自定義函式應用程式
- Apache Presto 實用資源
- Apache Presto - 快速指南
- Apache Presto - 實用資源
- Apache Presto - 討論
Apache Presto - JDBC 介面
Presto 的 JDBC 介面用於訪問 Java 應用程式。
必備條件
安裝 presto-jdbc-0.150.jar
可以透過訪問以下連結下載 JDBC jar 檔案,
https://repo1.maven.org/maven2/com/facebook/presto/presto-jdbc/0.150/
下載 jar 檔案後,請將其新增到 Java 應用程式的類路徑中。
建立簡單應用程式
讓我們使用 JDBC 介面建立一個簡單的 java 應用程式。
編碼 − PrestoJdbcSample.java
import java.sql.*;
import com.facebook.presto.jdbc.PrestoDriver;
//import presto jdbc driver packages here.
public class PrestoJdbcSample {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.facebook.presto.jdbc.PrestoDriver");
connection = DriverManager.getConnection(
"jdbc:presto://:8080/mysql/tutorials", "tutorials", “");
//connect mysql server tutorials database here
statement = connection.createStatement();
String sql;
sql = "select auth_id, auth_name from mysql.tutorials.author”;
//select mysql table author table two columns
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
int id = resultSet.getInt("auth_id");
String name = resultSet.getString(“auth_name");
System.out.print("ID: " + id + ";\nName: " + name + "\n");
}
resultSet.close();
statement.close();
connection.close();
}catch(SQLException sqlException){
sqlException.printStackTrace();
}catch(Exception exception){
exception.printStackTrace();
}
}
}
儲存檔案並退出應用程式。現在,在一個終端中啟動 Presto 伺服器並在一個新終端中開啟以編譯並執行結果。以下為步驟 −
編譯
~/Workspace/presto/presto-jdbc $ javac -cp presto-jdbc-0.149.jar PrestoJdbcSample.java
執行
~/Workspace/presto/presto-jdbc $ java -cp .:presto-jdbc-0.149.jar PrestoJdbcSample
輸出
INFO: Logging initialized @146ms ID: 1; Name: Doug Cutting ID: 2; Name: James Gosling ID: 3; Name: Dennis Ritchie
廣告