> Java > java지도 시간 > 본문

Java에 Presto 쿼리를 통합하는 방법

王林
풀어 주다: 2023-04-20 18:46:08
앞으로
1683명이 탐색했습니다.

Java 통합 presto 쿼리

1.pom 파일은 관련 jar를 소개합니다

    <dependency>
            <groupId>com.facebook.presto</groupId>
            <artifactId>presto-jdbc</artifactId>
            <version>0.234.1</version>
        </dependency>
로그인 후 복사

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에 액세스합니다

Java에 Presto 쿼리를 통합하는 방법

pom.xml을 소개합니다. presto-jdbc

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-jdbc</artifactId>
<version>0.267</version>
</dependency>
로그인 후 복사
errree

위 내용은 Java에 Presto 쿼리를 통합하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!