文档首页 > > 开发指南> Presto应用开发> 开发程序> 样例代码说明

样例代码说明

分享
更新时间: 2020/06/19 GMT+08:00

Presto JDBC使用样例

下面的代码片段在PrestoJDBCExample类中,用于实现JDBC连接Presto TPCDS Catalog。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
private static Connection connection;
private static Statement statement;
/** 
 * Only when Kerberos authentication enabled, configurations in presto-examples/conf/presto.properties
 * should be set. More details please refer to https://prestodb.io/docs/0.215/installation/jdbc.html.
*/
private static void initConnection(String url, boolean krbsEnabled) throws SQLException {
    if (krbsEnabled) {
        String filePath = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
        File proFile = new File(filePath + "presto.properties");if (proFile.exists()) {
            Properties props = new Properties();
            try {
                props.load(new FileInputStream(proFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection = DriverManager.getConnection(url, props);
        }
    } else {
          connection = DriverManager.getConnection(url, "presto", null);
    }
    statement = connection.createStatement();
}

private static void releaseConnection() throws SQLException {
    statement.close();
    connection.close();
}

public static void main(String[] args) throws SQLException {
    try {
        /**
         * Replace example_ip with your cluster presto server ip.
         * By default, Kerberos authentication disabled cluster presto service port is 7520, Kerberos
         * authentication enabled cluster presto service port is 7521
         * The postfix /tpcds/sf1 means to use tpcds catalog and sf1 schema, you can use hive catalog as well
         * If Kerberos authentication enabled, set the second param to true.
         * see PrestoJDBCExample#initConnection(java.lang.String, boolean).
         */
        initConnection("jdbc:presto://example_ip:7520/tpcds/sf1", false);
        //initConnection("jdbc:presto://example_ip:7521/tpcds/sf1", true);
        ResultSet resultSet = statement.executeQuery("select * from call_center");
        while (resultSet.next()) {
            System.out.println(resultSet.getString("cc_name") + " : " + resultSet.getString("cc_employees"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        releaseConnection();
    }
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问