您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java怎么集成presto查詢”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Java怎么集成presto查詢”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
<dependency> <groupId>com.facebook.presto</groupId> <artifactId>presto-jdbc</artifactId> <version>0.234.1</version> </dependency>
presto: url: xxxxxx username: root password: root port: 8088
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.sugon.xuanyuan.common.utils.StringUtils; import com.sugon.xuanyuan.service.dataprovider.utils.JdbcUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import java.sql.*; import java.util.Properties; /** * @Description: * @author: luoy * @date: 2020-06-24 09:45 */ @Configuration public class PrestoConnect { @Value("${presto.url}") private String server; @Value("${presto.port}") private String port; @Value("${presto.username}") private String username; @Value("${presto.password}") private String password; private Connection getConnection() throws Exception { /** * 功能:presto 通過 jdbc 連接 * 示例:jdbc:presto://host:port/ **/ String jdbcurl = "jdbc:presto://" + server + ":" + port + "/"; Connection conn ; Properties props = new Properties(); Class.forName("com.facebook.presto.jdbc.PrestoDriver"); props.setProperty("user", username); if (StringUtils.isNotBlank(password)) { props.setProperty("password", password); props.setProperty("SSL", "true"); //props.setProperty("SSLTrustStorePath", SSLTrustStorePath); //props.setProperty("SSLTrustStorePassword", SSLTrustStorePassword); jdbcurl = String.format("jdbc:presto://%s:%s/", server, port); } conn = DriverManager.getConnection(jdbcurl, props); /*設(shè)置連接的數(shù)據(jù)源類型 * 示例:mysql、hive */ conn.setCatalog("hive"); return conn; } public JSONArray getDataAll(String sql) throws Exception { JSONArray array = new JSONArray(); Statement ps = null; ResultSet rs = null; Connection con = null; try { con = getConnection(); ps = con.createStatement(); rs = ps.executeQuery(sql); // 獲取列數(shù) ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍歷ResultSet中的每條數(shù)據(jù) while (rs.next()) { JSONObject jsonObj = new JSONObject(); // 遍歷每一列 for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); String value = StringUtils.isBlank(rs.getString(columnName)) ? "" : rs.getString(columnName); jsonObj.put(columnName, value); } array.add(jsonObj); } } catch (Exception e) { throw new Exception("ERROR:" + e.getMessage(), e); } finally { //關(guān)閉資源(先開后關(guān)) JdbcUtil.close(rs, ps, con); } return array; } }
<dependency> <groupId>com.facebook.presto</groupId> <artifactId>presto-jdbc</artifactId> <version>0.267</version> </dependency>
/** * @ Author zhangsf * @CreateTime 2022/1/6 - 10:00 PM */ package presto; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class PrestoJdbcDemo { public static void main(String[] args) throws Exception{ //class.forname try { Class.forName("com.facebook.presto.jdbc.PrestoDriver"); }catch (ClassNotFoundException e){ e.printStackTrace(); } //若presto沒有設(shè)置SSL認證,只需填寫用戶名,不需要填寫密碼。 Connection connection = DriverManager.getConnection("jdbc:presto://localhost:8080/mysql/tp_music","root",null); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("select * from mysql.tp_music.singer limit 3"); while (rs.next()) { System.out.println("name:"+rs.getString(2)+" birth:"+rs.getString(5)+" location:"+rs.getString(6)); } rs.close(); connection.close(); } }
讀到這里,這篇“Java怎么集成presto查詢”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。