Apache-presto-jdbc-interface

提供:Dev Guides
移動先:案内検索

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://localhost: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();
      }
   }
}

ファイルを保存して、アプリケーションを終了します。 次に、1つのターミナルで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