依赖

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

1 maven与presto集成代码

import java.sql.*;
import java.util.*;

public class prestoQuery {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.facebook.presto.jdbc.PrestoDriver");
        Connection connection = DriverManager.getConnection("jdbc:presto://hadoop102:8090/hive/test","hive",null);  ;
        Statement stmt = connection.createStatement();

        String sql = "select * from motor limit 10000";

        //获取开始时间
        long startTime = System.currentTimeMillis();
        List<Map<String, Object>> list = new ArrayList<>();     //存放结果(表数据)
        ResultSet rs = stmt.executeQuery(sql);   //调用sql

        //获取结束时间
        long endTime = System.currentTimeMillis();

        try {
//            表数据遍历
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map<String, Object> rowData = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }

            System.out.println(list);    //输出table数据

            //输出程序运行时间
            double spendTime = (endTime - startTime)/1000.0;
            System.out.println("查询时间:" + spendTime + "s");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

2 SpringBoot集成Presto

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.*;

@RestController
@RequestMapping("/presto")
public class PrestoQueryController {

	//将Connection、Statement 作为全局变量写在方法体外,避免每调用一次方法进行一次连接,节省资源
//    Class.forName("com.facebook.presto.jdbc.PrestoDriver");
    Connection connection = DriverManager.getConnection("jdbc:presto://hadoop102:8090/hive/test","root",null);
    Statement stmt = connection.createStatement();

	//全局变量后 写构造器
    public PrestoQueryController() throws SQLException {
    }


    @RequestMapping("/queryDatabases")
    public List DataBasesList() throws Exception {
        String sql = "show schemas from hive";    //展示hive的所有库
        ResultSet rs = stmt.executeQuery(sql);
        List list = new ArrayList<>();   //存放结果(hive的所有库)

        // 库遍历
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            List rowData = new ArrayList<>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.add(rs.getObject(i));
            }
            list.add(rowData);
        }


        return list;

    }
    //http://localhost:8088/presto/queryDatabases


    @RequestMapping("/queryTable")
    public List<Map<String, Object>> TableList(String table) throws Exception{
//        String sql = "show catalogs";    //展示仓库类型
        List<Map<String, Object>> list = new ArrayList<>();     存放结果(表数据)
        String sql = "select * from " + table + " limit 100000";

        //获取开始时间
        long startTime = System.currentTimeMillis();

        ResultSet rs = stmt.executeQuery(sql);   //调用sql

        //获取结束时间
        long endTime = System.currentTimeMillis();
        try {
            // 表数据遍历
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map<String, Object> rowData = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }

//            System.out.println(list);    //输出table数据

            //输出程序运行时间
            double spendTime = (endTime - startTime)/1000.0;
            System.out.println("查询时间:" + spendTime + "s");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    return list;
    }
    //  http://localhost:8088/presto/queryTable?table=motor


}

3 测试

在这里插入图片描述

Logo

永洪科技,致力于打造全球领先的数据技术厂商,具备从数据应用方案咨询、BI、AIGC智能分析、数字孪生、数据资产、数据治理、数据实施的端到端大数据价值服务能力。

更多推荐