- 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
广告