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)證,只需填寫用戶名,不需要填寫密碼。
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詳細(xì)講解靜態(tài)資源導(dǎo)入的實(shí)現(xiàn)
在Web開(kāi)發(fā)過(guò)程中,我們需要接觸許多靜態(tài)資源,如CSS、JS、圖片等;在之前的開(kāi)發(fā)中,這些資源都放在Web目錄下,用到的時(shí)候按照對(duì)應(yīng)路徑訪問(wèn)即可。不過(guò)在SpringBoot項(xiàng)目中,沒(méi)有了Web目錄,那這些靜態(tài)資源該放到哪里去,又要如何訪問(wèn)呢?這就是我們要講的靜態(tài)資源導(dǎo)入2022-05-05
servlet之cookie簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Cookie技術(shù)誕生以來(lái),它就成了廣大網(wǎng)絡(luò)用戶和Web開(kāi)發(fā)人員爭(zhēng)論的一個(gè)焦點(diǎn)。下面這篇文章主要給大家介紹了關(guān)于servlet之cookie簡(jiǎn)介的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
JAVA中string數(shù)據(jù)類型轉(zhuǎn)換詳解
在JAVA中string是final類,提供字符串不可以修改,string類型在項(xiàng)目中經(jīng)常使用,下面給大家介紹了string七種數(shù)據(jù)類型轉(zhuǎn)換,需要的朋友可以參考下2015-07-07
SSH框架網(wǎng)上商城項(xiàng)目第28戰(zhàn)之使用Ajax技術(shù)局部更新商品數(shù)量和總價(jià)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第28戰(zhàn)之使用Ajax技術(shù)局部更新商品數(shù)量和總價(jià),感興趣的小伙伴們可以參考一下2016-06-06
解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
IDEA創(chuàng)建Spring項(xiàng)目無(wú)法選擇Java8的問(wèn)題及解決
文章描述了在使用Spring創(chuàng)建項(xiàng)目時(shí)遇到的問(wèn)題,通過(guò)將服務(wù)器地址從https://start.spring.io/替換為https://start.aliyun.com/,成功解決了無(wú)法選擇Java8的問(wèn)題2025-01-01
spring boot整合mybatis+mybatis-plus的示例代碼
這篇文章主要介紹了spring boot整合mybatis+mybatis-plus的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java關(guān)鍵字volatile和synchronized作用和區(qū)別
這篇文章主要為大家詳細(xì)介紹了Java關(guān)鍵字volatile和synchronized的作用和區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
springboot如何配置嵌套map和list參數(shù)
這篇文章主要介紹了springboot如何配置嵌套map和list參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

