全部產品
Search
文件中心

E-MapReduce:通過JDBC方式訪問Presto

更新時間:Jul 01, 2024

建立了包含Presto服務的叢集後,如果您需要進行複雜的資料查詢、分析和處理操作,或者需要將查詢結果集成到Java應用程式中,則可以使用Presto提供的JDBC Driver串連資料庫。

在Maven中引入JDBC Driver

您需要根據您的E-MapReduce叢集版本,在pom.xml檔案中添加相應的依賴來引入Presto JDBC Driver。

組件版本

JDBC Driver

Driver類名

0.2XX

<dependency>
    <groupId>com.facebook.presto</groupId>
    <artifactId>presto-jdbc</artifactId>
    <version>0.2XX</version>
</dependency>

com.facebook.presto.jdbc.PrestoDriver

其中,Java版本需要為Java 8或更高版本,且所有使用者都需被授予system.jdbc表的查詢許可權。

資料庫連接

您可以通過如下JDBC URL,使用JDBC Driver串連資料庫。

jdbc:presto://<COORDINATOR>:<PORT>/[CATALOG]/[SCHEMA]

串連樣本如下所示。

jdbc:presto://master-1-1:8889               # 串連資料庫,使用Catalog和Schema。
jdbc:presto://master-1-1:8889/hive          # 串連資料庫,使用Catalog(hive)和Schema。
jdbc:presto://master-1-1:8889/hive/default  # 串連資料庫,使用Catalog(hive)和Schema(default)。

串連參數

JDBC Driver支援多種配置參數,這些參數可以通過以下兩種方式傳入:

  • 使用Properties對象。

    String url = "jdbc:presto://<主節點名稱>:8889/hive/default";
    Properties properties = new Properties();
    properties.setProperty("user", "presto");
    Connection connection = DriverManager.getConnection(url, properties);
    //......進行資料庫操作
  • 直接在URL中指定。

    String url = "jdbc:presto://<主節點名稱>:8889/hive/default?user=presto";
    Connection connection = DriverManager.getConnection(url);
    //......進行資料庫操作

常用參數說明如下。

參數名稱

格式

參數說明

user

STRING

用於身分識別驗證和授權的使用者名稱。

password

STRING

用於LDAP身分識別驗證的密碼。

socksProxy

STRING:NUMBER

SOCKSProxy 伺服器地址。例如localhost:1080。

httpProxy

STRING:NUMBER

HTTPProxy 伺服器地址。例如localhost:8888。

SSL

BOOLEAN

是否使用HTTPS串連。預設值為false。

SSLKeyStorePath

STRING

儲存KeyStore檔案的路徑。

SSLKeyStorePassword

STRING

KeyStore的訪問密碼。

SSLTrustStorePath

STRING

指向Java TrustStore檔案的路徑。

SSLTrustStorePassword

STRING

Java TrustStore的訪問密碼。

KerberosRemoteServiceName

STRING

Kerberos服務的名稱。

KerberosPrincipal

STRING

Kerberos Principal名稱。

KerberosUseCanonicalHostname

BOOLEAN

是否使用正常化的主機名稱。預設為值false。

KerberosConfigPath

STRING

Kerberos設定檔的路徑。

KerberosKeytabPath

STRING

Kerberos KeyTab檔案的路徑。

KerberosCredentialCachePath

STRING

Kerberos Credential快取檔案的路徑。

樣本

下面是Java使用JDBC Driver串連資料庫的範例程式碼。

Connection connection = null;
Statement statement = null;
try {
    // 根據組件名稱使用正確的JDBC URL。
    String url = "jdbc:presto://<主節點名稱>:8889/hive/default";
    Properties properties = new Properties();
    properties.setProperty("user", "presto");
    // 建立連線物件。
    connection = DriverManager.getConnection(url, properties);
    // 建立Statement對象。
    statement = connection.createStatement();
    // 執行查詢。
    ResultSet rs = statement.executeQuery("select * from t1");
    // 擷取結果。
    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 {
  // 銷毀Statement對象。
  if (statement != null) {
      try {
        statement.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
  // 關閉串連。
  if (connection != null) {
      try {
        connection.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
}

相關文檔

如果您只是進行簡單的資料查詢操作,建議使用命令列的方式訪問Presto,詳情請參見通過命令列方式訪問Presto