【问题标题】:How to execute Presto query using Java API? [closed]如何使用 Java API 执行 Presto 查询? [关闭]
【发布时间】:2018-05-31 11:44:35
【问题描述】:

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

【问题讨论】:

    标签: java sql presto


    【解决方案1】:

    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 的友好技术支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 2022-10-05
      • 2021-06-11
      相关资源
      最近更新 更多