栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

大数据组件与后端集成:SpringBoot+Presto

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

大数据组件与后端集成:SpringBoot+Presto

依赖


    com.facebook.presto
    presto-jdbc
    0.245

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> 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 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> TableList(String table) throws Exception{
//        String sql = "show catalogs";    //展示仓库类型
        List> 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 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 测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号