Java怎么集成presto查询
作者:SoftHope
时间:2023-04-26
来源:互联网
浏览:0
Java集成presto查询1.pom文件引入相关jarcom.facebook.prestopresto-jdbc0.234.12.application.yml配置presto相关presto:url:xxxxxxusername:rootpassword:rootport:80883.获取连接与测试importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.sugon.xuanyuan.co
Java集成presto查询
1.pom文件引入相关jar
com.facebook.presto presto-jdbc 0.234.1
2.application.yml配置presto相关
presto: url: xxxxxx username: root password: root port: 8088
3.获取连接与测试
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);
/*设置连接的数据源类型
* 示例: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);
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍历ResultSet中的每条数据
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 {
//关闭资源(先开后关)
JdbcUtil.close(rs, ps, con);
}
return array;
}
}Java程序访问presto

pom.xml中引入presto-jdbc
com.facebook.presto presto-jdbc 0.267
/**
* @ 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没有设置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();
}
}
作者最新文章
苹果折叠屏iPhone是翻盖还是对折形态
2026-09-14 13:33
PDF转Word的4种方法及结果核对步骤
2026-09-09 06:00
速腾聚创自研SPAD-SoC芯片交付破50万颗,MARS基地实现8秒下线一台激光雷达
2026-09-08 17:42
TECNO Camon Slim 5G发布:6.39mm机身与6000mAh电池规格解析
2026-09-08 17:04
小米 18 Fold 暖金白图赏:中折叠形态与核心规格解析
2026-09-08 16:50
上一篇:
Mysql双主复制配置步骤详解
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















