【筆記】Presto實作 (二)架設
「presto」建置筆記,主要流程從官方的文件
https://prestodb.io/docs/current/installation/deployment.html
照著官方文件走,就可以順利完成大部分流程:
以下是筆記紀錄:
(一)下載程序包
去官網連結下載最新的程序包,https://prestodb.io/download.html
※下載檔案
sudo wget https://repo1.maven.org/maven2/com/facebook/presto/presto-server/0.227/presto-server-0.227.tar.gz
※解壓縮檔案
sudo tar -zxvf presto-server-0.227.tar.gz
(二)更改用戶組
sudo chgrp -R centos presto
sudo chown -R centos presto
(三)目錄檔案配置
presto
└── presto-server-0.227
├── lib
├── bin
├── plugin
└── etc
├── jvm.config
├── config.properties
├── log.properties
├── node.properties
└── catalog
└── http.properties
其中etc目錄一開始如果是不存在的。
我們必須創建。並且配置上述的各種檔案。
etc檔案主要配置共有5個配置:
正常來說檔案都是不存在,請手動配置,以下配置都已經添加註解上去。
正常來說檔案都是不存在,請手動配置,以下配置都已經添加註解上去。
(1) jvm.config (java配置檔案)
由於presto是java base,會需要有java啟動的相關配置。
以下是demo用的參數設定,實際大小情況還是視個人專案決定。
以下是demo用的參數設定,實際大小情況還是視個人專案決定。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-server | |
-Xmx2G | |
-XX:+UseG1GC | |
-XX:G1HeapRegionSize=32M | |
-XX:+UseGCOverheadLimit | |
-XX:+ExplicitGCInvokesConcurrent | |
-XX:+HeapDumpOnOutOfMemoryError | |
-XX:+ExitOnOutOfMemoryError |
(2) config.properties (presto相關設定)
關於presto的基礎配置,像是如何定義presto的角色。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------- | |
# config | |
# 包含了Presto server的所有配置資訊。 | |
# 每個Presto server既是一個coordinator也是一個worker。 | |
#------------------------------------------------------------- | |
#指定是否運維Presto例項作為一個coordinator(接收來自客戶端的查詢情切管理每個查詢的執行過程) | |
coordinator=true | |
#是否允許在coordinator服務中進行排程工作 (false則專心為coordinator) | |
#對於大型的叢集,在一個節點上的Presto server即作為coordinator又作為worke將會降低查詢效能。 | |
#因為如果一個伺服器作為worker使用,那麼大部分的資源都會被worker佔用,那麼就不會有足夠的資源進行關鍵任務排程、管理和監控查詢執行 | |
node-scheduler.include-coordinator=true | |
#指定HTTP server的埠。Presto使用HTTP進行內部和外部的所有通訊 | |
http-server.http.port=8080 | |
query.max-memory=1GB | |
query.max-memory-per-node=512MB | |
#Presto 通過Discovery 服務來找到叢集中所有的節點。為了能夠找到叢集中所有的節點,每一個Presto例項都會在啟動的時候將自己註冊到discovery服務。 | |
#Presto為了簡化部署,並且也不想再增加一個新的服務程序,Presto coordinator 可以執行一個內嵌在coordinator 裡面的Discovery 服務。 | |
#這個內嵌的Discovery 服務和Presto共享HTTP server並且使用同樣的埠 | |
discovery-server.enabled=true | |
#Discovery server的URI。由於啟用了Presto coordinator內嵌的Discovery 服務,因此這個uri就是Presto coordinator的uri。注意:這個URI一定不能以“/“結尾 | |
discovery.uri=http://localhost:8080 | |
(3) node.properties (節點設定檔案)
由於presto是一個分散式架構,需要定義每一個機台所代表的唯一id。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------- | |
# node | |
# 包含針對於每個節點的特定的配置資訊。 一個節點就是在一臺機器上安裝的Presto例項 | |
#------------------------------------------------------------- | |
#叢集名稱, 所有在同一個叢集中的Presto節點必須擁有相同的叢集名稱 | |
node.environment=test | |
#每個Presto節點的唯一標示。每個節點的node.id都必須是唯一的。 | |
#在Presto進行重啟或者升級過程中每個節點的node.id必須保持不變。如果在一個節點上安裝多個Presto例項 | |
#例如:在同一臺機器上安裝多個Presto節點),那麼每個Presto節點必須擁有唯一的node.id | |
node.id=node-ch01 | |
#資料儲存目錄的位置(作業系統上的路徑), Presto將會把日期和資料儲存在這個目錄下 | |
node.data-dir=/home/centos/presto/data/ | |
(4) log.properties (log檔案配置)
java log的設定相關。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------- | |
# log | |
# 這個配置檔案中允許你根據不同的日誌結構設定不同的日誌級別。 | |
# 每個logger都有一個名字(通常是使用logger的類的全標示類名). Loggers通過名字中的“.“來表示層級和整合關係,資訊如下: | |
#------------------------------------------------------------- | |
#四個等級:DEBUG,INFO,WARN,ERROR | |
com.facebook.presto=INFO | |
(5) catalog 目錄,(放入connector相關的設定檔)
catalog是一個目錄。由於presto有非常多的connector可以選擇。
每一個connector的設定也不盡相同,但都放在此目錄下。
(四)配置Connector
connector 對presto來說是一個很重要的存在。
也正是因為有它的存在,presto才可以讀取各種不同的 storge (s3,mysql,hive...等ˇ等)
要知道有那些connector可以使用,或是怎麼用。
https://prestodb.io/docs/current/
官方文檔上都說得非常詳細。
目前由於demo的需求,是透過 presto 去讀取「csv」類型檔案。
這部分,presto的 http connector可以解決這個需求。
這裡就舉例配置http的connector,其他類型都是如法炮製,可以舉一反多。
如上圖,這次demo大概流程如上
(1) 透過http connector, presto去對一個伺服器讀取檔案的schema (json格式)。
(2) presto獲得schema後,得知檔案來源與解析格式。
(3) presto知道如何去讀取data.csv
配置方式如下:
(1)在webserver 新建schema 與 csv 檔案
這個json的用意,是在定義檔案讀取的schema,讓presto知道如何去解讀csv,
在此先定義一個叫做schema.json檔案。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"schema": [ | |
{ | |
"name": "table1", | |
"columns": [ | |
{ | |
"name": "memid", | |
"type": "varchar" | |
}, | |
{ | |
"name": "num", | |
"type": "bigint" | |
} | |
], | |
"sources": [ | |
"http://xxxxxxx.com.tw/data/data.csv" | |
] | |
}, { | |
"name": "table2", | |
"columns": [ | |
{ | |
"name": "uid", | |
"type": "bigint" | |
}, | |
{ | |
"name": "game_name", | |
"type": "varchar" | |
} | |
], | |
"sources": [ | |
"http://xxxxxxxxx.com.tw/data/data2.csv" | |
] | |
} | |
] | |
} |
可以看出這邊定義了二個Table,在結構裡面也很清楚,csv的檔案路徑,欄位解析方式。
以下是這個csv的檔案:
data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test1234 | 2 | |
---|---|---|
test5678 | 3 |
(2)新建一個 http.properties
在第三步驟的 catalog目錄下新建一個檔案,並將內容更改為下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"schema": [ | |
{ | |
"name": "table1", | |
"columns": [ | |
{ | |
"name": "memid", | |
"type": "varchar" | |
}, | |
{ | |
"name": "num", | |
"type": "bigint" | |
} | |
], | |
"sources": [ | |
"http://xxxxxxx.com.tw/data/data.csv" | |
] | |
}, { | |
"name": "table2", | |
"columns": [ | |
{ | |
"name": "uid", | |
"type": "bigint" | |
}, | |
{ | |
"name": "game_name", | |
"type": "varchar" | |
} | |
], | |
"sources": [ | |
"http://xxxxxxxxx.com.tw/data/data2.csv" | |
] | |
} | |
] | |
} |
做到這邊,基本上connector都已經配置完畢。
(五)啟動伺服器
安裝目錄的bin/launcher文件,就是啟動腳本。
啟動方式有以下兩種:
(1)作為一個後台進程啟動:bin/launcher start
(2)也可以在前台運行, 可查看具體的日誌:bin/launcher run
如需要提停止服務,進程命令:bin/launcher stop
查看進程:ps -aux|grep PrestoServer 或 jps
(六)測試執行
https://prestodb.io/docs/current/installation/cli.html
官方也有提供載點可以下載,
須注意的是,在Linux,要將檔案jar的副檔名去除,更名為presto
將CLI的JAR重新命名為 presto(副檔名JAR需去掉) (executable with chmod +x,)
並放在跟bin同一層的目錄裡。
之後下cli指令
./presto --server localhost:8080 --catalog http --schema default
這樣就可以連上了。再來我們測試sql語法,查詢csv裡面的資料。
select * from http.schema.table1;
資料就會顯示出來。(七)後記
由於presto是java base,理所當然它的支援性都是對java比較友善。如果需要跟presto做介接查詢,當然還是有很多人有提供其他語言的plugin
但官方主要還是提供java的jar檔做引入。
如果寫java的人,需要連上presto,其實很簡單,只需要引入jar,然後如下就可以了。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void excuteDB() { | |
Connection con = null; | |
Statement stmt = null; | |
ResultSet rs = null; | |
try { | |
Class.forName("com.facebook.presto.jdbc.PrestoDriver"); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
try { | |
con = DriverManager.getConnection("jdbc:presto://127.0.0.1:8080/http/default", "root", null); | |
stmt = con.createStatement(); | |
rs = stmt.executeQuery("select * from http.default.table1"); | |
while (rs.next()) { | |
System.out.println("memid:" + rs.getString("memid") + ",name:" + rs.getInt("num")); | |
} | |
rs.close(); | |
con.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
SQLUtil.close(rs); | |
SQLUtil.close(stmt); | |
SQLUtil.close(con); | |
} | |
} |
張貼留言