實時查詢引擎 - Facebook Presto 介紹與應用

1. Presto 是什麼

  Facebook presto是什麼,繼Facebook建立了HIVE神器後的又一以SQL語言做爲接口的分佈式實時查詢引擎,能夠對PB級的數據進行快速的交互式查詢。它支持標準的ANSI SQL.包含查詢,聚合,JOIN以及窗口函數等。除了Facebook這個創造都在使用外,國內像京東,美團等也都有普遍的使用。對於英文很差的同窗能夠訪問由京東建立的這個中文翻譯站點:http://prestodb-china.com/,只是這個版本才0.100,如今最新版已到0.156.node

Presto是一個開源的分佈式SQL查詢引擎,適用於交互式分析查詢,數據量支持GB到PB字節。sql

Presto的設計和編寫徹底是爲了解決像Facebook這樣規模的商業數據倉庫的交互式分析和處理速度的問題。shell

它能夠作什麼?

Presto支持在線數據查詢,包括Hive, Cassandra, 關係數據庫以及專有數據存儲。 一條Presto查詢能夠將多個數據源的數據進行合併,能夠跨越整個組織進行分析。數據庫

Presto以分析師的需求做爲目標,他們指望響應時間小於1秒到幾分鐘。 Presto終結了數據分析的兩難選擇,要麼使用速度快的昂貴的商業方案,要麼使用消耗大量硬件的慢速的「免費」方案。json

誰在使用它?

Facebook使用Presto進行交互式查詢,用於多個內部數據存儲,包括300PB的數據倉庫。 天天有1000多名Facebook員工使用Presto,執行查詢次數超過30000次,掃描數據總量超過1PB。安全

領先的互聯網公司包括Airbnb和Dropbox都在使用Presto。ruby

 

2. Presto 結構

  Presto一樣是須要部署到每個DataNode上的分佈式系統,它包括一個coordinator和多個worker: 
Presto結構服務器

  • Coordinator: 接入接口,解析SQL語句,生成查詢計劃,任務分發等。
  • Worker:負責與數據的讀寫交互以及執行查詢計劃

  值得一提的是Presto以插件形式對數據存儲層進行了抽象,它叫作鏈接器,如:Cassandra Connector,Hive Connector,MySQL Connector等,能夠看出它不只默認提供了Hadoop相關組件的鏈接器,還提供了Mysql, Postgresql等RDBMS的鏈接器,同時也能夠方便的經過自定義鏈接器開發,達到適用於不一樣數據存儲層的擴展目的。markdown

  Presto提供如下幾種類型的使用接口:jvm

  • Presto命令行
  • JDBC驅動

3. Presto安裝

  Presto只支持Linux系統的部署。它的Worker節點同時也能夠做爲Coordinator節點,可是Presto建議獨立部署Coordinator節點,並採用獨立服務器進行部署,避免性能影響。 
  本文的測試環境爲基於CDH 5.5的Hadoop集羣環境的安裝和測試。Presto 版本:0.152.3. Presto 的安裝JDK版本必需要求:1.8. 安全方式爲獨立Coordinator節點+Worker節點的方式

 1. 解壓Presto到每一臺Worker節點和Coordinator節點

tar -xzvf presto-server-0.152.3.tar.gz
  • 1

 2. 配置node.properties 
   node.properties包含了Presto的節點配置信息,在解壓後目錄的 etc/node.properties位置。,如:

node.environment=myprestoproduction #所有相同的集羣名字,經測試不能大小寫混合 node.id=ffffffff-ffff-ffff-ffff-fffffffffff1 #這個每一個presto節點ID都須要不同,可在後面數字遞增 node.data-dir=/usr/local/presto-server-0.152.3/data #presto數據存儲目錄,放在瞭解壓軟件目錄下
  • 1
  • 2
  • 3

 3. 配置jvm.config 
  jvm.config這個配置文件經過名字,你們應該知道是配置什麼了吧。內容以下:

-server -Xmx8G -XX:+UseG1GC -XX:G1HeapRegionSize=32M -XX:+UseGCOverheadLimit -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError=kill -9 %p
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

 4. Presto配置:config.properties 
  config.properties配置文件用於配置Presto的運行參數,這裏Coordinator與Workder節點須要分開配置不一樣的內容。 
  Coordinator節點配置:

coordinator=true #這裏指定做爲coordinator節點運行 node-scheduler.include-coordinator=false http-server.http.port=8089 query.max-memory=50GB #單個查詢可用的總內存 query.max-memory-per-node=1GB #單個查詢單個節點的可用最大內存 discovery-server.enabled=true #Discovery服務用於Presto集羣的節點狀態服務 discovery.uri=http://master:8089
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  Worker節點配置:

coordinator=false http-server.http.port=8089 query.max-memory=50GB query.max-memory-per-node=1GB discovery.uri=http://master:8089
  • 1
  • 2
  • 3
  • 4
  • 5
  • 至此就能夠正常啓動Presto了。可是沒有配置任何Connector的Presto也只能拿來看一看了,因此下面仍是先把Hive Connector配置好。

 5. 預先配置好Hive Connector 
  新建好文件: etc/catalog/hive.properties,內容爲:

