Java アプリケーションでは、Presto が提供する JDBC ドライバーを使用してデータベースにアクセスできます。 手順は、一般的な RDBMS データベースと同じです。

Maven の概要

次の設定を POM ファイルに追加して、Presto JDBC ドライバーを導入できます。
<dependency>
    <groupId>com.facebook.presto</groupId>
    <artifactId>presto-jdbc</artifactId>
    <version>0.187</version>
</dependency>

ドライバークラス名

Presto JDBC ドライバークラスは com.facebook.presto.jdbc.PrestoDriver です。

接続文字列

次の接続文字列形式をサポートしています。
jdbc:presto://<COORDINATOR>:<PORT>/[CATALOG]/[SCHEMA]
例:
jdbc:presto://emr-header-1:9090               # Connects to data base, using the default Catalog and Schema
jdbc:presto://emr-header-1:9090/hive          # Connects to data base, using Catalog(hive) and the default Schema
jdbc:presto://emr-header-1:9090/hive/default  # Connects to data base, using Catalog(hive) and Schema(default)

接続パラメーター

Presto JDBC ドライバーは、URL パラメーターまたは Properties として設定し、DriverManager に渡すことができるさまざまなパラメーターをサポートしています。

パラメーターを Properties として DriverManager に渡す例は以下のとおりです。
String url = "jdbc:presto://emr-header-1:9090/hive/default";
Properties properties = new Properties();
properties.setProperty("user", "hadoop");
Connection connection = DriverManager.getConnection(url, properties);
......
パラメーターを URL パラメーターとして DriverManager に渡す例は以下のとおりです。
String url = "jdbc:presto://emr-header-1:9090/hive/default? user=hadoop";
Connection connection = DriverManager.getConnection(url);
......
パラメーターの説明は、以下のとおりです。
パラメーター名 形式 説明
user STRING ユーザー名。
password STRING パスワード。
Socksproxy \:\ SOCKS プロキシサーバーのアドレスおよびポート。 たとえば、localhost:1080 です。
httpProxy \:\ HTTP プロキシサーバーのアドレスおよびポート。 たとえば、localhost:8888 です。
SSL true\ 接続に HTTPS を使用するか否か。 これはデフォルトでは false です。
SSLTrustStorePath STRING Java TrustStore ファイルのパス。
SSLTrustStorePassword STRING Java TrustStore パスワード。
KerberosRemoteServiceName STRING Kerberos サービスの名前。
KerberosPrincipal STRING Kerberos principal。
KerberosUseCanonicalHostname true\ canonical ホスト名を使用するか否か。 これはデフォルトでは false です。
KerberosConfigPath STRING Kerberos 設定ファイルのパス。
KerberosKeytabPath STRING Kerberos KeyTab ファイルのパス。
KerberosCredentialCachePath STRING Kerberos 資格情報キャッシュのパス

Java の例

以下は、Java で Presto JDBC ドライバーを使用する例です。
.....
// Loads the JDBC Driver class
try {
    Class.forName("com.facebook.presto.jdbc.PrestoDriver");
} catch(ClassNotFoundException e) {
    LOG.ERROR("Failed to load presto jdbc driver.", e);
    System.exit(-1);
}
Connection connection = null;
Statement stmt = null;
try {
    String url = "jdbc:presto://emr-header-1:9090/hive/default";
    Properties properties = new Properties();
    properties.setProperty("user", "hadoop");
    // Creates the connection object
    Connection = drivermanager. getconnection (URL, properties );
    // Creates the Statement object
    statement = connection.createStatement();
    Executes the query
    ResultSet rs = statement.executeQuery("select * from t1");
    Returns results
    int columnNum = rs.getMetaData().getColumnCount();
    int rowIndex = 0;
    while (rs.next()) {
        rowIndex++;
        for(int i = 1; i <= columnNum; i++) {
            System.out.println("Row " + rowIndex + ", Column " + i + ": " + rs.getInt(i));
        }
    }
} catch(SQLException e) {
    LOG.ERROR("Exception thrown.", e);
} finally {
  // Destroys Statement object
  If (statement! = null) {
      try {
        statement.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
  Closes connection
  if (connection ! = null) {
      try {
        connection.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
}

リバースプロキシの使用

HAProxy リバースプロキシコーディネーターを使用して、プロキシサービスを介して Presto サービスにアクセスできます。

  • 非セキュリティクラスタープロキシの設定
    非セキュリティクラスターのクラスタープロキシを設定するには、次の手順を実行します。
    1. プロキシノードに HAProxy をインストールします。
    2. HAProxy 設定を変更し (/Etc/haproxy. cfg)、次のコンテンツを追加します。
      ......
      
      listen prestojdbc :9090
          Mode TCP
          option tcplog
          balance source
          Server presto-coodinator-1 emr-header-1: 9090
    3. HAProxy サービスを再起動します。
    これで、プロキシサーバーを使用して Presto にアクセスできます。 接続サーバーの IP アドレスをプロキシサービスの IP アドレスに変更するのみです。