1. 程式人生 > >[喵咪大資料]Presto查詢引擎

[喵咪大資料]Presto查詢引擎

如果大家正在按照筆者的教程嘗試使用大資料元件還是之前有使用過相關的元件,大家會發現一個問題HIVE在負責的查詢下呼叫Mapreduce會很慢,在這個場景下就湧現出很多查詢引擎來優化,比如大家熟悉的Spark-SQL,Impala,kilin已經今天的主角Presto, Presto以速度和極強的擴充套件性取得了勝利,不僅能夠提高對HIVE資料查詢速度還能和異構資料庫進行關聯查詢,比如HIVE和Mysql進行關聯查詢,那麼我們就來迫不及待的揭開Presto的廬山真面目

附上:

1.安裝Presto

ca /app/install
wget https://repo1.maven.org
/maven2/com/facebook/presto/presto-server/0.184/presto-server-0.184.tar.gz tar -zxvf presto-server-0.184.tar.gz mv presto-server-0.184 /usr/local/presto-0.184

設定環境變數

vim /etc/profile
# presto
export PRESTO=/usr/local/presto-0.184
export PATH=$PRESTO/bin:$PATH
source /etc/profile

配置檔案

先進入到presto根目錄下 cd /usr/local/presto-0.184

配置節點資訊

vim etc/node.properties
node.environment=production
node.id=ffffffff-ffff-ffff-ffff-ffffffffffff
node.data-dir=/usr/local/presto-0.184/data

配置jvm相關引數

vim etc/jvm.config
-server
-Xmx16G
-XX:+UseConcMarkSweepGC
-XX:+ExplicitGCInvokesConcurrent
-XX:+CMSClassUnloadingEnabled                                                                                                     -XX
:+AggressiveOpts
-XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError=kill -9 %p -XX:ReservedCodeCacheSize=150M

Presto Server 相關的配置,每一個 Presto Server 可以通時作為 coordinator 和 worker 使用。你可以將他們配置在一個極點上,但是,在一個大的叢集上建議分開配置以提高效能。

vim etc/config.properties
coordinator=true
node-scheduler.include-coordinator=true
http-server.http.port=8080
discovery-server.enabled=true
discovery.uri=http://hadoop-1:8080

coordinator 的最小配置:

coordinator=true
node-scheduler.include-coordinator=false
http-server.http.port=8080
task.max-memory=1GB
discovery-server.enabled=true
discovery.uri=http://cdh1:8080

worker 的最小配置:

coordinator=false
http-server.http.port=8080
task.max-memory=1GB
discovery.uri=http://cdh1:8080

可選的,作為測試,你可以在一個節點上同時配置兩者(我們在單節點上使用先選擇這個配置):

coordinator=true
node-scheduler.include-coordinator=true
http-server.http.port=8080
task.max-memory=1GB
discovery-server.enabled=true
discovery.uri=http://cdh1:8080

引數說明:

  • coordinator:Presto 例項是否以 coordinator 對外提供服務
  • node-scheduler.include-coordinator:是否允許在 coordinator 上進行排程任務(單機測試配置為true不然沒有節點可以使用)
  • http-server.http.port:HTTP 服務的埠
  • task.max-memory=1GB:每一個任務(對應一個節點上的一個查詢計劃)所能使用的最大記憶體
  • discovery-server.enabled:是否使用 Discovery service 發現叢集中的每一個節點。
  • discovery.uri:Discovery server 的 url

配置日誌等級

vim etc/log.properties
com.facebook.presto=INFO

Catalog配置

如果你想使用 hive 的聯結器,則建立 hive.properties:

mkdir etc/catalog
vim etc/catalog/hive.properties
connector.name=hive-hadoop2
hive.metastore.uri=thrift://hadoop-1:9083
hive.config.resources=/usr/local/hadoop-2.7.3/etc/hadoop/core-site.xml,/usr/local/hadoop-2.7.3/etc/hadoop/hdfs-site.xml

關於hive的聯結器有以下幾種可以更具安裝的hive版本資訊進行選擇

  • hive-cdh5
  • hive-cdh4
  • hive-hadoop1
  • hive-hadoop2

啟動HIVE metastore 和 hiveserver2

hive --service metastore
hive --service hiveserver2

啟動presto

launcher start  -- 後臺執行
launcher run   --日誌執行
launcher stop  --停止

2.使用presto-cli查詢

cd /usr/local/presto-0.184/bin/
wget https://repo1.maven.org/maven2/com/facebook/presto/presto-cli/0.184/presto-cli-0.184-executable.jar
mv presto-cli-0.184-executable.jar presto-cli
chmod -R 777 presto-cli
presto-cli --server hadoop-1:8080 --catalog hive --schema default

此時就可以正常的執行SQL 了 ,在資料量大的查詢情況下速度基本比Hive快了5-6倍

presto:default> show tables;
     Table      
----------------
 employee       
(11 rows)

Query 20170919_031227_00002_mmfcn, FINISHED, 1 node
Splits: 18 total, 18 done (100.00%)
0:00 [11 rows, 327B] [35 rows/s, 1.03KB/s]

關於查詢出來的資料常常要匯出資料,Presto也提供匯出CSV檔案的方式

presto-cli --server hadoop-1:8080 --catalog hive --schema default --execute "select msn,count(*) from apilog where apiname = 'Classify.categoryAppList' group by msn;"  --output-format CSV_HEADER > Classify.csv

3. 線上管理工具Airpal

cd /usr/local/
git clone https://github.com/airbnb/airpal.git
cd airpal
# 構建Aripal
./gradlew clean shadowJar -Dairpal.useLocalNode

建立mysql資料庫

mysql -u root -p
mysql> CREATE DATABASE airpal;
mysql> USE airpal;
mysql> CREATE USER 'airpal'@'localhost' IDENTIFIED BY 'airpal';
mysql> GRANT ALL ON airpal.* TO 'airpal'@'localhost' IDENTIFIED BY 'airpal';
mysql> GRANT ALL ON airpal.* TO 'airpal'@'%' IDENTIFIED BY 'airpal';
mysql> FLUSH PRIVILEGES;
mysql> quit;

配置檔案設定

cp reference.example.yml reference.yml   
vim reference.yml
    # HTTP-specific options.
    # 最好查詢設定的埠是否被佔用。
    server:
    applicationConnectors:
        - type: http
        port: 8081
        idleTimeout: 10 seconds

    adminConnectors:
        - type: http
        port: 8082

    shiro:
    iniConfigs: ["classpath:shiro_allow_all.ini"]

    dataSourceFactory:
    driverClass: com.mysql.jdbc.Driver
    user: airpal
    password: passwd
    url: jdbc:mysql://localhost:3306/airpal

    flywayFactory:
    locations: ["classpath:db.migration.common", "classpath:db.migration.mysql"]

    # The URL to the Presto coordinator.
    prestoCoordinator: http://prestoCoor:9098

資料庫初始化

java -Duser.timezone=UTC -cp build/libs/airpal-*-all.jar com.airbnb.airpal.AirpalApplication db migrate reference.yml

直接啟動Airpal:

java -server -Duser.timezone=UTC -cp build/libs/airpal-*-all.jar com.airbnb.airpal.AirpalApplication server reference.yml

通過訪問 IP:8081 即可訪問進線上查詢

4 總結

Presto的強大之處不止於此,這裡只是簡單演示通過Presto來提高對HIve的查詢效率,還有更多的功能需要探索,可以參考官網的文件

注:筆者能力有限有說的不對的地方希望大家能夠指出,也希望多多交流!