Heim  >  Artikel  >  Java  >  So integrieren Sie eine Presto-Abfrage in Java

So integrieren Sie eine Presto-Abfrage in Java

王林
王林nach vorne
2023-04-20 18:46:081662Durchsuche

Java integrierte Presto-Abfrage

1.pom-Datei führt zugehöriges JAR ein

    <dependency>
            <groupId>com.facebook.presto</groupId>
            <artifactId>presto-jdbc</artifactId>
            <version>0.234.1</version>
        </dependency>

2.application.yml-Konfiguration Presto-bezogen

presto:
  url: xxxxxx
  username: root
  password: root
  port: 8088

3 .Stellen Sie eine Verbindung her und testen Sie

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-Programmzugriff presto

So integrieren Sie eine Presto-Abfrage in Java

Presto-jdbc

in pom.xml einführen
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-jdbc</artifactId>
<version>0.267</version>
</dependency>
rrree

Das obige ist der detaillierte Inhalt vonSo integrieren Sie eine Presto-Abfrage in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen