如何使用 Java API 执行 Presto 查询? [英] How to execute Presto query using Java API?

查看:146
本文介绍了如何使用 Java API 执行 Presto 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PrestoAzure 上的 ="nofollow noreferrer">Qubole 数据服务.我想从 Java 程序执行 Presto 查询.如何通过 Java 程序在 Azure 上的 Qubole 数据服务上的 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?

推荐答案

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

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

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

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:

本质上,

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

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.

这篇关于如何使用 Java API 执行 Presto 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