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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM