How to execute Presto query using Java API?

时间:2017-12-18 06:38:01

标签: java sql presto

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 个答案:

答案 0 :(得分:4)

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.