Python 官方文档:入门教程 => 点击学习
目录Java集成presto查询1.pom文件引入相关jar2.application.yml配置presto相关3.获取连接与测试Java程序访问prestopom.xml中引入p
<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;
@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 {
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);
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;
}
}
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-jdbc</artifactId>
<version>0.267</version>
</dependency>
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();
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Java集成presto查询方式
本文链接: https://lsjlt.com/news/151990.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0