Java集成presto查詢方式
Java集成presto查詢
1.pom文件引入相關(guān)jar
<dependency> <groupId>com.facebook.presto</groupId> <artifactId>presto-jdbc</artifactId> <version>0.234.1</version> </dependency>
2.application.yml配置presto相關(guān)
presto: url: xxxxxx username: root password: root port: 8088
3.獲取連接與測(cè)試
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 通過(guò) 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)閉資源(先開(kāi)后關(guān)) JdbcUtil.close(rs, ps, con); } return array; } }
Java程序訪問(wèn)presto
pom.xml中引入presto-jdbc
<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沒(méi)有設(shè)置SSL認(rèn)證,只需填寫(xiě)用戶名,不需要填寫(xiě)密碼。 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(); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表
這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java項(xiàng)目實(shí)戰(zhàn)之在線考試系統(tǒng)的實(shí)現(xiàn)(系統(tǒng)介紹)
這篇文章主要介紹了Java項(xiàng)目實(shí)戰(zhàn)之在線考試系統(tǒng)的實(shí)現(xiàn)(系統(tǒng)介紹),本文通過(guò)實(shí)例代碼,截圖的形式給大家展示系統(tǒng)技術(shù)架構(gòu),需要的朋友可以參考下2020-02-02使用java8 API遍歷過(guò)濾文件目錄及子目錄和隱藏文件示例詳解
這篇文章主要介紹了使用java8API遍歷過(guò)濾文件目錄及子目錄及隱藏文件示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java中使用synchronized關(guān)鍵字實(shí)現(xiàn)簡(jiǎn)單同步操作示例
這篇文章主要介紹了Java中使用synchronized關(guān)鍵字實(shí)現(xiàn)簡(jiǎn)單同步操作示例,本文起講解了synchronized修飾函數(shù)、synchronized修飾代碼塊、synchronized修飾靜態(tài)方法等內(nèi)容,需要的朋友可以參考下2015-04-04Dom4j解析xml復(fù)雜多節(jié)點(diǎn)報(bào)文方式
這篇文章主要介紹了Dom4j解析xml復(fù)雜多節(jié)點(diǎn)報(bào)文方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java架構(gòu)設(shè)計(jì)之六步拆解 DDD
DDD(Domain-Driven Design 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì))是由Eric Evans最先提出,目的是對(duì)軟件所涉及到的領(lǐng)域進(jìn)行建模,以應(yīng)對(duì)系統(tǒng)規(guī)模過(guò)大時(shí)引起的軟件復(fù)雜性的問(wèn)題2022-02-02SpringBoot中實(shí)現(xiàn)文件上傳、下載、刪除功能的步驟
本文將詳細(xì)介紹如何在 Spring Boot 中實(shí)現(xiàn)文件上傳、下載、刪除功能,采用的技術(shù)框架包括:Spring Boot 2.4.2、Spring MVC、MyBatis 3.5.6、Druid 數(shù)據(jù)源、JUnit 5 等,文中有詳細(xì)的操作步驟和示例代碼供大家參考,需要的朋友可以參考下2024-01-01如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控
這篇文章主要介紹了如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04