How to execute Presto query using Java API? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-23 06:21:24

问题


I am using Presto in Qubole Data Service on Azure. I want to execute Presto query from Java program. How can I execute query in Presto cluster which is on Qubole Data Service on Azure from Java Program?


回答1:


Presto offers a normal JDBC driver that allows you to run SQL queries. All you have to do is to include it in your java application. There is an example on how to connect to a Presto cluster on their website https://prestodb.io/docs/current/installation/jdbc.html:

// 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);

I hope you know how to execute SQL Statements with a normal database in Java. If not, see https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html:

Essentially,

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

As to figuring out the correct connection parameters (jdbc url in the first example) for your environment, please refer to your friendly tech support at Qubole.



来源:https://stackoverflow.com/questions/47863268/how-to-execute-presto-query-using-java-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!