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

  • 相关阅读:
    poj 2528 Mayor's posters (线段树+离散化)
    poj 1201 Intervals (差分约束)
    hdu 4109 Instrction Arrangement (差分约束)
    poj 1195 Mobile phones (二维 树状数组)
    poj 2983 Is the Information Reliable? (差分约束)
    树状数组 讲解
    poj 2828 Buy Tickets (线段树)
    hdu 1166 敌兵布阵 (树状数组)
    Ubuntu网络配置
    Button控制窗体变量(开关控制灯的状态)
  • 原文地址:https://www.cnblogs.com/hit-zb/p/12591898.html
Copyright © 2011-2022 走看看