zoukankan      html  css  js  c++  java
  • Presto Jdbc

    Presto Jdbc

    标签(空格分隔): Presto


    一, 建立连接

    传统的JDBC方式类似,建立PrestoConnection”连接“,并且通过unwrap方法将connection转换为PrestoConnection。实际上是赋值一些基本信息,并且建立新的OkHttpClient。

    String url = "jdbc:presto://ip:port/hive/“;    //默认连接hive
    String user = "PRESTO";
    Properties properties = new Properties();
    properties.setProperty("user", user);
    PrestoConnection conn = DriverManager.getConnection(prestoUrl, properties).unwrap(PrestoConnection.class);
    conn.setClientInfo("ApplicationName", "group_1");
    //指定资源组
    conn.setSessionProperty("query_max_total_memory", "1GB");    //指定此次操作可使用的presto最大内存大小
    
    

    conn.setSessionProperty("","");类似的属性可以在 presto Client 中进入查看:

    SET SESSION;
    即可显示 可以在SESSION 级别修改的 属性。

    二,建立Statement执行语句

    指定SQL执行的相关属性。在设置监听器的时候需要注意!presto的任务监听器会阻塞presto任务的执行,所以不建议在监听器中做任何耗时的操作。如果需要使用监听器记录presto任务的状态,可自己启动一个线程使用prestoResultSet.getStats()获取当前任务状态,监听任务进度。

    PrestoStatement statement = conn.createStatement().unwrap(PrestoStatement.class);
    statement.setQueryTimeout(10);    
//设置SQL语句可执行的时长(秒)
    statement.setLargeMaxRows(1000);
  //设置可获取结果集的大小(分批获取,直到超过此值后结束)
    AtomicReference<String> queryId = new AtomicReference<>();
    statement.setProgressMonitor(queryStats -> {    //设置监听器(可选),可监听presto任务执行状况
        queryId.set(queryStats.getQueryId());    //获取presto任务ID(可用该ID终止任务)
    });
    PrestoResultSet resultSet = statement.executeQuery("select * from table").unwrap(PrestoResultSet.class);
    

    三,获取结果集

    将结果集转换为json列表。这里需要注意的是resultSet.next()方法,Presto服务端并不会一次全部把结果返回给客户端,而是不断的通过next()方法调用HTTP接口获取(每次获取结果集大小默认1mb),直到PrestoClient状态不为Running时结束。

    List<JSONObject> results = new ArrayList<>();
    int count = resultSet.getMetaData().getColumnCount();
    String[] columns = new String[count];
    for (int i = 0; i < count; i++) {
        columns[i] = resultSet.getMetaData().getColumnName(i + 1);
    }
    while (resultSet.next()) {
        JSONObject jsonObject = new JSONObject();
        for (int j = 0; j < count; j++) {
            jsonObject.put(columns[j], resultSet.getString(j + 1));
        }
        results.add(jsonObject);
    }
    

    参考
    v1版本规范 https://github.com/prestodb/presto/wiki/HTTP-Protocol
    presto资源组 http://prestodb.github.io/docs/current/admin/resource-groups.html
    SystemSessionProperties https://github.com/prestodb/presto/blob/master/presto-main/src/main/java/com/facebook/presto/SystemSessionProperties.java
    https://zhuanlan.zhihu.com/p/72488989

  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/hit-zb/p/12591898.html
Copyright © 2011-2022 走看看