如何使用Java API执行Presto查询? [关闭]

问题描述 投票:-1回答:1

我在Presto上使用Qubole Data Service中的Azure。我想从Java程序执行Presto查询。如何在Java程序的Azure上的Qubole数据服务上的Presto集群中执行查询?

java sql presto
1个回答
4
投票

Presto提供了一个普通的JDBC驱动程序,允许您运行SQL查询。您所要做的就是将它包含在您的Java应用程序中。有关如何在其网站https://prestodb.io/docs/current/installation/jdbc.html上连接到Presto群集的示例:

// URL parameters
String url = "jdbc:presto://example.net:8080/hive/sales";
Properties properties = new Properties();
properties.setProperty("user", "test");
properties.setProperty("password", "secret");
properties.setProperty("SSL", "true");
Connection connection = DriverManager.getConnection(url, properties);

// properties
String url = "jdbc:presto://example.net:8080/hive/sales?user=test&password=secret&SSL=true";
Connection connection = DriverManager.getConnection(url);

我希望您知道如何使用Java中的普通数据库执行SQL语句。如果没有,请参阅https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

实质上,

Statement stmt = null;
String query = "select COF_NAME, SUP_ID, PRICE, " +
                "SALES, TOTAL " +
                "from " + dbName + ".COFFEES";
try {
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + "\t" + supplierID +
                           "\t" + price + "\t" + sales +
                           "\t" + total);
    }
} catch (SQLException e ) {
    JDBCTutorialUtilities.printSQLException(e);
} finally {
    if (stmt != null) { stmt.close(); }
}

至于为您的环境确定正确的连接参数(第一个示例中的jdbc url),请参阅Qubole的友好技术支持。

© www.soinside.com 2019 - 2024. All rights reserved.