connector.name=hive-cdh5 # 根據Hadoop版本狀況,值能夠是: hive-hadoop1, hive-hadoop2,hive-cdh4,hive-cdh5 hive.metastore.uri=thrift://master:9083 # hive的MetaStore服務URL hive.config.resources=/etc/hadoop/conf/core-site.xml,/etc/hadoop/conf/hdfs-site.xml
  • 1
  • 2
  • 3

 6. 啓動Presto 
  通過這麼多的配置,終於能夠到了啓動Presto這一步,這一步就簡單了,直接在每個節點上執行啓動命令便可:

bin/launcher start
  • 1
  • 值得注意的是, CDH安裝的JDK版本爲1.7,而Presto要求的版本是1.8.所以須要更改launcher文件,在前面增長JAVA環境變量設置,覆蓋默認的1.7設置。

 7. 鏈接到Presto 
  使用命令行鏈接到Presto: 
  但是怎會如此輕鬆就能讓你連上去,你得下載一個文件:presto-cli-0.156-executable.jar,而後重命名爲presto,並增長可執行權限(chmod +x),後能夠執行鏈接命令:

./presto --server master:8089 presto:default> SELECT * FROM system.runtime.nodes; node_id | http_uri | node_version | coordinator | state --------------------------------------+---------------------------+--------------+-------------+-------- ffffffff-ffff-ffff-ffff-fffffffffff2 | http://192.168.5.202:8089 | 0.152.3 | false | active ffffffff-ffff-ffff-ffff-fffffffffff1 | http://192.168.5.200:8089 | 0.152.3 | true | active ffffffff-ffff-ffff-ffff-fffffffffff3 | http://192.168.5.203:8089 | 0.152.3 | false | active ffffffff-ffff-ffff-ffff-fffffffffff4 | http://192.168.5.204:8089 | 0.152.3 | false | active (4 rows) Query 20161108_101627_00016_3i6da, FINISHED, 2 nodes Splits: 2 total, 2 done (100.00%) 0:00 [4 rows, 300B] [9 rows/s, 727B/s]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

  system鏈接器是Presto自帶的鏈接器,不須要配置。包含Presto的節點信息,配置信息以及metrics信息等。

5. 使用Presto查詢HIVE表數據

 1. 使用命令行鏈接到Presto,並指定使用HIVE鏈接器:

./presto-cli-0.107-jd-executable.jar --server master:8089 --catalog hive --schema default #指定默認鏈接到HIVE的default數據庫
  • 1
  • 2

 2. 查詢HIVE表數據,接下來就可使用標準SQL查詢HIVE數據,如:

presto:default> desc sample_08;
   Column    | Type | Comment -------------+---------+--------- code | varchar | description | varchar | total_emp | integer | salary | integer | (4 rows) Query 20161108_145619_00028_3i6da, FINISHED, 2 nodes Splits: 2 total, 2 done (100.00%) 0:00 [4 rows, 258B] [11 rows/s, 726B/s] presto:default> select * from sample_08 limit 3; code | description | total_emp | salary ---------+------------------------+-----------+-------- 00-0000 | All Occupations | 135185230 | 42270 11-0000 | Management occupations | 6152650 | 100310 11-1011 | Chief executives | 301930 | 160440 (3 rows) Query 20161108_145632_00029_3i6da, FINISHED, 2 nodes Splits: 2 total, 2 done (100.00%) 0:02 [823 rows, 45KB] [439 rows/s, 24KB/s] 

 

  • 12
  • 13
  • 14

6. 問題

 1. 在使用Presto查詢Parquet格式中的Decimal數據類型時會出現異常,須要手動轉換:

presto:default> desc test_decimal; Column | Type | Comment ----------+---------------+--------- dec_col | decimal(2,0) | (1 rows) Query 20161108_151431_00066_3i6da, FINISHED, 2 nodes Splits: 2 total, 2 done (100.00%) 0:00 [5 rows, 358B] [14 rows/s, 1.02KB/s] presto:default> select dec_col from test_decimal limit 1; Query is gone (server restarted?) #這兒產生異常了 presto:default> select cast(dec_col as integer) from test_decimal limit 1; _col0 ------- 1 (1 row) Query 20161108_151456_00068_3i6da, FINISHED, 1 node Splits: 2 total, 2 done (100.00%) 0:00 [7.28K rows, 118KB] [19.5K rows/s, 314KB/s]

 2. 另一個就是Presto的異常信息太簡結了,不少都是Query is gone,很很差排查,如:

presto:default> explain select * from sample_08; Query is gone (server restarted?)

 3. 兼容性問題,好比:

presto:default> select * from sample_tabpart limit 10; Query 20161109_031436_00013_3i6da failed: Unsupported Hive type char(4) found in partition keys of table default.sample_tabpart # 不支持以CHAR爲類型的分區KEY

7. 最後

  Facebook presto雖然發展時間不長,版本也還不高,但當前版本在功能上已比較豐富,並且在查詢效率上已達到了近乎實時的要求,且很是靈活。Presto將會成爲實時查詢工具上的一個重要選擇。

轉:http://blog.csdn.net/hezh914/article/details/53097853

相關文章
相關標籤/搜索