mumu-presto documentation

presto 简介

Presto是一个开源的分布式SQL查询引擎,用于针对各种大小(从千兆字节到千兆字节)的数据源运行交互式分析查询。

Presto是从头开始设计和编写的,用于交互式分析,接近商业数据仓库的速度,同时扩展到Facebook等组织的规模。

presto是一款基于内存、基于sql的大数据实时查询(Ad-hoc)开源计算框架,由facebook开发。presto使用connector连接不同的数据源,将不同数据源的数据
存储在内存中进行计算,可以跨数据源进行计算。

大数据计算框架主要分为脚本程序和类sql两种方式。脚本程序主要由mapreduce、pig、crunch、sparkRDD、cascading、tez等执行框架。类sql的实时计算框
架包含hive、sparkSQL、impala。其中sparkSQL也是基于内存的计算框架。

脚本分析组件:

Concept Apache Hadoop MapReduce Apache Crunch Apache Pig Apache Spark Cascading Apache Tez
Input Data InputFormat Source LoadFunc InputFormat Tap (Source) | Tez Input
Output Data OutputFormat Target StoreFunc OutputFormat Tap (Sink) | Tez Output
Data Container Abstraction N/A PCollection Relation RDD Pipe Vertex
Data Format Serialization Writables POJOs and PTypes Pig Tuples and Schemas POJOs and Java/Kryo Serialization Cascading Tuples and Schemes Events
Data Transformation Mapper, Reducer, and Combiner DoFn PigLatin and UDFs Functions (Java API) Operations Processor

SQL分析组件:

Concept Apache hive presto sparkSQL impala
多数据源
基于内存
查询速度 极快
支持数据量 一般

presto发展

随着数据量的快速递增,传统数据库已经不能满足大数据量的业务需求,接着出现了hadoop大数据平台,hadoop就是专门来处理大量数据的存储和离线 计算的开源框架。但是hadoop提供的是map-reduce计算框架,这种框架适用于大数据的离线和批量计算,因为该框架考虑的是数据吞吐率而不是计算效 率,所以不能满足大数据快速实时(Ad-hoc)查询。

各大互联网公司和企业也发现map-reduce计算框架在快速查询所表现出来的弊端,各个互联网公司都纷纷进行大数据实时查询的开发,facebook于2012 年开始开发presto,目前产品已经超过facebook1000名雇员开始使用,每天运行超过30000个查询,每日查询数据量在1PB级别。facebook称rpresto比hive 要好上10倍还多,于2013年进行开源发布。

presto特点

presto是专门为大数据实时查询而设计和开发的产品。由于presto是使用java开发,所以极易学习和使用,并且对于特定的场景进行自定义开发和性能优化。 无论是对多数据源的支持,还是高性能、可拓展、易用性等,presto都是大数据产品中的佼佼者,presto的特性如下所示:

多数据源

  • Postgresql Connector
  • Mysql Connector
  • Cassandra Connector
  • Hive Connector
  • Kafka Connector
  • jmx Connector
  • Accumulo Connector
  • Elasticsearch Connector
  • Kudu Connector
  • Local File Connector
  • Memory Connector
  • MongoDB Connector
  • Redis Connector
  • SQL Server Connector
  • Redshift Connector
  • Thrift Connector
  • System Connector
  • TPCDS Connector
  • TPCH Connector

目前presto可以支持数据源包含上面多种connector数据源。而且可以通过spi接口进行自定义数据源接入开发。

支持sql

presto可以完全支持ANSI SQL,并提供了一个shell供客户端查询计算。

可拓展

presto有很好的拓展性,开发人员可以使用spi来编写自己的connector,并且使用sql语句查询和分析自己编写的connector数据源

混合计算

在数据库中每种类型的数据源都对应这一个特定的connector,用户可以根据业务需求在presto中针对一个特定的connector类型配置一个或者多个catalog, 用户可以混合多个catalog进行join查询和计算。

高性能

经过facebook测试计算,presto的性能是hive的10被,是sparkSQL的三倍。

流水线

presto是使用pipeline来进行设计的,查询的时候客户不需要等待所有的结果出来完成之后才能获取到结果,而是计算出来一部分结果之后就反馈给 客户,形成一个流水线操作。

presto基本概念

presto服务进程

presto服务器进程一共有两种服务器进程:coordinator服务进程和worker服务进程,其中coordinator服务进程主要作用是:接收查询请求、生成查询执行计划、
任务调度和worker管理。而worker服务进程则执行被分解后的查询执行计划:task。
_images/presto-coordinator-worker.jpg
coordinator
presto服务器进程部署于集群中的单独节点上,是整个presto集群的管理节点。coordinator服务进程主要作用是接收查询请求,查询语句解析,生成查询执行 计划、stage和task并且对生成的task进行调度。除此之外,还对整个集群的worker节点进行管理。coordinator服务进程是整个集群的master进程,该进程于 worker进程通信从而获取到最新的worker进程信息,又与客户端进程进行通信,获取查询请求。而这些所有功能都是由coordinator进程的RESTFUL服务来实现。
worker
在一个presto集群中存在一个coordinator服务进程和多个worker服务进程,coordinator服务进程是管理节点,而worker进程是工作节点。在每一个worker进程 都有一个worker服务进程,该服务主要进行数据处理和task任务的执行。worker进程每隔固定的时间都会向coordinator服务进程发送心跳,告诉coordinator我 还活着,接受调度。当客户端接受到查询请求的时候,coordinator会从当前存活的节点随机选择worker进程进行task。而worker在执行task的时候又会对数据进 行分片处理,并行进行数据处理。

presto模型

presto可以通过不同的connector访问多种数据源,如hive、kafka、elasticsearch等(详情参照),该节说明presto是如何访问多种不同类型的数据源,并且对
presto模型和概念进行描述。presto可以通过connector连接一个数据源,可以根据一个connector配置多个catalog,一个catalog可以有多个schema,一个
schema可以有多个table。
_images/presto-model.jpg
connector

presto是通过多种多样的connector来访问不同的数据源的。可以将connector当做presto访问数据源的驱动程序,每一个数据源必须有配套的connector。每种 connector都实现了presto标准的spi接口,因此只要你实现了spi接口,就可以编写一个自定义的connector。

当需要使用某种类型的connector的时候,需要在presto的安装目录下的etc/catalog里面创建配置文件,而且在该配置文件中必须要配置connector.name属性 presto的connector manager需要根据connector.name来查找数据源进行访问。

catalog
presto的catalog类似于mysql的数据库实例,而shcema类似于数据库。catalog的名称就是在etc/catalog里面添加的配置文件名称,如果配置了一个 postgres.properties,那么catalog就是postgres。
schema
presto的schema类似于数据库名称,一个catalog和schema确定一个表的明确位置信息。当使用presto去查询hive和mysql的时候,你会发现schema是数据库名称。 而当使用postgresql的时候schema是pg的schema。
table
presto的table和传统数据库的表定义是一样的。

presto查询执行

presto在执行sql语句的时候,将这些sql解析成对应的查询,并且在分布式集群中执行这些查询。
_images/presto-query.png
statement

statement语句就是指我们输入的sql查询语句。presto支持符合ANSI标准的SQL语句。这种语句由字句(Clause)、表达式(Expression)和断言(Predicate)组成。

presto将语句和查询分开主要是由于查询和语句概念不同,语句是输入的sql语句,而查询是根据语句生成的查询执行计划,进而生成可以执行的查询语,代表 着分布到所有worker节点的实际查询操作。

query
presto接收查询语句,生成查询执行计划query。一个查询执行代表这可以在集群中运行的查询,他由运行在worker上的节点和相互关联的阶段stage组成。 一个查询执行由stage、task、driver、operator、和datasource组成,这些组件之间通过内部联系共同组成了一个查询执行,从而得到sql查询结果。
stage

stage即是查询执行阶段,当执行一个sql的时候,presto会把查询分成多个不同阶段的stage,一个stage代表查询执行计划的一部分。通常情况下,stage 之间是树状结构。

每一个查询都有一个root stage,root stage用于聚集其他stage的数据并产生最终结果。stage并不会在集群中实际运行,stage是逻辑 执行计划和建模。

每一个stage(single stage和source stage)都会有输入和输出,都会从上游stage读取数据,然后将结果传递到下游stage。source stage没有上游stage,single没有下游stage。

_images/presto-stage.jpg

presto包含四种类型的stage:

  • Coordinay_Only

这种stage用于执行ddl、dml表结构创建和修改。

  • Single

这种stage聚合子stage结果,并将最终数据返回到客户端程序。

  • fixed

这种stage用于接收子stage产生的数据,并且在集群中对这些数据进行聚合和分组操作。

  • source

这种stage直接接收数据源数据,从数据源读取数据,该阶段也会对sql进行优化,如数据过滤和断言下发。

exchange

presto是通过exchange来连接stage和stage的。exchage用来完成有上下游关系的stage之间的数据交换。

在presto中有两种类型的exchange,output buffer 和exchange client。生产数据的stage通过output buffer的exchange将数据传递给下游stage。消费数据 的stage通过名为exchange client的exchange获取上游stage的数据。

task

stage并不会在presto集群中实际执行,每一个stage都是由多个task组成,task是实际运行在presto的worker节点上。

在presto中一个查询被分解中多个stage阶段,每一个stage阶段又按照业务逻辑分成多个任务,每个任务处理一个或者多个分片,每个分片是由一个driver 程序来实际运行,通过这种分解来并行执行任务。

driver
一个task包含多个driver,每一个driver是对一个split分片的operator操作集合。因此一个driver处理一个split,并且由将结果传递到下一个stage的 task任务中,一个driver拥有一个输入和一个输出。
operator
一个operator是对一个split分片的实际操作,过过滤、加权、转换等。一个operator依次读取split的数据,并产生输出。operator每次读取一个page, 也输出一个page。
split
一个split是数据集的子集。当presto执行查询的时候,首先会从coordinator获取一个表所对应的split,然后presto会指定查询计划,选择合适的worker 去执行split。这里通常会用到数据本地化,就是尽量将worker分配在数据存储的地方。
page

page是presto数据处理单元最小的单元。一个page对象包含多个block对象,而一个block对象是一个字段的多行数据。多个block横切的一行就是presto 真实的一行数据。一个page最大为1M,最多16*1024行。

_images/presto-page.png

presto架构

Presto是一个在一组机器上运行的分布式系统。 完整安装包括协调员和多个工作人员。 查询从客户端(Presto CLI、presto-jdbc)提交给协调器。 协调器解析, 分析和计划查询执行,然后将处理分发给worker工作进程。

presto硬件架构

_images/presto-architecture-machine.png

presto软件架构

_images/presto-architecture-soft.png

presto VS hive

Presto是一个开源的分布式SQL查询引擎,用于针对各种大小(从千兆字节到千兆字节)的数据源运行交互式分析查询。

presto VS spark

Presto是一个开源的分布式SQL查询引擎,用于针对各种大小(从千兆字节到千兆字节)的数据源运行交互式分析查询。

presto 安装部署

presto目前只能部署在linxu操作系统中,暂时不支持windows操作系统。prsto是使用java来进行编写的,因此linux环境必须先要安装java1.8版本。 presto可以部署在单节点上,也可以部署在集群中。

环境准备

presto是java程序开发的项目,所以项目最低依赖于java。

java安装

1、从oracle下载jdk1.8.12+版本的java。

wget https://download.oracle.com/otn/java/jdk/8u221-b11/230deb18db3e4014bb8e3e8324f81b43/jdk-8u221-linux-x64.tar.gz

2、解压jdk到、/usr/local目录

tar -xzvf jdk-8u221-linux-x64.tar.gz -C /usr/local

3、配置JAVA_HOME和path

JAVA_HOME=/usr/local/jdk1.8.0_221
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

单节点安装

github下载安装包

wget https://github.com/prestodb/presto/archive/0.217.tar.gz

解压安装包

tar -xzvf presto-server-0.217.tar.gz -C /usr/local
_images/presto-dirs.png

修改config.properties配置

_images/presto-config.png
  • coordinator

    是否该服务器作为presto的主节点(coordinator服务)。单机模式需要将该值配置为true。

  • node-scheduler.include-coordinator

    是否启用coordinator进程来进行数据计算。单机模式需要将该值配置为true。

  • http-server.http.port

    presto服务端口

  • query.max-memory

    单个query查询最大内存

  • query.max-memory-per-node

    单个节点最大内存

  • query.max-total-memory-per-node

    单个节点总内存

  • discovery-server.enabled

    是否开启discovery服务,默认discovery服务和coordinator服务绑定在一起。如果coordinator配置为true,那么discovery-server.enabled也需要配置为true。

  • query.discovery.uri

    discovery服务uri地址信息,worker节点通过discovery服务来找到master。

修改jvm.properties配置

_images/presto-jvm.png
  • server

    以java服务端进程启动

  • xmx

    jvm最大堆内存,该值需要和query.max-memory-per-node之间有关联,不能够query.max-memory-per-node的值大于jvm最大堆大小。

query.max-memory-per-node < xmx*0.8
xmx > query.max-memory-per-node/0.8
  • xx

    UseG1GC使用的是g1的gc收集器进行新生代和老年代gc收集,目前g1是最新性能比较好的gc收集器。

修改node.properties配置

_images/presto-node.png
  • node.environment

    presto集群名称

  • node.id

    presto的worker节点id

  • node.data-dir

    presto的日志目录,该目录无需手动创建,程序会自动创建。

修改log.properties配置

_images/presto-log.png
  • com.facebook.presto

    presto的log日志级别

presto服务启动

./launcher start
./launcher restart
_images/presto-startuplog.png

集群安装

github下载安装包

wget https://github.com/prestodb/presto/archive/0.217.tar.gz

解压安装包

tar -xzvf presto-server-0.217.tar.gz -C /usr/local
_images/presto-dirs.png

coordinator配置

coordinator服务器配置信息,主要包含config.properties和node.properties两方面配置信息。

config.properties配置
coordinator=true
node-scheduler.include-coordinator=false
http-server.http.port=9001
query.max-memory=5GB
query.max-memory-per-node=512MB
query.max-total-memory-per-node=512MB
discovery-server.enabled=true
discovery.uri=http://172.31.134.225:9001
node.properties配置
node.environment=production
node.id=ffffffff-ffff-ffff-ffff-ffffffffff01
node.data-dir=/opt/presto/data217

worker配置

worker服务器配置信息,主要包含config.properties和node.properties两方面配置信息。

config.properties配置
coordinator=false
node-scheduler.include-coordinator=false
http-server.http.port=9001
query.max-memory=5GB
query.max-memory-per-node=512MB
query.max-total-memory-per-node=512MB
discovery-server.enabled=false
discovery.uri=http://172.31.134.225:9001
node.properties配置

node.environment配置信息在worker节点和coordinator节点需要保持一致。node.id配置信息必须要唯一。node.data-dir配置目录建议保持一致,方便日志定位。

node.environment=production
node.id=ffffffff-ffff-ffff-ffff-ffffffffff02
node.data-dir=/opt/presto/data217

修改jvm.properties配置

-server
-Xmx1G
-XX:+UseG1GC
-XX:G1HeapRegionSize=32M
-XX:+UseGCOverheadLimit
-XX:+ExplicitGCInvokesConcurrent
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError
  • server

    以java服务端进程启动

  • xmx

    jvm最大堆内存,该值需要和query.max-memory-per-node之间有关联,不能够query.max-memory-per-node的值大于jvm最大堆大小。

query.max-memory-per-node < xmx*0.8
xmx > query.max-memory-per-node/0.8
  • xx

    UseG1GC使用的是g1的gc收集器进行新生代和老年代gc收集,目前g1是最新性能比较好的gc收集器。

修改log.properties配置

com.facebook.presto=INFO
  • com.facebook.presto

    presto的log日志级别

presto服务启动

./launcher start
./launcher restart

 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.max-request-retries          5                  5                  Maximum number of Elasticsearch request retries
 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.max-request-retry-time       10.00s             10.00s             Use exponential backoff starting at 1s up to the value specified by this configuration when retrying failed requests
 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.request-timeout              100.00ms           120.00s            Elasticsearch request timeout
 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.scroll-size                  1000               1000000            Scroll batch size
 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.scroll-timeout               1.00s              60000.00ms         Scroll timeout
 2019-08-20T17:07:15.791+0800    INFO    main    Bootstrap       elasticsearch.table-description-directory  etc/elasticsearch  etc/elasticsearch  Directory that contains JSON table description files
 2019-08-20T17:07:18.813+0800    INFO    main    stderr  ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
 2019-08-20T17:07:24.028+0800    INFO    main    com.facebook.presto.metadata.StaticCatalogStore -- Added catalog elasticsearch using connector elasticsearch --
 2019-08-20T17:07:24.035+0800    INFO    main    com.facebook.presto.security.AccessControlManager       -- Loading system access control --
 2019-08-20T17:07:24.037+0800    INFO    main    com.facebook.presto.security.AccessControlManager       -- Loaded system access control allow-all --
 2019-08-20T17:07:24.225+0800    INFO    main    com.facebook.presto.server.PrestoServer ======== SERVER STARTED ========

源码编译

git安装

apt install git

maven安装

apt install maven

源码下载

git clone https://github.com/prestodb/presto.git

客户端安装

cli下载

wget https://repo1.maven.org/maven2/com/facebook/presto/presto-cli/0.224/presto-cli-0.224-executable.jar

制作可执行客户端

mv presto-cli-0.224-executable.jar presto
chmod +x presto

客户端使用

./presto --server localhost:9001 --schema default --catalog elasticsearch

软连接建立

ln -s /usr/local/presto-server-0.217/presto /usr/local/bin/presto

presto校验

verifier下载

wget https://repo1.maven.org/maven2/com/facebook/presto/presto-verifier/0.217/presto-verifier-0.217-executable.jar

制作可执行客户端

mv presto-verifier-0.224-executable.jar verifier
chmod +x verifier

表创建

CREATE TABLE verifier_queries (
   id int(11) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
   suite varchar(256) NOT NULL,
   name varchar(256) DEFAULT NULL,
   control_catalog varchar(256) NOT NULL,
   control_schema varchar(256) NOT NULL,
   control_query text NOT NULL,
   test_catalog varchar(256) NOT NULL,
   test_schema varchar(256) NOT NULL,
   test_query text NOT NULL,
   control_username varchar(256) NOT NULL DEFAULT 'verifier-test',
   control_password varchar(256) DEFAULT NULL,
   test_username varchar(256) NOT NULL DEFAULT 'verifier-test',
   test_password varchar(256) DEFAULT NULL,
   session_properties_json varchar(2048) DEFAULT NULL)

config.properties

source-query.suite=my_suite
source-query.database=jdbc:mysql://localhost:3306/my_database?user=my_username&password=my_password
control.gateway=jdbc:presto://localhost:8080
test.gateway=jdbc:presto://localhost:8081
test-id=1

客户端使用

./verifier verify config.properties

软连接建立

ln -s /usr/local/presto-server-0.217/verifier /usr/local/bin/verifier

benchmark性能测试

benchmark下载

wget https://repo1.maven.org/maven2/com/facebook/presto/presto-benchmark-driver/0.217/presto-benchmark-driver-0.217-executable.jar

制作可执行客户端

mv presto-benchmark-driver-0.217-executable.jar presto-benchmark-driver
chmod +x presto-benchmark-driver

使用方式

root@docker:/usr/local/presto-server-0.217# ./presto-benchmark-driver --help
NAME
        presto-benchmark - Presto benchmark driver

SYNOPSIS
        presto-benchmark [--catalog <catalog>]
                [--client-request-timeout <client request timeout>] [--debug]
                [(-h | --help)] [--max-failures <max failures>] [--query <query>...]
                [--runs <runs>] [--schema <schema>] [--server <server>]
                [--session <session>...] [--socks <socks>] [--sql <sql>]
                [--suite <suite>...] [--suite-config <suite-config>] [--user <user>]
                [--warm <warm>]

OPTIONS
        --catalog <catalog>
            Default catalog

        --client-request-timeout <client request timeout>
            Client request timeout (default: 2m)

        --debug
            Enable debug information (default: false)

        -h, --help
            Display help information

        --max-failures <max failures>
            Max number of consecutive failures before benchmark fails

        --query <query>
            Queries to execute

        --runs <runs>
            Number of times to run each query (default: 3)

        --schema <schema>
            Default schema

        --server <server>
            Presto server location (default: localhost:8080)

        --session <session>
            Session property (property can be used multiple times; format is
            key=value)

        --socks <socks>
            Socks proxy to use

        --sql <sql>
            Directory containing sql files (default: sql)

        --suite <suite>
            Suite to execute

        --suite-config <suite-config>
            Suites configuration file (default: suite.json)

        --user <user>
            Username

        --warm <warm>
            Number of times to run each query for a warm-up (default: 1)

创建suite.json

root@docker:/usr/local/presto-server-0.217# vim suite.json
{
    "file_formats": {
         "query": ["test.*"],
         "schema": ["test.*"],
         "session": {}
    }
}
  • file_formats

    测试suite单元

  • query

    匹配sql文件的sql正则表达式

  • schema

    匹配的schema正则表达式

  • session 查询携带的参数信息

创建sql文件

root@docker:mkdir sql

root@docker:/usr/local/presto-server-0.217# vim sql/test.sql

select count(1) from hive.test.t_ods_industry_atd;

执行benchmark

在同目录下创建sql目录,然后在目录里面添加test.sql,每个sql文件只能包含一条sql语句,不能以','号分割多条sql语句。

./presto-benchmark-driver --server 172.31.134.225:9001  --debug --suite-config suite.json  --catalog hive --warm 10

root@docker:/usr/local/presto-server-0.217# ./presto-benchmark-driver --server 172.31.134.225:9001  --debug  --catalog hive  --warm 10
2019-08-21T09:41:27.936+0800        INFO    main    io.airlift.log.Logging  Logging to stderr
suite       query   compression     format  scale   wallTimeP50     wallTimeMean    wallTimeStd     processCpuTimeP50       processCpuTimeMean      processCpuTimeStd       queryCpuTimeP50 queryCpuTimeMean        queryCpuTimeStstatus    error
file_formats        test                            742     773     79      2630    2746    311     216     217   12        pass
  • suite

    执行的suite

  • query

    执行的sql文件

  • compression

    压缩方式,none、snappy、zlib

  • format

    文件存储格式

  • scale

    执行次数,compression、format、scale参数都是从schema匹配 "tpch_sf(?<scale>.*)_(?<format>orc)_(?<compression>.*?)"获取的数据类型, 如tpch_sf100_orc_snappy。

  • WallTime

    用户要可以看到查询结果要等待的时间,median, mean and standard deviation of the query runs

  • processCpuTime

    整个集群为助理查询而消耗的CPU时间,包含一些垃圾回收的时间,median, mean and standard deviation of the query runs

  • queryCpuTime

    整个集群为助理查询而消耗的CPU时间,median, mean and standard deviation of the query runs

webUI界面

aripal webUI

aripal是airbnb专门为presto开发的一个web界面,可以通过web界面,对presto指定查询,做bi分析。详情查看: https://github.com/airbnb/airpal

aripal下载
wget https://github.com/airbnb/airpal/archive/master.zip
aripal环境
apt install nodejs
apt install npm
apt install gradle
aripal编译
unzip master.zip
cd airpal-master

修改build.gradle 添加postgresql依赖
compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.34'
compile group: 'org.postgresql', name: 'postgresql', version:'42.2.5'
compile group: 'com.h2database', name: 'h2', version:'1.4.190'

./gradlew clean shadowJar
aripal配置
root@docker:/usr/local/soft/airpal-master# vim reference.yml
# Logging settings
logging:

  loggers:
     org.apache.shiro: INFO

 # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
  level: INFO

# 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: org.postgresql.Driver
  user: ads
  password: ads@123
  url: jdbc:postgresql://172.31.134.225:5432/hue

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

# The URL to the Presto coordinator.
prestoCoordinator: http://172.31.134.225:9001
aripal运行
java -Duser.timezone=UTC -cp build/libs/airpal-*-all.jar com.airbnb.airpal.AirpalApplication db migrate reference.yml

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

shib webUI

shib是专门为SQL-Like 大数据分析组件设计的来进行数据查询、数据统计的bi工具,支持hive、hive2、presto、bigQuery等大数据工具。详情查看: https://github.com/tagomoris/shib

shib下载
wget https://github.com/tagomoris/shib/archive/v1.0.2.tar.gz
shib依赖环境
apt install nodejs
apt install npm
npm install -g cnpm --registry=https://registry.npm.taobao.org
shib安装
tar -xzvf shib-1.0.2.tar.gz -C /usr/local
cd /usr/local/
cnpm install
hiveserver2配置
vi config.js

var servers = exports.servers = {
  listen: 3000,
  fetch_lines: 1000,
  query_timeout: 30, // 30 seconds for Presto query timeouts (it will fail)
  setup_queries: [],
  storage: {
    datadir: './var'
  },
  engines: [
    { label: 'hivecluster1',
      executer: {
        name: 'presto',
        host: '172.31.134.225',
        port: 9001,
        user: 'admin',
        catalog: 'hive',
        support_database: true,
        default_database: 'test'
      },
      monitor: null
    },
  ],
};
presto配置
vi config.js

var servers = exports.servers = {
  listen: 3000,
  fetch_lines: 1000,
  query_timeout: 30,
  setup_queries: [],
  storage: {
    datadir: './var'
  },
  engines: [
    { label: 'prestocluster1',
      executer: {
        name: 'presto',
        host: '172.31.134.225',
        port: 9001,
        user: 'admin',
        catalog: 'hive',
        support_database: true,
        default_database: 'test'
      },
      monitor: null
    },
  ],
};
shib运行
npm start
_images/presto-shib.png

hue webUI

Hue是cdh专门的一套web管理器,它包括3个部分hue ui,hue server,hue db。hue提供所有的cdh组件的shell界面的接口。你可以在hue编写mr, 查看修改hdfs的文件,管理hive的元数据,运行Sqoop,编写Oozie工作流等大量工作。详情查看: https://docs.gethue.com/

hue暂时不支持查询presto,但是可以通过以下方式使其支持查询。

Kubernetes
helm repo add gethue https://helm.gethue.com
helm repo update
helm install gethue/hue
docker安装
docker run -it -p 8888:8888 gethue/hue:latest

presto 使用

Presto is a distributed SQL query engine designed to query large data sets distributed over one or more heterogeneous data sources.

presto客户端使用

presto提供了客户端查询进行sql查询。而且在客户端中可以自己指定catalog和schema,非常方便快速的定位到我们需要查询的表。

帮助文档

root@docker:/usr/local/presto-server-0.217# ./presto --help
NAME
        presto - Presto interactive console

SYNOPSIS
        presto [--access-token <access token>] [--catalog <catalog>]
                [--client-info <client-info>]
                [--client-request-timeout <client request timeout>]
                [--client-tags <client tags>] [--debug] [--execute <execute>]
                [(-f <file> | --file <file>)] [(-h | --help)]
                [--http-proxy <http-proxy>] [--ignore-errors]
                [--keystore-password <keystore password>]
                [--keystore-path <keystore path>]
                [--krb5-config-path <krb5 config path>]
                [--krb5-credential-cache-path <krb5 credential cache path>]
                [--krb5-disable-remote-service-hostname-canonicalization]
                [--krb5-keytab-path <krb5 keytab path>]
                [--krb5-principal <krb5 principal>]
                [--krb5-remote-service-name <krb5 remote service name>]
                [--log-levels-file <log levels file>] [--output-format <output-format>]
                [--password] [--resource-estimate <resource-estimate>...]
                [--schema <schema>] [--server <server>] [--session <session>...]
                [--socks-proxy <socks-proxy>] [--source <source>]
                [--truststore-password <truststore password>]
                [--truststore-path <truststore path>] [--user <user>] [--version]

OPTIONS
        --access-token <access token>
            Access token

        --catalog <catalog>
            Default catalog

        --client-info <client-info>
            Extra information about client making query

        --client-request-timeout <client request timeout>
            Client request timeout (default: 2m)

        --client-tags <client tags>
            Client tags

        --debug
            Enable debug information

        --execute <execute>
            Execute specified statements and exit

        -f <file>, --file <file>
            Execute statements from file and exit

        -h, --help
            Display help information

        --http-proxy <http-proxy>
            HTTP proxy to use for server connections

        --ignore-errors
            Continue processing in batch mode when an error occurs (default is
            to exit immediately)

        --keystore-password <keystore password>
            Keystore password

        --keystore-path <keystore path>
            Keystore path

        --krb5-config-path <krb5 config path>
            Kerberos config file path (default: /etc/krb5.conf)

        --krb5-credential-cache-path <krb5 credential cache path>
            Kerberos credential cache path

        --krb5-disable-remote-service-hostname-canonicalization
            Disable service hostname canonicalization using the DNS reverse
            lookup

        --krb5-keytab-path <krb5 keytab path>
            Kerberos key table path (default: /etc/krb5.keytab)

        --krb5-principal <krb5 principal>
            Kerberos principal to be used

        --krb5-remote-service-name <krb5 remote service name>
            Remote peer's kerberos service name

        --log-levels-file <log levels file>
            Configure log levels for debugging using this file

        --output-format <output-format>
            Output format for batch mode [ALIGNED, VERTICAL, CSV, TSV,
            CSV_HEADER, TSV_HEADER, NULL] (default: CSV)

        --password
            Prompt for password

        --resource-estimate <resource-estimate>
            Resource estimate (property can be used multiple times; format is
            key=value)

        --schema <schema>
            Default schema

        --server <server>
            Presto server location (default: localhost:8080)

        --session <session>
            Session property (property can be used multiple times; format is
            key=value; use 'SHOW SESSION' to see available properties)

        --socks-proxy <socks-proxy>
            SOCKS proxy to use for server connections

        --source <source>
            Name of source making query

        --truststore-password <truststore password>
            Truststore password

        --truststore-path <truststore path>
            Truststore path

        --user <user>
            Username

        --version
            Display version information and exit

使用方式

./presto --server localhost:9001 --catalog hive --schema default

制作脚本

echo "./presto --server localhost:9001 --catalog hive --schema default" > presto-cli-hive
chmod +x presto-cli-hive
ln -s /usr/local/presto-server-0.217/presto-cli-hive /usr/local/bin/

执行sql语句

root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --execute "show catalogs";
"elasticsearch"
"hive"
"kafka"
"postgres"
"system"
root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --execute "show tables;";
"t_ods_industry_flow"
root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --execute "select count(1) from t_ods_industry_flow";
"11904"

执行sql文件

echo "select count(1) from t_ods_industry_flow;" > hive_default_flow.sql
root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --file hive_default_flow.sql
"11904"

sql结果导出

root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --execute "select count(1) from t_ods_industry_flow" --output-format CSV > result.csv
root@docker:/usr/local/presto-server-0.217# cat result.csv
"11904"

root@docker:/usr/local/presto-server-0.217# ./presto --server localhost:9001 --schema default --catalog hive --file hive_default_flow.sql --output-format CSV > result2.csv
root@docker:/usr/local/presto-server-0.217# cat result2.csv
"11904"

presto的show展示

presto使用show命令可以查看CATALOGS, COLUMNS, CREATE, FUNCTIONS, GRANTS, SCHEMAS, SESSION, STATS, TABLES信息。

catalogs

查看presto服务中接入的catalog

presto:default> show catalogs;
Catalog
---------------
 elasticsearch
 hive
 kafka
 postgres
 system
(5 rows)

Query 20190829_090915_00005_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]

schemas

presto:default> show schemas;
   Schema
--------------------
 default
 information_schema
 test
(3 rows)

Query 20190829_091122_00007_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [3 rows, 44B] [9 rows/s, 142B/s]

presto:default> show schemas from hive;
       Schema
--------------------
 default
 information_schema
 test
(3 rows)

Query 20190829_091132_00008_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [3 rows, 44B] [10 rows/s, 159B/s]

tables

presto:default> show tables;
    Table
---------------------
 t_ods_industry_flow
(1 row)

Query 20190829_091440_00017_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:01 [1 rows, 36B] [1 rows/s, 71B/s]

presto:default> show tables from test;
            Table
------------------------------
 t_ods_industry_atd
 t_ods_industry_atd_sp
 t_ods_industry_flow
 t_ods_industry_flow_test
 t_ods_industry_gynetres
 t_ods_industry_gynetres_test
(6 rows)

Query 20190829_091448_00018_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:01 [6 rows, 217B] [4 rows/s, 175B/s]

presto:default> show tables from elasticsearch.default;
        Table
----------------------
 guangdong_jmr_sample
 jmr_ipunit
(2 rows)

Query 20190829_091454_00019_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [2 rows, 64B] [5 rows/s, 179B/s]

columns

类似与 show create table table 和 desc table

presto:information_schema> show COLUMNS from columns;
      Column      |  Type   | Extra | Comment
------------------+---------+-------+---------
 table_catalog    | varchar |       |
 table_schema     | varchar |       |
 table_name       | varchar |       |
 column_name      | varchar |       |
 ordinal_position | bigint  |       |
 column_default   | varchar |       |
 is_nullable      | varchar |       |
 data_type        | varchar |       |
 comment          | varchar |       |
 extra_info       | varchar |       |
(10 rows)

Query 20190829_092021_00037_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [10 rows, 822B] [22 rows/s, 1.82KB/s]

functions

presto:test> show functions;
            Function             |         Return Type          |                                 Argument Type
---------------------------------+------------------------------+----------------------------------------------
 abs                             | bigint                       | bigint
 abs                             | decimal(p,s)                 | decimal(p,s)
 abs                             | double                       | double
 abs                             | integer                      | integer
 abs                             | real                         | real
 abs                             | smallint                     | smallint
 abs                             | tinyint                      | tinyint
 acos                            | double                       | double
 approx_distinct                 | bigint                       | T
 approx_distinct                 | bigint                       | T, double
 approx_percentile               | array(bigint)                | bigint, array(double)
 approx_percentile               | array(bigint)                | bigint, bigint, array(double)
 approx_percentile               | array(double)                | double, array(double)
 approx_percentile               | array(double)                | double, bigint, array(double)
 approx_percentile               | array(real)                  | real, array(double)
 approx_percentile               | array(real)                  | real, bigint, array(double)
 approx_percentile               | bigint                       | bigint, bigint, double
 approx_percentile               | bigint                       | bigint, bigint, double, double
 approx_percentile               | bigint                       | bigint, double
 approx_percentile               | double                       | double, bigint, double
 approx_percentile               | double                       | double, bigint, double, double
 approx_percentile               | double                       | double, double
 approx_percentile               | real                         | real, bigint, double

session

presto:test> show session;
                      Name                       |         Value         |        Default        |  Type   |
-------------------------------------------------+-----------------------+-----------------------+---------+---
 aggregation_operator_unspill_memory_limit       | 4MB                   | 4MB                   | varchar | Ex
 colocated_join                                  | false                 | false                 | boolean | Ex
 concurrent_lifespans_per_task                   | 0                     | 0                     | integer | Ex
 default_filter_factor_enabled                   | false                 | false                 | boolean | us
 dictionary_aggregation                          | false                 | false                 | boolean | En
 distributed_index_join                          | false                 | false                 | boolean | Di
 distributed_join                                |                       |                       | boolean | (D
 distributed_sort                                | true                  | true                  | boolean | Pa
 dynamic_schedule_for_grouped_execution          | false                 | false                 | boolean | Ex
 enable_intermediate_aggregations                | false                 | false                 | boolean | En
 enable_stats_calculator                         | true                  | true                  | boolean | Ex
 exchange_compression                            | false                 | false                 | boolean | En
 execution_policy                                | all-at-once           | all-at-once           | varchar | Po
 fast_inequality_joins                           | true                  | true                  | boolean | Us
 filter_and_project_min_output_page_row_count    | 256                   | 256                   | integer | Ex
 filter_and_project_min_output_page_size         | 500kB                 | 500kB                 | varchar | Ex
 grouped_execution_for_aggregation               | false                 | false                 | boolean | Us
 hash_partition_count                            | 100                   | 100                   | integer | Nu
 ignore_stats_calculator_failures                | true                  | true                  | boolean | Ig
 initial_splits_per_node                         | 16                    | 16                    | integer | Th
 iterative_optimizer_enabled                     | true                  | true                  | boolean | Ex
 iterative_optimizer_timeout                     | 3.00m                 | 3.00m                 | varchar | Ti
 join_distribution_type                          | PARTITIONED           | PARTITIONED           | varchar | Th

grants

presto:test> show GRANTS;
 Grantee | Catalog | Schema | Table | Privilege | Grantable
---------+---------+--------+-------+-----------+-----------
(0 rows)

Query 20190829_092709_00053_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:02 [0 rows, 0B] [0 rows/s, 0B/s]

sql基本查询

presto的sql遵循sql99规范,基本sql查询都是和关系型数据库保持一致的。

列表查询

presto:test> select *from t_ods_industry_flow limit 10;
 all_count | all_flow | actual_all_count | actual_all_flow |                 uuid                 | all_count_t
-----------+----------+------------------+-----------------+--------------------------------------+------------
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
 95351     | 65447202 | 779672           | 536157640       | e7d2c211-804f-4d1a-a94f-b05715cb2129 | NULL
(10 rows)

数量计算

presto:test> select count(1) from t_ods_industry_flow;
 _col0
-------
 39680
(1 row)

Query 20190829_093923_00065_rewpf, FINISHED, 1 node
Splits: 37 total, 37 done (100.00%)
0:01 [39.7K rows, 0B] [47K rows/s, 0B/s]

聚合计算

presto:test> select protocol_name,count(1) counter from t_ods_industry_flow group by protocol_name order by counter desc limit 10;
 protocol_name | counter
---------------+---------
 HTTP          |   33600
 ICMP4         |    3960
 IPV4          |     840
 FTP           |     560
 UDP           |     400
 GRE           |     320
(6 rows)

Query 20190829_094129_00068_rewpf, FINISHED, 1 node
Splits: 69 total, 69 done (100.00%)
0:01 [39.7K rows, 1.58KB] [36.6K rows/s, 1.46KB/s]

多数据源混合查询

presto:test> select device.service,count(1) counter from postgres.public.tb_protocol_device device left join hive.test.t_ods_industry_atd atd on (device.service=atd.dst_service) group by device.service order by counter desc limit 10;
        service        | counter
-----------------------+---------
 general-electric-srtp |       1
 bachmann-tcp          |       1
 bacnet                |       1
 CoAP                  |       1
 modbus                |       1
 codesys               |       1
 onvif                 |       1
 XMPP                  |       1
 bachmann-udp          |       1
 cspv4                 |       1
(10 rows)

Query 20190829_095118_00081_rewpf, FINISHED, 1 node
Splits: 108 total, 108 done (100.00%)
0:02 [130K rows, 8.52KB] [66.8K rows/s, 4.37KB/s]

presto 核心设计

查询的提交、presto集群中任务的调度、节点的管理、查询信息的更新和数据的传输,都依赖于presto集群中构建的RESTFULful服务。在这一部分,我们会对presto提供的
RESTful服务以及查询执行过程进行详细说明,并对关键步骤进行源码解析,通过这一部分,我们可以对presto的整个查询执行过程有全面深入的理解。

RESTFUL架构

在presto中几乎所有的操作都是依赖于AirLift构造的RESTful服务来完成的,包括worker节点的管理、查询语句的提交、查询状态的显示、各个task之间数据的传递等。因此
presto中的RESTful服务是presto集群的基础。
presto中提供了四种类型的RESTful接口,分别是statement服务接口、query服务接口、stage服务接口、task服务接口。

statement服务接口

与sql语句相关的请求均由该服务接口处理,包括接收提交的sql语句、获取查询执行结果的语句、取消查询语句等。statement服务接口的实现类为StatementResource。

query服务接口

与查询相关的RESTful请求均由query服务接口处理,包裹sql语句的提交、获取查询执行的结果、取消查询等。query服务接口实现类为QueryResource。

stage服务接口

与stage相关的RESTful请求均由stage服务接口处理,其实该接口只提供了一个功能,就是取消或者结束一个指定的stage。stage服务接口的实现类为StageResource。

task服务接口

与task相关的RESTful请求均由ask服务接口处理,包括task的创建、更新、状态查询和结果查询等。task服务接口的实现类为TaskResource。

presto集群中的数据传输、节点通信、心跳感应、计算监控、计算调度和计算分布全部都是基于RESTful服务实现的,因此presto中的RESTful服务就是presto所有服务的基石。

提交查询

终端用户可以用过jdbc或者cli提交查询语句,也可以通过第三方机构或者个人使用python、c语言开发的驱动提交查询。presto客户端对查询语句的提交主要分为三个步骤。
  • 从指定的文件、命令行参数或者cli窗口中获取需要执行的sql语句。
  • 将得到的sql语句组装成一个RESTful请求,发送给Coordinator,并处理返回的response。
  • cli会不停的循环分批读取查询结果并在屏幕进行动态展示,直到查询结果完全显示完毕。
查询流程图
_images/presto-commit-query.png

生成查询计划

本章主要讲述presto对一个传入的sql语句如何进行解析并生成最终的执行计划。
_images/presto-create-query.png
从上图可以看到,生成查询计划分成语法分析、词法分析、语义分析、执行计划生成、执行计划优化、执行计划分阶段执行。

基本概念

node

查询语句经过词法和语法分析之后,会生成抽象语法树(AST),该语法树中的每一个几点都是一个Node(SQL语句的一部分,如select、where、group by等)。Node是一个抽象类,实现类如下:

  1. approximate 用于近似查询
  2. explainOption 标识explain语句的可选参数,有explainFormat和explainType两类。explainFormat标识输出结果的格式,有text和graphviz两种类型。explainType标识explain语句的类型,有logical和distributed两类,分别标识生成逻辑执行计划与生成分布式执行计划。
  3. expression 标识sql语句中出现的表达式。
  4. framebound 用于窗口函数中华东窗口的可选参数。
  5. relation 是一个抽象类,标识多个节点之间的关系,如join、union等。
  6. select 标识查询语句中的select部分。
  7. selectitem 标识select语句中的列类型,有allcolumns和singlecolumns两种类型。
  8. sortitem 标识排序的某一列及其类型。
  9. statement 标识presto能使用的sql类型的sql语句。
  10. tableelement 标识建表语句描述表的每一列,包括列名与类型。
  11. window 表示一个窗口函数。
  12. windowFrame 表示窗口函数中欢动窗口的可选参数。
  13. with 表示一个查询中所有的with语句,主要元素有recursive、querys。
  14. withquery 表示一个with语句,主要元素有name、query、columnNames。
metadata API
metadata API即是matadata接口,其提供了对源数据进行各种操作的接口,列如列出所有的数据库名、表名等。这些接口在对sql进行语义分宜以及某些ddl操作(如create table)的执行过程中会用到。
metadata api将不同Connector对其元数据的各种啊哦做抽象成一了统一的接口,使得在使用这些接口时无需考虑具体的底层connector实现。
metadata api除了提供对元数据操作的接口,还提供了一些通用的与connector无关的方法,例如列出presto支持的自定义函数等。

词法和语法分析

presto的此法于语法分析是封装在SQLQuerymanager的createQuery方法中。
语法规则

presto使用ANTLR4编写sql语法,语法规则的定义在presto-parse项目的sqlbase.g4文件中,通过ANTLR4查看该文件的语法图。

词法分析

SQLParse的createStatement方法调用其内部方法invokeParser。

语法分析

presto使用visitor模式对sql语句进行语法分析。

获取查询执行引擎

queryexecution表示一次查询执行,用于启动、停止与管理一个查询,以及统计这个查询的相关信息。

获取queryExecutionFactory

根据statement类型获取相对应的QueryExecutionFactory。QueryExecutionFactory是一个借口,其实现类有DataDefinitionExecutionFactory以及SqlQueryExecutionFactory。 executionFactories则是一个Map,存储了不同的Statement类型与QueryExecutionFactory实现类的对应关系,该map的初始化实在CoordinatorModule中进行的,对应关系如表:

Statement实现类 QueryExecutionFactory实现类
createTable DataDefinitionExecutionFactory
RenameTable
RenameColumn
DropTable
CreateView
SetSession
ResetSession
Query SqlQueryExecutionFactory
Explain
showColumns
showPartitions
showFunctions
showTables
showSchemas
showCatalogs
user
showSessions
createTableAsSelect
insert
Delete

create table 、rename table 等ddl操作的sql语句对应了DataDefinitionExecutionFactory,而非ddl操作的sql语句。例如select、insert等对应了SqlQueryExecutionFactory。

创建QueryExecution
当以上的词法与语法分析出错,照着找不到statement实现类与QueryExecutionFactory实现类的对应关系时,将创建一个FailedQueryExecution,冰封装错误信息,最后返回给用户。
调用之前获取的QueryExecutionFactory的createQueryExecution方法,获取对应的QueryExecution。DataDefinitionExecutionFactory创建的是DataDefinitionExecution,而
SqlQueryExecutionFactory创建的是SqlQueryExecution。
在DataDefinitionExecutionFactory创建DataDefinitionExecution时,根据statement类型将对应的DataDefinitionExecutionTask实现类与DataDefinitionExecution绑定。
启动QueryExecution

获取QueryExecution之后,SqlQueryQueueManager方法将QueryExecution与配置的查询队列规则进行匹配,如匹配成功且队列未满,则将QueryExecution加入匹配队列。查询队列按照 FIFO规则调度查询。最后启动QueryExecution。 DataDefinitionExecution启动直接调用其绑定的DataDefinitionTask实现类的execute方法即可。以dropTable为例,由于DropTable与dropTableTask绑定,会华之杰执行DropTableTask 的execute方法。 SqlQueryExecution启动比较复杂,需要执行查询计划、优化查询计划、分阶段执行查询计划。

语义分析

由于DataDefinitionExecution的执行直接调用DataDefinitionTask实现类的execute方法,并未经过执行计划生成的步骤,故以下的内容只针对SqlQueryExecutionFactory。

statement分析

statementAnalyzer是对statement进行予以分析的类,针对不同的statement实现类进行语义分析。

relation分析

TupleAnalyzer类是对Query中的Relation进行分析的类。

表达式分析

ExpressionAnalyzer类对sql语句中的表达式进行分析,主要功能如下:

  1. 获取表达式的类型
  2. 获取需要进行类型转换的表达式及其转换的目的类型。
  3. 获取表达式中存在的函数信息。
  4. 获取表达式中所有合法的列名及对应列的编号。
  5. 获取表达式中In语句中的子查询。

执行计划生成

LogicalPlanner类会根据以上针对SQL语句分析所得的结果,生成逻辑执行计划。

执行计划节点

在讲解执行计划生成之前,首先介绍一下执行计划树中的节点类型。

  1. AggregationNode 是用于聚合操作的节点,聚合的类型有Final、Partial、Single三种,分别表示最终聚合、局部聚合和单店聚合,其中执行计划在进行优化之前,聚合的类型都是单点聚合,在执行计划优化器中会对其进行拆分成局部聚合和最终聚合。
  2. DeleteNode 是用于Delete操作的节点。
  3. DistinctLimitNode 是用于处理以下类型的sql语句的节点。
  4. ExchangeNode 是用于在执行计划中不同stage之间交换数据的节点,出现在逻辑执行计划中。
  5. FilterNode 是用国语进行过滤操作的节点
  6. IndexJoinNode 是用于对Index Join操作的节点。
  7. IndexSorceNode 是与Index join配合使用的执行数据源读取操作的节点。
  8. JoinNode 是执行Join操作的节点
  9. LimitNode 是执行limit操作的及诶单
  10. MarkDistinctNode 是用于处理一下outputNode、projectNode的sql语句的节点。
  11. OutputNode 输出最终结果的节点
  12. project 用于进行列映射的及诶单,用于将ProjectNode下层节点输出的列映射到Project上层节点输入的列。
  13. RemoteSourceNode 类似于ExchangeNode,用于分布式执行计划中不同的stage之间交换数据,出现在分布式执行计划中。
  14. RowNumberNode 用于处理窗口函数row_number
  15. SampleNode 用于处理抽样函数
  16. SemiJoinnode 用于处理执行计划生成过程中产生的SemiJoin。
  17. SortNode 用于排序操作。
  18. TableCommitNode 用于对create table as select语句、insert语句、delete语句的操作执行commit。
  19. TableScanNode 用于读取表的数据。
  20. TableWriterNode 用于向目的的表写入数据。
  21. TopNNode 用于取数据排序后的前N条结果,使用效率更高的TopN算法,而不是对所有数据进行全局派去在取前N条,TopN问题与算法不在具体详述。
  22. TopNRowNumberNode 用于处理窗口函数row_number中排序前N条记录,使用效率更高的TopN算法。
  23. UnionNode 用于处理Union操作
  24. UnnestNode 用于处理Unnest操作
  25. ValuesNode 用于处理Values语句。
  26. WindowNode 用于处理窗口函数。
sql执行计划

LogicalPlanner负责整个sql语句执行计划的生成,根据sql语句的类型生成不同的执行计划,然后针对生成的执行计划,分别使用已注册的执行计划优化器进行优化。

  1. TableWriter Plan

crate table as select 语句和insert语句都会生成tablewriterplan,其所生成的执行计划树如下。

其中queryplan是指create table as select语句或insert语句后面的查询语句生成的执行计划树,在tablewriternode和outputnode之间添加tablecommitnode可以防止数据写入失败导致的中间状态, 但确保数据写入成功之后再进行commit操作。

  1. Deleteplan

Delete语句生成DeletePlan,其执行进化树结果如图。

  1. queryplan

所有热拉提欧尼类型的sql语句都会生成queryplan,由下一节中的relationPlanner分析并生成查询执行计划。

relation执行计划

RelationPlanner用于针对Relation类型的sql语句生成执行计划。

1、table

visitTable对table进行分析主要分为以下两步。如果该table是with所定义的表明,或者该table实际是一个view,则处理其所关联的查询生成执行计划。如果该table是普通的表,则构建TableScanNode。

2、AliasedRelation

visitAliasedRelation处理AliasedRelation所关联的relation,并生成执行计划。

3、SampledRelation

visitSampledRelation处理SampledRelation分为处理其关联的relation,生成执行计划树。构建一个sampleNode,添加到以上的执行计划树之上。

4、join

visitjoin处理join分为处理join左侧的relation,生成左侧执行计划树。如果join右侧是unnest且join类型为cross join或者Implicit join,则根据unnest构造一个UnnestNode以及一个ProjectNode,添加到左侧 执行计划树智商并返回。

query执行计划

queryplanner用于处理query和querySpecification。

执行计划优化

生成执行计划之后,会对所生成的执行计划进行优化,目前presto只支持基于规则的优化器。现有的优化器包括如下几种。

ImplementSampleAsFilter

将bernoulli抽样的samplenode改写为filternode,filternode的过滤条件为 rand() < SampleRatio

CannonicalizeExpressions

将执行计划中设计的表达式进行标准化,标准化的主要工作有。

is not null 改写为 not(is null)
if 语句改写为case when语句
处理时间函数
SimplifyExpressions

对执行计划中设计的表达式进行简化和优化处理,具体可查看ExpressionInterpreter。

UnaliaseSymbolReferences

用于去除执行计划中projectnode中的无异议映射。

UnaliaseSymbolReferences

用于去除执行计划中projectnode中的无意义映射。

执行计划分段

经过执行计划生成与执行计划优化之后,最后对执行计划进行分段。

source

source阶段是从数据源的表中读取数据的阶段,一般包括tableScanNode和projectNode,以及可能存在的filterNode等。

fixed

fixed阶段位于source阶段之后,该阶段将source阶段读取的数据分散到多个节点上进行处理,主要处理的操作有局部聚合、局部join、局部数据写入表等。

single

single阶段位于fixed阶段之后,只在单个节点上执行,用于汇总所有的处理结果,例如针对局部聚合的数据进行最终聚合,并将结果传输给coordinator。

Coordinator_only

Coordinator_only阶段只在coordinator上执行,对insert和create table操作进行commit的tableCommitNode属于Coordinator_only阶段。

查询计划执行

count执行计划
presto:test> explain (type distributed) select count(distinct src_ip) from t_ods_industry_atd;
                                                                                       Query Plan
---------------------------------------------------------------------------------------------------------------
 Fragment 0 [SINGLE]
     Output layout: [count]
     Output partitioning: SINGLE []
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Output[_col0] => [[count]]
             Cost: ?, Output: ? rows (?B)
             _col0 := count
         - Aggregate(FINAL) => [[count]]
                 Cost: ?, Output: ? rows (?B)
                 count := "count"("count_4")
             - LocalExchange[SINGLE] () => [[count_4]]
                     Cost: ?, Output: ? rows (?B)
                 - RemoteSource[1] => [[count_4]]
                         Cost: ?, Output: ? rows (?B)

 Fragment 1 [HASH]
     Output layout: [count_4]
     Output partitioning: SINGLE []
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Aggregate(PARTIAL) => [[count_4]]
             Cost: ?, Output: ? rows (?B)
             count_4 := "count"("src_ip")
         - Aggregate(FINAL)[src_ip][$hashvalue] => [[src_ip, $hashvalue]]
                 Cost: ?, Output: ? rows (?B)
             - LocalExchange[HASH][$hashvalue] ("src_ip") => [[src_ip, $hashvalue]]
                     Cost: ?, Output: ? rows (?B)
                 - RemoteSource[2] => [[src_ip, $hashvalue_5]]
                         Cost: ?, Output: ? rows (?B)

 Fragment 2 [SOURCE]
     Output layout: [src_ip, $hashvalue_6]
     Output partitioning: HASH [src_ip][$hashvalue_6]
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Aggregate(PARTIAL)[src_ip][$hashvalue_6] => [[src_ip, $hashvalue_6]]
             Cost: ?, Output: ? rows (?B)
         - ScanProject[table = hive:HiveTableHandle{schemaName=test, tableName=t_ods_industry_atd, analyzeParti
                 Estimates: {rows: ? (?), cpu: ?, memory: 0.00, network: 0.00}/{rows: ? (?), cpu: ?, memory: 0.
                 Cost: ?, Output: ? rows (?B)
                 $hashvalue_6 := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("src_ip"), 0))
                 LAYOUT: test.t_ods_industry_atd
                 src_ip := src_ip:string:20:REGULAR
                 event_type_id:string:-1:PARTITION_KEY
                     :: [[1], [2], [3], [6], [8]]
                 ds:string:-1:PARTITION_KEY
                     :: [[2019-06-26], [2019-07-17]]


(1 row)

Query 20190830_091640_00070_rewpf, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
0:06 [0 rows, 0B] [0 rows/s, 0B/s]
join执行计划
presto:test> explain (type distributed)  select device.service,count(1) counter from postgres.public.tb_protocol_device device left join hive.test.t_ods_industry_atd atd on (device.service=atd.dst_service) group by device.service order by counter desc limit 10;
                                                                                         Query Plan
---------------------------------------------------------------------------------------------------------------
 Fragment 0 [SINGLE]
     Output layout: [service, count]
     Output partitioning: SINGLE []
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Output[service, counter] => [[service, count]]
             Cost: ?, Output: ? rows (?B)
             counter := count
         - TopN[10 by (count DESC_NULLS_LAST)] => [[service, count]]
                 Cost: ?, Output: ? rows (?B)
             - LocalExchange[SINGLE] () => [[service, count]]
                     Cost: ?, Output: ? rows (?B)
                 - RemoteSource[1] => [[service, count]]
                         Cost: ?, Output: ? rows (?B)

 Fragment 1 [HASH]
     Output layout: [service, count]
     Output partitioning: SINGLE []
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - TopNPartial[10 by (count DESC_NULLS_LAST)] => [[service, count]]
             Cost: ?, Output: ? rows (?B)
         - Project[] => [[service, count]]
                 Cost: ?, Output: ? rows (?B)
             - Aggregate(FINAL)[service][$hashvalue] => [[service, $hashvalue, count]]
                     Cost: ?, Output: ? rows (?B)
                     count := "count"("count_12")
                 - LocalExchange[HASH][$hashvalue] ("service") => [[service, count_12, $hashvalue]]
                         Cost: ?, Output: ? rows (?B)
                     - RemoteSource[2] => [[service, count_12, $hashvalue_13]]
                             Cost: ?, Output: ? rows (?B)

 Fragment 2 [HASH]
     Output layout: [service, count_12, $hashvalue_19]
     Output partitioning: HASH [service][$hashvalue_19]
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Aggregate(PARTIAL)[service][$hashvalue_19] => [[service, $hashvalue_19, count_12]]
             Cost: ?, Output: ? rows (?B)
             count_12 := "count"(*)
         - Project[] => [[service, $hashvalue_19]]
                 Cost: ?, Output: ? rows (?B)
                 $hashvalue_19 := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("service"), 0))
             - LeftJoin[("expr" = "dst_service")][$hashvalue_14, $hashvalue_16] => [[service]]
                     Cost: ?, Output: ? rows (?B)
                     Distribution: PARTITIONED
                 - RemoteSource[3] => [[service, expr, $hashvalue_14]]
                         Cost: ?, Output: ? rows (?B)
                 - LocalExchange[HASH][$hashvalue_16] ("dst_service") => [[dst_service, $hashvalue_16]]
                         Estimates: {rows: ? (?), cpu: ?, memory: 0.00, network: ?}
                         Cost: ?, Output: ? rows (?B)
                     - RemoteSource[4] => [[dst_service, $hashvalue_17]]
                             Cost: ?, Output: ? rows (?B)

 Fragment 3 [SOURCE]
     Output layout: [service, expr, $hashvalue_15]
     Output partitioning: HASH [expr][$hashvalue_15]
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - Project[] => [[service, expr, $hashvalue_15]]
             Estimates: {rows: ? (?), cpu: ?, memory: 0.00, network: 0.00}
             Cost: ?, Output: ? rows (?B)
             $hashvalue_15 := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("expr"), 0))
         - ScanProject[table = postgres:postgres:public.tb_protocol_device:null:public:tb_protocol_device, grou
                 Estimates: {rows: ? (?), cpu: ?, memory: 0.00, network: 0.00}/{rows: ? (?), cpu: ?, memory: 0.
                 Cost: ?, Output: ? rows (?B)
                 expr := CAST("service" AS varchar)
                 service := JdbcColumnHandle{connectorId=postgres, columnName=service, jdbcTypeHandle=JdbcTypeH

 Fragment 4 [SOURCE]
     Output layout: [dst_service, $hashvalue_18]
     Output partitioning: HASH [dst_service][$hashvalue_18]
     Stage Execution Strategy: UNGROUPED_EXECUTION
     - ScanProject[table = hive:HiveTableHandle{schemaName=test, tableName=t_ods_industry_atd, analyzePartition
             Estimates: {rows: ? (?), cpu: ?, memory: 0.00, network: 0.00}/{rows: ? (?), cpu: ?, memory: 0.00,
             Cost: ?, Output: ? rows (?B)
             $hashvalue_18 := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("dst_service"), 0))
             LAYOUT: test.t_ods_industry_atd
             dst_service := dst_service:string:25:REGULAR
             event_type_id:string:-1:PARTITION_KEY
                 :: [[1], [2], [3], [6], [8]]
             ds:string:-1:PARTITION_KEY
                 :: [[2019-06-26], [2019-07-17]]


(1 row)

Query 20190830_092059_00071_rewpf, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
0:07 [0 rows, 0B] [0 rows/s, 0B/s]
_images/presto-count-plain.png

查询调度

presto根据生成的逻辑执行计划将其拆分成多个且有层级关系的stage,presto的各个stage都可以理解为将整个查询的执行计划分成了若干段,实际就是将整个sql处理过程 拆分成具有各自功能的执行阶段。每个执行阶段都会被进一步分解为若干个task,presto将查询请求解析成各个执行阶段后,变回将各个阶段分配到各个计算节点中执行,这 个分配的过程实际是基于stage进行的,每个stage的调度过程是基于split分配worker node的过程,不同的stage有不同的调度策略。

生成调度自行器

stage层级关系的形式是通过查询执行计划演变而来的,对于每个stage,presto都会生成一个与其对应的SqlStageExecution实例,SqlStageExecution承载咳stage的启动和任务 的调度,也可称之为整个查询的调度执行器。sql语句经过优化执行后,获取sql的逻辑执行计划subPlan。

查询调度过程

SqlStageExecution内部主要由一下组件组成:

  • nodeScheduler 将task分配给node的核心模块。
    • nodeManager 获取存活的节点列表,将其保存在NodeMap中,并且定时更新列表中的缓存。
    • nodeMap 用于存储presto节点信息,包含ip端口组成的列表、ip组成的节点列表、rack组成的节点列表。其中rack只是简单的将节点inetAddress封装成rackId,并且一个节点对应一个rack。
    • nodeSchedulerConfig 配置调度的相关参数
    • nodeSelector 提供了各个stage中task分配节点的算法。
  • nodeTaskMap 保存了当前stage分配的task和几点的映射列表,其中NodeTasks维护了一个节点对应的task列表,并且会对每个task注册状态监听器,确保task完成后去task列表中移除。
  • remoteTaskFactory 生成RemoteTask的工厂类。
  • stageStateMachinestage状态监听器

presto的查询调度本质上就是split分配到各个节点的过程,每个阶段一句本身所陈丹的职责,调度方式有所区别,从整体上来说,split分配节点的方式基本为随机选择的策略,在次基础上尽量保证 每个节点处理的split相对平均。

执行查询

向presto集群提交查询,整个执行过程会尽力以下4个阶段。

  • 提交查询 客户端向coordinator提供的restful服务提交sql语句。
  • 生成查询执行计划。coordinator根据传递的sql语句生成对应的查询执行计划。
  • 查询调度。coordinator会调度最空闲的worker执行相应的task进行实际的计算任务。
_images/presto-executequery.png

查询执行逻辑

presto中的所有查询会一次转化为SqlQueryExecution、SqlStageExecution和SqlTaskExecution,因此所有的查询最终都会转化支撑位一个个单独的Task在每个Worker节点上执行。

_images/presto-executionlogic.png

一个SqlQueryExecution可以被分解成多个SqlStageExecution,而一个SqlStageExecution可以被分解成多个SqlTaskExecution,每个SqlTaskExecution运行在一个Worker上,并且在一个 Worker上运行的属于同一个SqlStageExecution的SqlTaskExecution个数不能多于一个(如果设置node-scheduler。multiple-tasks-per-node-enable=true,则在一个worker上可以同时 运行多个Fixed阶段的SqlTaskExecution)。

每个SqlTaskExecution的作用就是处理一个或者多个split,然后将处理的结果输送给下游Stage中的SqlTaskExecution,并且不同Stage中的Task的调度和执行方式也是不同的。

task调度

presto中的Task分别属于几种不同的Stage,分别为Source、Fixed、Single和Coordinator_only。Task的调度逻辑在SqlStageExecution中实现。

source task调度

presto可以读取多种数据源中的数据,然后在内存中进行计算。presto就是通过Source Stage从各种各样的数据源中读取数据的,属于source stage的Task 我们称之为Source Task,Source Task的执行逻辑如下。

  • 根据Split的本地行,生成Node与Split的对应关系。
  • 根据Node与Split的对应关系,在指定的Node上启动Source Task处理位于该Node上的Splits。
fixed task调度

属于Fixed Stage的Task我们称之为Fixed Task。从前面的章节可以得知:在Fixed Stage中会将Join操作两边的数据集分别分解成 initial-hash-partitions个数的数据自己,然后将Hash值相等的 行分布到相同的worker上进行join计算。这样就相当于以initial-hash-partitions倍的并行度进行计算,从而极大的提高了Fixed Stage的计算性能。 运行在每个Worker上的Fixed Task的任务就是贱hash过来的数据进行join操作,然后将结果输出给校友Stage。

single task调度

属于Single Stage的Task我们称之为Single Task。在Single Stage中会将其上游Stage的所有输出汇集到一个Worker 节点上进行汇总计算。运行在Worker上的Single Task的任务就是将其从上游Stage中所有Task产生的数据汇总计算,然后将结果输出给 Coordinator,最后再由Coordinator输出给客户端。Single Task的典型使用场景就是全局排序,在进行全局排序计算的时候,是在Single Stage中完成 的。在进行计算之前会将其上游Stage的输出全部都汇集到Single Node上,然后在Single Node上运行Single Task回见汇聚到的数据进行全局排序。

coordinator_only task调度
当执行DDL或者DML语句的时候,只需要在presto的coordinator对元数据进行添加或者更改。另外,但执行create table as select语句的时候,将会产生两个stage,分别是source stage和coordinator_only
stage,其中source stage负责从表中查询语句并将数据写入表对应的路径下。coordinator_only stage负责根据查询语句创建元数据。

task执行

从前面的章节中我们了解到task的调度过程,无论是哪种类型的task,最终都需要调用方法创建task,或者调用方法更新task。

创建task
更新task
运行task

查询队列

presto队列是用于控制查询并发量和可接受的sql数量,可针对用户、提交来源、session等信息进行个性化配置。队列规则定义在一个json文件中。 可以在config.properties中通过query。queue-config-file来指定队列规则的json文件路径。

配置讲解

query.queue-config-file=etc/queues.json
queues队列定义

配置文件主要包括queues和rules两部分。其中queues详细说明队列规则的配置。 - queuename 队列名称美也就是每个队列的标签名称。 - maxConcurrent 该队列运行同时运行查询的最大并发数量 - maxQueued 该队列允许同时接受提交查询请求的最大数量。

"global_queue":{
   "maxConcurrent":20,
   "maxQueued":1000
}

其中global_queue就是queuename,该队列最多同时运行20个查询,最大的队列长度为1000。

rules规则定义

规则定义在rules标签下,每个规则可定义4个属性,每个属性的具体含义如下:

  • user 用户名,取值来源于session中的user,若在cli中默认为操作系统用户:presto jdbc为传入的用户名。
  • source sql来源,目前一般有两种来源, 目前一般有两种来源,即presto-cli和presto-jdbc,其内容支持正则表达式表示。
  • session session参数定义,范围在system session中所包含的参数。
  • queues 队列列表,可以定义多个。
{
   "user":20,
   "source":1000,
   "session.experimental_big_query":"true",
   "queues":[
      "user.a",
      "global"
   ],
}

一个提交查询请求在匹配队列时,按照以下的逻辑以此判断。

  • 将查询请求中的user信息和规则中的user进行匹配,若匹配成功,则继续与查询请求中的source的内容即系匹配。如果匹配成功,则继续匹配session所定义的规则,如果session定义的规则也满足,则返回该规则下所定义的队列列表。
  • 若user信息未匹配成功,则继续与查询请求中source的内容继续匹配,如果撇皮成功,则继续匹配session所定义的规则。如果session定义的规则也满足,则返回该规则下所定义的队列列表。
  • 如果user和source信息均为匹配成功,则匹配session所定义的规则,如果满足则返回该规则下所定义的队列列表。
  • 最终一个查询请求可能会匹配对个规则,默认取第一个。
  • 最终所匹配到的规则中可能配置了多个队列,如果都满足条件则默认选择第一个队列。

队列加载

presto队列信息的加载是在QueryQueueManager中完成的。而队列的加载是在SqlQueryQueueManager的构造方法中完成的。 如果未配置队列的配置文件,name系统会自动创建两个队列:global和big。big队列对session参数expermental_big_query为true的查询定义了限制规则,该队列限制查询的最大运行并发度为10, 最大排队并发度为500.不过这个session参数回叙版本将不会再启动。gloabal队列为其他的查询定义了限制规则,该队列限制查询的最大运行并发度为1000,最大排队并发度为50000。如果配置了 队列配置文件,则依次读取队列和规则信息,并将规则信息组装进QueryQueueRule列表中。

队列匹配

presto获取到查询请求的session信息后,匹配规则信息。若满足某一队列,则返回当前队列中定义的队列。但将查询添加到队列中的时候需要检查当前QueryQueue集合中的队列已执行和排队的查询数据是否已经达到队列的maxConcurrent和maxQueued总和上限值, 只要QueryQueue集合中有一个队列的一致性和排队的查询数据达到maxConcurrent和maxQueued总和上限值,则本次查询请求提交时报。若都满足该要求,选择QueryQueue集合的第一个队列提交查询,提交前需要判断该队列以接受的查询请求数量是否已经达到当前 运行查询数量和以排队的查询数量上限值之和,若达到,name即使提交也会失败。

presto connector

Presto is a distributed SQL query engine designed to query large data sets distributed over one or more heterogeneous data sources.

system

system connector提供了了集群的运行指标及相关的源元数据信息,这些集群信息、运行状况信息也可以通过sql的方式进行查询。system connect不需要专门配置 catalog,由presto集群在启动的时候进行自动添加。

system connector主要包含information_schema、jdbc、metadata、runtime四个schema。information_schema主要包含本catalog下的信息,jdbc包含整个presto 集群的服务信息,metadata包含整个presto集群的元数据信息,runtime包含presto集群的运行状况信息。

presto:system> show schemas from system;
       Schema
--------------------
 information_schema
 jdbc
 metadata
 runtime
(4 rows)

Query 20190830_040205_00015_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [4 rows, 57B] [19 rows/s, 277B/s]

information_schema

information_schema提供了presto catalog的元数据信息,默认情况下每个presto catalog都会存在一个information_schema。information_schema包含catalog下的 schema信息、function信息、catalog下的所有表信息、catalog下的视图信息、表权限信息、表的列字段信息。

presto:system> show tables from system.information_schema;
      Table
------------------
 columns
 schemata
 table_privileges
 tables
 views
(5 rows)

Query 20190830_053818_00016_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [5 rows, 182B] [13 rows/s, 497B/s]
columns

columns可以查看该catalog下的所有字段信息。show columns就是查看该表下的数据,只是添加了过滤条件。

presto:system> select * from system.information_schema.columns;
 table_catalog |    table_schema    |    table_name     |         column_name          | ordinal_position | col
---------------+--------------------+-------------------+------------------------------+------------------+----
 system        | runtime            | queries           | query_id                     |                1 | NUL
 system        | runtime            | queries           | state                        |                2 | NUL
 system        | runtime            | queries           | user                         |                3 | NUL
 system        | runtime            | queries           | source                       |                4 | NUL
 system        | runtime            | queries           | query                        |                5 | NUL
 system        | runtime            | queries           | resource_group_id            |                6 | NUL
 system        | runtime            | queries           | queued_time_ms               |                7 | NUL
 system        | runtime            | queries           | analysis_time_ms             |                8 | NUL
 system        | runtime            | queries           | distributed_planning_time_ms |                9 | NUL
 system        | runtime            | queries           | created                      |               10 | NUL
 system        | runtime            | queries           | started                      |               11 | NUL
 system        | runtime            | queries           | last_heartbeat               |               12 | NUL
 system        | runtime            | queries           | end                          |               13 | NUL
 system        | runtime            | transactions      | transaction_id               |                1 | NUL
 system        | runtime            | transactions      | isolation_level              |                2 | NUL
 system        | runtime            | transactions      | read_only                    |                3 | NUL
 system        | runtime            | transactions      | auto_commit_context          |                4 | NUL
 system        | runtime            | transactions      | create_time                  |                5 | NUL
 system        | runtime            | transactions      | idle_time_secs               |                6 | NUL
 system        | runtime            | transactions      | written_catalog              |                7 | NUL
 system        | runtime            | transactions      | catalogs                     |                8 | NUL
 system        | jdbc               | types             | type_name                    |                1 | NUL
 system        | jdbc               | types             | data_type                    |                2 | NUL
 system        | jdbc               | types             | precision                    |                3 | NUL
 system        | jdbc               | types             | literal_prefix               |                4 | NUL
 system        | jdbc               | types             | literal_suffix               |                5 | NUL
 system        | jdbc               | types             | create_params                |                6 | NUL
 system        | jdbc               | types             | nullable                     |                7 | NUL
 system        | jdbc               | types             | case_sensitive               |                8 | NUL
 system        | jdbc               | types             | searchable                   |                9 | NUL
 system        | jdbc               | types             | unsigned_attribute           |               10 | NUL
 system        | jdbc               | types             | fixed_prec_scale             |               11 | NUL
 system        | jdbc               | types             | auto_increment               |               12 | NUL
 system        | jdbc               | types             | local_type_name              |               13 | NUL
 system        | jdbc               | types             | minimum_scale                |               14 | NUL
 system        | jdbc               | types             | maximum_scale                |               15 | NUL
 system        | jdbc               | types             | sql_data_type                |               16 | NUL
 system        | jdbc               | types             | sql_datetime_sub             |               17 | NUL
 system        | jdbc               | types             | num_prec_radix               |               18 | NUL
 system        | jdbc               | udts              | type_cat                     |                1 | NUL
 system        | jdbc               | udts              | type_schem                   |                2 | NUL
 system        | jdbc               | udts              | type_name                    |                3 | NUL
 system        | jdbc               | udts              | class_name                   |                4 | NUL
 system        | jdbc               | udts              | data_type                    |                5 | NUL
 system        | jdbc               | udts              | remarks                      |                6 | NUL
 system        | jdbc               | udts              | base_type                    |                7 | NUL
 system        | metadata           | column_properties | catalog_name                 |                1 | NUL
 system        | metadata           | column_properties | property_name                |                2 | NUL
 system        | metadata           | column_properties | default_value                |                3 | NUL
 system        | metadata           | column_properties | type                         |                4 | NUL
 system        | metadata           | column_properties | description                  |                5 | NUL
 system        | jdbc               | super_types       | type_cat                     |                1 | NUL
 system        | jdbc               | super_types       | type_schem                   |                2 | NUL
 system        | jdbc               | super_types       | type_name                    |                3 | NUL
 system        | jdbc               | super_types       | supertype_cat                |                4 | NUL
 system        | jdbc               | super_types       | supertype_schem              |                5 | NUL
 system        | jdbc               | super_types       | supertype_name               |                6 | NUL
 system        | information_schema | views             | table_catalog                |                1 | NUL
 system        | information_schema | views             | table_schema                 |                2 | NUL
 system        | information_schema | views             | table_name                   |                3 | NUL
 system        | information_schema | views             | view_definition              |                4 | NUL
 system        | jdbc               | procedure_columns | procedure_cat                |                1 | NUL
 system        | jdbc               | procedure_columns | procedure_schem              |                2 | NUL
 system        | jdbc               | procedure_columns | procedure_name               |                3 | NUL
 system        | jdbc               | procedure_columns | column_name                  |                4 | NUL
 system        | jdbc               | procedure_columns | column_type                  |                5 | NUL
 system        | jdbc               | procedure_columns | data_type                    |                6 | NUL
 system        | jdbc               | procedure_columns | type_name                    |                7 | NUL
 system        | jdbc               | procedure_columns | precision                    |                8 | NUL
 system        | jdbc               | procedure_columns | length                       |                9 | NUL
 system        | jdbc               | procedure_columns | scale                        |               10 | NUL
 system        | jdbc               | procedure_columns | radix                        |               11 | NUL
 system        | jdbc               | procedure_columns | nullable                     |               12 | NUL
 system        | jdbc               | procedure_columns | remarks                      |               13 | NUL
 system        | jdbc               | procedure_columns | column_def                   |               14 | NUL
 system        | jdbc               | procedure_columns | sql_data_type                |               15 | NUL
 system        | jdbc               | procedure_columns | sql_datetime_sub             |               16 | NUL
 system        | jdbc               | procedure_columns | char_octet_length            |               17 | NUL
 system        | jdbc               | procedure_columns | ordinal_position             |               18 | NUL
 system        | jdbc               | procedure_columns | is_nullable                  |               19 | NUL
 system        | jdbc               | procedure_columns | specific_name                |               20 | NUL
 system        | information_schema | schemata          | catalog_name                 |                1 | NUL
 system        | information_schema | schemata          | schema_name                  |                2 | NUL
 system        | jdbc               | procedures        | procedure_cat                |                1 | NUL
 system        | jdbc               | procedures        | procedure_schem              |                2 | NUL
 system        | jdbc               | procedures        | procedure_name               |                3 | NUL
 system        | jdbc               | procedures        | remarks                      |                4 | NUL
 system        | jdbc               | procedures        | procedure_type               |                5 | NUL
 system        | jdbc               | procedures        | specific_name                |                6 | NUL
 system        | information_schema | columns           | table_catalog                |                1 | NUL
 system        | information_schema | columns           | table_schema                 |                2 | NUL
 system        | information_schema | columns           | table_name                   |                3 | NUL
 system        | information_schema | columns           | column_name                  |                4 | NUL
 system        | information_schema | columns           | ordinal_position             |                5 | NUL
 system        | information_schema | columns           | column_default               |                6 | NUL
 system        | information_schema | columns           | is_nullable                  |                7 | NUL
 system        | information_schema | columns           | data_type                    |                8 | NUL
 system        | information_schema | columns           | comment                      |                9 | NUL
 system        | information_schema | columns           | extra_info                   |               10 | NUL
 system        | information_schema | table_privileges  | grantor                      |                1 | NUL
 system        | information_schema | table_privileges  | grantee                      |                2 | NUL
 system        | information_schema | table_privileges  | table_catalog                |                3 | NUL
 system        | information_schema | table_privileges  | table_schema                 |                4 | NUL
 system        | information_schema | table_privileges  | table_name                   |                5 | NUL
 system        | information_schema | table_privileges  | privilege_type               |                6 | NUL
 system        | information_schema | table_privileges  | is_grantable                 |                7 | NUL
 system        | information_schema | table_privileges  | with_hierarchy               |                8 | NUL
 system        | jdbc               | pseudo_columns    | table_cat                    |                1 | NUL
 system        | jdbc               | pseudo_columns    | table_schem                  |                2 | NUL
 system        | jdbc               | pseudo_columns    | table_name                   |                3 | NUL
 system        | jdbc               | pseudo_columns    | column_name                  |                4 | NUL
 system        | jdbc               | pseudo_columns    | data_type                    |                5 | NUL
 system        | jdbc               | pseudo_columns    | column_size                  |                6 | NUL
 system        | jdbc               | pseudo_columns    | decimal_digits               |                7 | NUL
 system        | jdbc               | pseudo_columns    | num_prec_radix               |                8 | NUL
 system        | jdbc               | pseudo_columns    | column_usage                 |                9 | NUL
 system        | jdbc               | pseudo_columns    | remarks                      |               10 | NUL
 system        | jdbc               | pseudo_columns    | char_octet_length            |               11 | NUL
 system        | jdbc               | pseudo_columns    | is_nullable                  |               12 | NUL
 system        | jdbc               | tables            | table_cat                    |                1 | NUL
 system        | jdbc               | tables            | table_schem                  |                2 | NUL
 system        | jdbc               | tables            | table_name                   |                3 | NUL
 system        | jdbc               | tables            | table_type                   |                4 | NUL
 system        | jdbc               | tables            | remarks                      |                5 | NUL
 system        | jdbc               | tables            | type_cat                     |                6 | NUL
 system        | jdbc               | tables            | type_schem                   |                7 | NUL
 system        | jdbc               | tables            | type_name                    |                8 | NUL
 system        | jdbc               | tables            | self_referencing_col_name    |                9 | NUL
 system        | jdbc               | tables            | ref_generation               |               10 | NUL
 system        | runtime            | tasks             | node_id                      |                1 | NUL
 system        | runtime            | tasks             | task_id                      |                2 | NUL
 system        | runtime            | tasks             | stage_id                     |                3 | NUL
 system        | runtime            | tasks             | query_id                     |                4 | NUL
 system        | runtime            | tasks             | state                        |                5 | NUL
 system        | runtime            | tasks             | splits                       |                6 | NUL
 system        | runtime            | tasks             | queued_splits                |                7 | NUL
 system        | runtime            | tasks             | running_splits               |                8 | NUL
 system        | runtime            | tasks             | completed_splits             |                9 | NUL
 system        | runtime            | tasks             | split_scheduled_time_ms      |               10 | NUL
 system        | runtime            | tasks             | split_cpu_time_ms            |               11 | NUL
 system        | runtime            | tasks             | split_blocked_time_ms        |               12 | NUL
 system        | runtime            | tasks             | raw_input_bytes              |               13 | NUL
 system        | runtime            | tasks             | raw_input_rows               |               14 | NUL
 system        | runtime            | tasks             | processed_input_bytes        |               15 | NUL
 system        | runtime            | tasks             | processed_input_rows         |               16 | NUL
 system        | runtime            | tasks             | output_bytes                 |               17 | NUL
 system        | runtime            | tasks             | output_rows                  |               18 | NUL
 system        | runtime            | tasks             | physical_written_bytes       |               19 | NUL
 system        | runtime            | tasks             | created                      |               20 | NUL
 system        | runtime            | tasks             | start                        |               21 | NUL
 system        | runtime            | tasks             | last_heartbeat               |               22 | NUL
 system        | runtime            | tasks             | end                          |               23 | NUL
 system        | metadata           | catalogs          | catalog_name                 |                1 | NUL
 system        | metadata           | catalogs          | connector_id                 |                2 | NUL
 system        | jdbc               | attributes        | type_cat                     |                1 | NUL
 system        | jdbc               | attributes        | type_schem                   |                2 | NUL
 system        | jdbc               | attributes        | type_name                    |                3 | NUL
 system        | jdbc               | attributes        | attr_name                    |                4 | NUL
 system        | jdbc               | attributes        | data_type                    |                5 | NUL
 system        | jdbc               | attributes        | attr_type_name               |                6 | NUL
 system        | jdbc               | attributes        | attr_size                    |                7 | NUL
 system        | jdbc               | attributes        | decimal_digits               |                8 | NUL
 system        | jdbc               | attributes        | num_prec_radix               |                9 | NUL
 system        | jdbc               | attributes        | nullable                     |               10 | NUL
 system        | jdbc               | attributes        | remarks                      |               11 | NUL
 system        | jdbc               | attributes        | attr_def                     |               12 | NUL
 system        | jdbc               | attributes        | sql_data_type                |               13 | NUL
 system        | jdbc               | attributes        | sql_datetime_sub             |               14 | NUL
 system        | jdbc               | attributes        | char_octet_length            |               15 | NUL
 system        | jdbc               | attributes        | ordinal_position             |               16 | NUL
 system        | jdbc               | attributes        | is_nullable                  |               17 | NUL
 system        | jdbc               | attributes        | scope_catalog                |               18 | NUL
 system        | jdbc               | attributes        | scope_schema                 |               19 | NUL
 system        | jdbc               | attributes        | scope_table                  |               20 | NUL
 system        | jdbc               | attributes        | source_data_type             |               21 | NUL
 system        | jdbc               | super_tables      | table_cat                    |                1 | NUL
 system        | jdbc               | super_tables      | table_schem                  |                2 | NUL
 system        | jdbc               | super_tables      | table_name                   |                3 | NUL
 system        | jdbc               | super_tables      | supertable_name              |                4 | NUL
 system        | runtime            | nodes             | node_id                      |                1 | NUL
 system        | runtime            | nodes             | http_uri                     |                2 | NUL
 system        | runtime            | nodes             | node_version                 |                3 | NUL
 system        | runtime            | nodes             | coordinator                  |                4 | NUL
 system        | runtime            | nodes             | state                        |                5 | NUL
 system        | information_schema | tables            | table_catalog                |                1 | NUL
 system        | information_schema | tables            | table_schema                 |                2 | NUL
 system        | information_schema | tables            | table_name                   |                3 | NUL
 system        | information_schema | tables            | table_type                   |                4 | NUL
 system        | metadata           | table_properties  | catalog_name                 |                1 | NUL
 system        | metadata           | table_properties  | property_name                |                2 | NUL
 system        | metadata           | table_properties  | default_value                |                3 | NUL
 system        | metadata           | table_properties  | type                         |                4 | NUL
 system        | metadata           | table_properties  | description                  |                5 | NUL
 system        | jdbc               | schemas           | table_schem                  |                1 | NUL
 system        | jdbc               | schemas           | table_catalog                |                2 | NUL
 system        | jdbc               | catalogs          | table_cat                    |                1 | NUL
 system        | jdbc               | columns           | table_cat                    |                1 | NUL
 system        | jdbc               | columns           | table_schem                  |                2 | NUL
 system        | jdbc               | columns           | table_name                   |                3 | NUL
 system        | jdbc               | columns           | column_name                  |                4 | NUL
 system        | jdbc               | columns           | data_type                    |                5 | NUL
 system        | jdbc               | columns           | type_name                    |                6 | NUL
 system        | jdbc               | columns           | column_size                  |                7 | NUL
 system        | jdbc               | columns           | buffer_length                |                8 | NUL
 system        | jdbc               | columns           | decimal_digits               |                9 | NUL
 system        | jdbc               | columns           | num_prec_radix               |               10 | NUL
 system        | jdbc               | columns           | nullable                     |               11 | NUL
 system        | jdbc               | columns           | remarks                      |               12 | NUL
 system        | jdbc               | columns           | column_def                   |               13 | NUL
 system        | jdbc               | columns           | sql_data_type                |               14 | NUL
 system        | jdbc               | columns           | sql_datetime_sub             |               15 | NUL
 system        | jdbc               | columns           | char_octet_length            |               16 | NUL
 system        | jdbc               | columns           | ordinal_position             |               17 | NUL
 system        | jdbc               | columns           | is_nullable                  |               18 | NUL
 system        | jdbc               | columns           | scope_catalog                |               19 | NUL
 system        | jdbc               | columns           | scope_schema                 |               20 | NUL
 system        | jdbc               | columns           | scope_table                  |               21 | NUL
 system        | jdbc               | columns           | source_data_type             |               22 | NUL
 system        | jdbc               | columns           | is_autoincrement             |               23 | NUL
 system        | jdbc               | columns           | is_generatedcolumn           |               24 | NUL
 system        | jdbc               | table_types       | table_type                   |                1 | NUL
 system        | metadata           | schema_properties | catalog_name                 |                1 | NUL
 system        | metadata           | schema_properties | property_name                |                2 | NUL
 system        | metadata           | schema_properties | default_value                |                3 | NUL
 system        | metadata           | schema_properties | type                         |                4 | NUL
 system        | metadata           | schema_properties | description                  |                5 | NUL
(226 rows)
schemata

查看该catalog下的所有schemas,show schemas就是查看该表下的数据。

presto:system> select * from system.information_schema.schemata;
 catalog_name |    schema_name
--------------+--------------------
 system       | information_schema
 system       | jdbc
 system       | metadata
 system       | runtime
(4 rows)

Query 20190830_054251_00018_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [4 rows, 101B] [25 rows/s, 638B/s]
table_privileges

查看表权限信息,show grants 就是查看该表的信息。

presto:system> select * from system.information_schema.table_privileges;
 grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
---------+---------+---------------+--------------+------------+----------------+--------------+---------------
(0 rows)

Query 20190830_054526_00019_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:04 [0 rows, 0B] [0 rows/s, 0B/s]
tables

查看该catalog下的所有表名。

presto:system> select * from system.information_schema.tables;
 table_catalog |    table_schema    |    table_name     | table_type
---------------+--------------------+-------------------+------------
 system        | runtime            | queries           | BASE TABLE
 system        | runtime            | transactions      | BASE TABLE
 system        | jdbc               | types             | BASE TABLE
 system        | jdbc               | udts              | BASE TABLE
 system        | metadata           | column_properties | BASE TABLE
 system        | jdbc               | super_types       | BASE TABLE
 system        | information_schema | views             | BASE TABLE
 system        | jdbc               | procedure_columns | BASE TABLE
 system        | information_schema | schemata          | BASE TABLE
 system        | jdbc               | procedures        | BASE TABLE
 system        | information_schema | columns           | BASE TABLE
 system        | information_schema | table_privileges  | BASE TABLE
 system        | jdbc               | pseudo_columns    | BASE TABLE
 system        | jdbc               | tables            | BASE TABLE
 system        | runtime            | tasks             | BASE TABLE
 system        | metadata           | catalogs          | BASE TABLE
 system        | jdbc               | attributes        | BASE TABLE
 system        | jdbc               | super_tables      | BASE TABLE
 system        | runtime            | nodes             | BASE TABLE
 system        | information_schema | tables            | BASE TABLE
 system        | metadata           | table_properties  | BASE TABLE
 system        | jdbc               | schemas           | BASE TABLE
 system        | jdbc               | catalogs          | BASE TABLE
 system        | jdbc               | columns           | BASE TABLE
 system        | jdbc               | table_types       | BASE TABLE
 system        | metadata           | schema_properties | BASE TABLE
(26 rows)

Query 20190830_054741_00020_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:03 [26 rows, 1.36KB] [8 rows/s, 437B/s]
views

查看该catalog下的所有视图信息,视图功能是要看connector是否支持视图功能。

presto:system> select * from system.information_schema.views;
 table_catalog | table_schema | table_name | view_definition
---------------+--------------+------------+-----------------
(0 rows)

Query 20190830_054831_00021_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]

jdbc

jdbc查看presto集群的服务信息,如属性信息、catalogs信息、所有catalogs下的表字段信息、所有catalogs下的表信息、presto的所有类型信息、presto所有自定义函数。
总之jdbc是对整个presto集群进行信息展示的。而information_schema是对当前catalog进行信息展示。
presto:system> show tables from system.jdbc;
       Table
-------------------
 attributes
 catalogs
 columns
 procedure_columns
 procedures
 pseudo_columns
 schemas
 super_tables
 super_types
 table_types
 tables
 types
 udts
(13 rows)

Query 20190830_054959_00022_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [13 rows, 304B] [36 rows/s, 846B/s]
attributes
presto:system> select * from system.jdbc.attributes;
 type_cat | type_schem | type_name | attr_name | data_type | attr_type_name | attr_size | decimal_digits | num_
----------+------------+-----------+-----------+-----------+----------------+-----------+----------------+-----
(0 rows)

Query 20190830_055703_00030_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:02 [0 rows, 0B] [0 rows/s, 0B/s]
catalogs
presto:system> select * from system.jdbc.catalogs;
   table_cat
---------------
 elasticsearch
 hive
 kafka
 postgres
 system
(5 rows)

Query 20190830_055734_00031_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [5 rows, 36B] [36 rows/s, 265B/s]
tables
presto:system> select * from system.jdbc.columns;
   table_cat   |    table_schem     |          table_name          |           column_name           | data_typ
---------------+--------------------+------------------------------+---------------------------------+---------
 elasticsearch | information_schema | table_privileges             | grantor                         |        1
 elasticsearch | information_schema | table_privileges             | grantee                         |        1
 elasticsearch | information_schema | table_privileges             | table_catalog                   |        1
 elasticsearch | information_schema | table_privileges             | table_schema                    |        1
 elasticsearch | information_schema | table_privileges             | table_name                      |        1
 elasticsearch | information_schema | table_privileges             | privilege_type                  |        1
 elasticsearch | information_schema | table_privileges             | is_grantable                    |        1
 elasticsearch | information_schema | table_privileges             | with_hierarchy                  |        1
 elasticsearch | information_schema | tables                       | table_catalog                   |        1
 elasticsearch | information_schema | tables                       | table_schema                    |        1
 elasticsearch | information_schema | tables                       | table_name                      |        1
 elasticsearch | information_schema | tables                       | table_type                      |        1
 elasticsearch | information_schema | views                        | table_catalog                   |        1
 elasticsearch | information_schema | views                        | table_schema                    |        1
 elasticsearch | information_schema | views                        | table_name                      |        1
 elasticsearch | information_schema | views                        | view_definition                 |        1
 elasticsearch | information_schema | columns                      | table_catalog                   |        1
 elasticsearch | information_schema | columns                      | table_schema                    |        1
 elasticsearch | information_schema | columns                      | table_name                      |        1
 elasticsearch | information_schema | columns                      | column_name                     |        1
 elasticsearch | information_schema | columns                      | ordinal_position                |        -
 elasticsearch | information_schema | columns                      | column_default                  |        1
 elasticsearch | information_schema | columns                      | is_nullable                     |        1
 elasticsearch | information_schema | columns                      | data_type                       |        1
 elasticsearch | information_schema | columns                      | comment                         |        1
(query aborted by user)

Query 20190830_055802_00032_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:02 [1.06K rows, 99.5KB] [472 rows/s, 44.4KB/s]
procedure_columns
presto:system> select * from system.jdbc.procedure_columns;
 procedure_cat | procedure_schem | procedure_name | column_name | column_type | data_type | type_name | precisi
---------------+-----------------+----------------+-------------+-------------+-----------+-----------+--------
(0 rows)

Query 20190830_055858_00033_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:02 [0 rows, 0B] [0 rows/s, 0B/s]
procedures
presto:system> select * from system.jdbc.procedures;
 procedure_cat | procedure_schem | procedure_name | remarks | procedure_type | specific_name
---------------+-----------------+----------------+---------+----------------+---------------
(0 rows)

Query 20190830_055942_00034_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]
pseudo_columns
presto:system> select * from system.jdbc.pseudo_columns;
 table_cat | table_schem | table_name | column_name | data_type | column_size | decimal_digits | num_prec_radix
-----------+-------------+------------+-------------+-----------+-------------+----------------+---------------
(0 rows)

Query 20190830_060051_00035_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:01 [0 rows, 0B] [0 rows/s, 0B/s]
schemas
presto:system> select * from system.jdbc.schemas;
    table_schem     | table_catalog
--------------------+---------------
 default            | elasticsearch
 information_schema | elasticsearch
 default            | hive
 information_schema | hive
 test               | hive
 industry           | kafka
 information_schema | kafka
 information_schema | postgres
 pg_catalog         | postgres
 public             | postgres
 information_schema | system
 jdbc               | system
 metadata           | system
 runtime            | system
(14 rows)

Query 20190830_060113_00036_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [14 rows, 247B] [61 rows/s, 1.05KB/s]
super_tables
presto:system> select * from system.jdbc.super_tables;
 table_cat | table_schem | table_name | supertable_name
-----------+-------------+------------+-----------------
(0 rows)

Query 20190830_060136_00037_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]
super_types
presto:system> select * from system.jdbc.super_types;
 type_cat | type_schem | type_name | supertype_cat | supertype_schem | supertype_name
----------+------------+-----------+---------------+-----------------+----------------
(0 rows)

Query 20190830_060157_00038_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]
table_types
presto:system> select * from system.jdbc.table_types;
 table_type
------------
 TABLE
 VIEW
(2 rows)

Query 20190830_060213_00039_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [2 rows, 9B] [13 rows/s, 62B/s]
tables
presto:system> select * from system.jdbc.tables;
   table_cat   |    table_schem     |          table_name          | table_type | remarks | type_cat | type_sch
---------------+--------------------+------------------------------+------------+---------+----------+---------
 elasticsearch | information_schema | columns                      | TABLE      | NULL    | NULL     | NULL
 elasticsearch | information_schema | tables                       | TABLE      | NULL    | NULL     | NULL
 elasticsearch | information_schema | views                        | TABLE      | NULL    | NULL     | NULL
 elasticsearch | information_schema | schemata                     | TABLE      | NULL    | NULL     | NULL
 elasticsearch | information_schema | table_privileges             | TABLE      | NULL    | NULL     | NULL
 elasticsearch | default            | guangdong_jmr_sample         | TABLE      | NULL    | NULL     | NULL
 elasticsearch | default            | jmr_ipunit                   | TABLE      | NULL    | NULL     | NULL
 hive          | information_schema | columns                      | TABLE      | NULL    | NULL     | NULL
 hive          | information_schema | tables                       | TABLE      | NULL    | NULL     | NULL
 hive          | information_schema | views                        | TABLE      | NULL    | NULL     | NULL
 hive          | information_schema | schemata                     | TABLE      | NULL    | NULL     | NULL
 hive          | information_schema | table_privileges             | TABLE      | NULL    | NULL     | NULL
 hive          | default            | t_ods_industry_flow          | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_flow          | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_atd           | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_flow_test     | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_atd_sp        | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_gynetres_test | TABLE      | NULL    | NULL     | NULL
 hive          | test               | t_ods_industry_gynetres      | TABLE      | NULL    | NULL     | NULL
 kafka         | information_schema | columns                      | TABLE      | NULL    | NULL     | NULL
 kafka         | information_schema | tables                       | TABLE      | NULL    | NULL     | NULL
 kafka         | information_schema | views                        | TABLE      | NULL    | NULL     | NULL
 kafka         | information_schema | schemata                     | TABLE      | NULL    | NULL     | NULL
 kafka         | information_schema | table_privileges             | TABLE      | NULL    | NULL     | NULL
 kafka         | industry           | nsfocus                      | TABLE      | NULL    | NULL     | NULL

Query 20190830_060232_00040_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:02 [62 rows, 2.04KB] [33 rows/s, 1.1KB/s]
types
presto:system> select * from system.jdbc.types;
        type_name         | data_type | precision  | literal_prefix | literal_suffix | create_params | nullable
--------------------------+-----------+------------+----------------+----------------+---------------+---------
 time                     |        92 |          8 | NULL           | NULL           | NULL          |        1
 color                    |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 HyperLogLog              |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 varbinary                |        -3 | 2147483647 | NULL           | NULL           | NULL          |        1
 timestamp with time zone |      2014 |         29 | NULL           | NULL           | NULL          |        1
 KdbTree                  |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 BingTile                 |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 boolean                  |        16 | NULL       | NULL           | NULL           | NULL          |        1
 CodePoints               |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 time with time zone      |      2013 |         14 | NULL           | NULL           | NULL          |        1
 JsonPath                 |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 Regressor                |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 SphericalGeography       |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 real                     |         7 |         24 | NULL           | NULL           | NULL          |        1
 JoniRegExp               |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 json                     |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 unknown                  |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 ipaddress                |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 ObjectId                 |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 bigint                   |        -5 |         19 | NULL           | NULL           | NULL          |        1
 interval year to month   |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 SetDigest                |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 P4HyperLogLog            |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 Re2JRegExp               |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 double                   |         8 |         53 | NULL           | NULL           | NULL          |        1
 Geometry                 |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 interval day to second   |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 Model                    |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 smallint                 |         5 |          5 | NULL           | NULL           | NULL          |        1
 tinyint                  |        -6 |          3 | NULL           | NULL           | NULL          |        1
 timestamp                |        93 |         23 | NULL           | NULL           | NULL          |        1
 date                     |        91 |         14 | NULL           | NULL           | NULL          |        1
 LikePattern              |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 integer                  |         4 |         10 | NULL           | NULL           | NULL          |        1
 array                    |      2003 | NULL       | NULL           | NULL           | NULL          |        1
 varchar                  |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 function                 |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 char                     |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 Classifier               |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 qdigest                  |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 row                      |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 decimal                  |      2000 | NULL       | NULL           | NULL           | NULL          |        1
 map                      |      2000 | NULL       | NULL           | NULL           | NULL          |        1
(43 rows)

Query 20190830_060257_00041_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:04 [43 rows, 2.28KB] [11 rows/s, 606B/s]
udts
presto:system> select * from system.jdbc.udts;
 type_cat | type_schem | type_name | class_name | data_type | remarks | base_type
----------+------------+-----------+------------+-----------+---------+-----------
(0 rows)

Query 20190830_060326_00042_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]

metadata

查询集群可用的catalog、schema、table和columns的元数据信息。

presto:system> show tables from system.metadata;
       Table
-------------------
 catalogs
 column_properties
 schema_properties
 table_properties
(4 rows)

Query 20190830_060659_00044_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [4 rows, 130B] [8 rows/s, 290B/s]
catalogs

查看presto集群所有的catalog。

presto:system> select * from system.metadata.catalogs;
 catalog_name  | connector_id
---------------+---------------
 elasticsearch | elasticsearch
 hive          | hive
 kafka         | kafka
 postgres      | postgres
 system        | system
(5 rows)

Query 20190830_060812_00045_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [5 rows, 72B] [22 rows/s, 330B/s]
column_properties

查看字段元数据信息

presto:system> select * from system.metadata.column_properties;
 catalog_name | property_name | default_value | type | description
--------------+---------------+---------------+------+-------------
(0 rows)

Query 20190830_060904_00046_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]
schema_properties

查看schema元数据信息

presto:system> select * from system.metadata.schema_properties;
 catalog_name | property_name | default_value |  type   |          description
--------------+---------------+---------------+---------+-------------------------------
 hive         | location      |               | varchar | Base file system location URI
(1 row)

Query 20190830_060921_00047_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [1 rows, 48B] [7 rows/s, 357B/s]
table_properties

查看表元数据信息

presto:system> select * from system.metadata.table_properties;
 catalog_name |      property_name       | default_value |      type      |                 description
--------------+--------------------------+---------------+----------------+------------------------------------
 hive         | avro_schema_url          |               | varchar        | URI pointing to Avro schema for the
 hive         | bucket_count             | 0             | integer        | Number of buckets
 hive         | bucketed_by              | []            | array(varchar) | Bucketing columns
 hive         | external_location        |               | varchar        | File system location URI for extern
 hive         | format                   | ORC           | varchar        | Hive storage format for the table
 hive         | orc_bloom_filter_columns | []            | array(varchar) | ORC Bloom filter index columns
 hive         | orc_bloom_filter_fpp     | 0.05          | double         | ORC Bloom filter false positive pro
 hive         | partitioned_by           | []            | array(varchar) | Partition columns
 hive         | sorted_by                | []            | array(varchar) | Bucket sorting columns
(9 rows)

Query 20190830_060950_00048_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:03 [9 rows, 533B] [3 rows/s, 203B/s]

runtime

runtime schema展示presto集群的运行状况信息,如presto节点信息、查询语句信息、presto任务信息、presto事务信息等。

presto:system> show tables from system.runtime;
    Table
--------------
 nodes
 queries
 tasks
 transactions
(4 rows)

Query 20190830_061154_00049_rewpf, FINISHED, 1 node
Splits: 19 total, 19 done (100.00%)
0:00 [4 rows, 97B] [9 rows/s, 225B/s]
nodes

查看集群节点信息。可以通过sql方便的查看到presto集群的版本号,哪个node是主节点,节点运行状况,节点的服务连接。

presto:system> select *from system.runtime.nodes;
               node_id                |          http_uri          | node_version | coordinator | state
--------------------------------------+----------------------------+--------------+-------------+--------
 ffffffff-ffff-ffff-ffff-ffffffffff12 | http://172.31.134.225:9001 | 0.217        | true        | active
(1 row)

Query 20190830_061354_00051_rewpf, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [1 rows, 74B] [6 rows/s, 483B/s]

presto:default> select *from system.runtime.nodes;
            node_id                |         http_uri         | node_version | coordinator | state
---------------------------------------+--------------------------+--------------+-------------+--------
 ffffffff-ffff-ffff-ffff-fffffffffff03 | http://192.168.10.3:9001 | 0.211        | false       | active
 ffffffff-ffff-ffff-ffff-fffffffffff05 | http://192.168.10.5:9001 | 0.211        | false       | active
 ffffffff-ffff-ffff-ffff-fffffffffff2  | http://192.168.10.1:9001 | 0.211        | false       | active
 ffffffff-ffff-ffff-ffff-fffffffffff1  | http://192.168.10.8:9001 | 0.211        | true        | active
 ffffffff-ffff-ffff-ffff-fffffffffff02 | http://192.168.10.2:9001 | 0.211        | false       | active
 ffffffff-ffff-ffff-ffff-fffffffffff04 | http://192.168.10.4:9001 | 0.211        | false       | active
(6 rows)

Query 20190830_061521_26124_fqui7, FINISHED, 2 nodes
Splits: 17 total, 17 done (100.00%)
0:00 [6 rows, 436B] [60 rows/s, 4.32KB/s]
queries

可以查询presto集群正在运行(RUNING)、运行完成(FINISHED)和运行失败(FAILED)的查询。queries记录查询语句,默认最多记录100条查询语句。

presto:default> select *from system.runtime.queries;
               node_id                |          query_id           |  state   | user |   source    |
--------------------------------------+-----------------------------+----------+------+-------------+-------------------------------------------------------------------------------------------------
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060142_24670_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement219 FROM SELECT
                                      |                             |          |      |             | attacked_industry as name,count(1) as value
                                      |                             |          |      |             |   FROM industry.t_ods_industry_atd
                                      |                             |          |      |             |  WHERE  event_time BETWEEN ? AND ? AND attacked_industry IS NOT NULL AND attacked_industry IN (?
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060406_25192_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement69 FROM select count(1) from (
                                      |                             |          |      |             |           select id from industry.t_ods_industry_atd
                                      |                             |          |      |             |            WHERE  event_time  >=  ?
                                      |                             |          |      |             |                    AND event_time  <=  ?
                                      |                             |          |      |             |                    AND cloud_platform_name = ?
                                      |                             |          |      |             |           )
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060147_24683_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement18 USING '2019-08-24', '2019-08-30', '食品制造业', '台湾', '香港', '澳门'
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060449_25349_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement74 USING '2019-08-30', '2019-08-29', TIMESTAMP '2019-08-29 14:04:49.326', TIMES
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060705_25700_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement86 USING '2019-07-30', '2019-08-30', '汽车制造业', '台湾', '香港', '澳门'
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060212_24756_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement288 FROM SELECT
                                      |                             |          |      |             | count(distinct attacked_corpname) as value
                                      |                             |          |      |             |   FROM industry.t_ods_industry_atd
                                      |                             |          |      |             |  WHERE  ds IN (?,?) AND event_time BETWEEN ? AND ? AND attacked_industry = ? AND attacked_provin
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060548_25551_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement103
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_061251_25997_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement383 USING '2019-08-30', '2019-08-29', TIMESTAMP '2019-08-29 14:12:49.116', TIME
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060331_25057_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement52 FROM SELECT
                                      |                             |          |      |             | count(distinct attacked_corpname) as value
                                      |                             |          |      |             |   FROM industry.t_ods_industry_atd
                                      |                             |          |      |             |  WHERE  ds BETWEEN ? AND ? AND attacked_industry = ? AND attacked_province NOT IN (?,?,?)
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060422_25251_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement79
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060446_25336_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement65 FROM SELECT
                                      |                             |          |      |             | count(distinct attacked_corpname) as value
                                      |                             |          |      |             |   FROM industry.t_ods_industry_atd
                                      |                             |          |      |             |  WHERE  ds BETWEEN ? AND ? AND attacked_industry = ? AND attacked_province NOT IN (?,?,?)
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060217_24769_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement23 USING TIMESTAMP '2019-07-30 00:00:00.000', TIMESTAMP '2019-08-30 23:59:59.99
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060427_25264_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement61
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060443_25323_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement92
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_061212_25947_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement366
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_061229_25968_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement373
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060226_24807_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement289
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060151_24696_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement17
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060555_25572_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement327
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060307_24976_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement54 FROM SELECT
                                      |                             |          |      |             | count(distinct attacked_corpname) as value
                                      |                             |          |      |             |   FROM industry.t_ods_industry_atd
                                      |                             |          |      |             |  WHERE  ds IN (?,?) AND event_time BETWEEN ? AND ? AND attacked_industry = ? AND attacked_provin
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060439_25310_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement90 USING TIMESTAMP '2019-07-30 00:00:00.000', TIMESTAMP '2019-08-30 23:59:59.99
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060343_25103_fqui7 | FINISHED | hive | presto-jdbc | EXECUTE statement304 USING '2019-08-30', '2019-08-29', TIMESTAMP '2019-08-29 14:03:39.358', TIME
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_061427_26109_fqui7 | FINISHED | hive | presto-jdbc | DEALLOCATE PREPARE statement420
 ffffffff-ffff-ffff-ffff-fffffffffff1 | 20190830_060451_25357_fqui7 | FINISHED | hive | presto-jdbc | PREPARE statement66 FROM SELECT
                                      |                             |          |      |             | count(distinct attacked_corpname) as value
tasks

tasks信息记录了task的明细信息,包括split分片信息、task运行的节点信息、运行时间、task处理的节点信息

presto:default> select *from system.runtime.tasks;
                node_id                |             task_id             |           stage_id            |          query_id           |  state   | splits | queued_splits | running_splits | complete
---------------------------------------+---------------------------------+-------------------------------+-----------------------------+----------+--------+---------------+----------------+---------
 ffffffff-ffff-ffff-ffff-fffffffffff1  | 20190830_061940_26127_fqui7.1.5 | 20190830_061940_26127_fqui7.1 | 20190830_061940_26127_fqui7 | RUNNING  |      1 |             0 |              1 |
 ffffffff-ffff-ffff-ffff-fffffffffff1  | 20190830_061845_26126_fqui7.1.0 | 20190830_061845_26126_fqui7.1 | 20190830_061845_26126_fqui7 | FINISHED |      1 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff1  | 20190830_061612_26125_fqui7.1.0 | 20190830_061612_26125_fqui7.1 | 20190830_061612_26125_fqui7 | FINISHED |      1 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff1  | 20190830_061512_26123_fqui7.1.0 | 20190830_061512_26123_fqui7.1 | 20190830_061512_26123_fqui7 | FINISHED |      1 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff1  | 20190830_061521_26124_fqui7.1.0 | 20190830_061521_26124_fqui7.1 | 20190830_061521_26124_fqui7 | FINISHED |      1 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060448_25347_fqui7.2.4 | 20190830_060448_25347_fqui7.2 | 20190830_060448_25347_fqui7 | FINISHED |    430 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061352_26069_fqui7.2.4 | 20190830_061352_26069_fqui7.2 | 20190830_061352_26069_fqui7 | FINISHED |    420 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060606_25610_fqui7.0.0 | 20190830_060606_25610_fqui7.0 | 20190830_060606_25610_fqui7 | FINISHED |     17 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061246_25991_fqui7.2.4 | 20190830_061246_25991_fqui7.2 | 20190830_061246_25991_fqui7 | FINISHED |    600 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060523_25469_fqui7.2.4 | 20190830_060523_25469_fqui7.2 | 20190830_060523_25469_fqui7 | FINISHED |   1119 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060546_25548_fqui7.1.4 | 20190830_060546_25548_fqui7.1 | 20190830_060546_25548_fqui7 | FINISHED |   1912 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061444_26120_fqui7.3.0 | 20190830_061444_26120_fqui7.3 | 20190830_061444_26120_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061122_25893_fqui7.1.1 | 20190830_061122_25893_fqui7.1 | 20190830_061122_25893_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060656_25690_fqui7.1.2 | 20190830_060656_25690_fqui7.1 | 20190830_060656_25690_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061304_26012_fqui7.1.2 | 20190830_061304_26012_fqui7.1 | 20190830_061304_26012_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061052_25860_fqui7.3.3 | 20190830_061052_25860_fqui7.3 | 20190830_061052_25860_fqui7 | FINISHED |    601 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060546_25550_fqui7.2.4 | 20190830_060546_25550_fqui7.2 | 20190830_060546_25550_fqui7 | FINISHED |    540 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060530_25499_fqui7.1.3 | 20190830_060530_25499_fqui7.1 | 20190830_060530_25499_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060839_25773_fqui7.0.0 | 20190830_060839_25773_fqui7.0 | 20190830_060839_25773_fqui7 | FINISHED |     17 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061416_26099_fqui7.1.0 | 20190830_061416_26099_fqui7.1 | 20190830_061416_26099_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060728_25724_fqui7.1.1 | 20190830_060728_25724_fqui7.1 | 20190830_060728_25724_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061235_25976_fqui7.2.4 | 20190830_061235_25976_fqui7.2 | 20190830_061235_25976_fqui7 | FINISHED |    640 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061249_25994_fqui7.2.4 | 20190830_061249_25994_fqui7.2 | 20190830_061249_25994_fqui7 | FINISHED |    446 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061011_25806_fqui7.1.4 | 20190830_061011_25806_fqui7.1 | 20190830_061011_25806_fqui7 | FINISHED |   2197 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_060512_25430_fqui7.1.3 | 20190830_060512_25430_fqui7.1 | 20190830_060512_25430_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061000_25797_fqui7.1.4 | 20190830_061000_25797_fqui7.1 | 20190830_061000_25797_fqui7 | FINISHED |     32 |             0 |              0 |
 ffffffff-ffff-ffff-ffff-fffffffffff2  | 20190830_061314_26024_fqui7.0.0 | 20190830_061314_26024_fqui7.0 | 20190830_061314_26024_fqui7 | FINISHED |     17 |             0 |              0 |
transactions

记录了presto集群执行事务的操作语句。

presto:default> select *from system.runtime.transactions;
            transaction_id            | isolation_level  | read_only | auto_commit_context |       create_time       | idle_time_secs | written_catalog |                   catalogs
--------------------------------------+------------------+-----------+---------------------+-------------------------+----------------+-----------------+---------------------------------------------
 576da0b3-574d-4192-9a73-2b1ea89cc731 | READ UNCOMMITTED | false     | true                | 2019-08-29 15:52:09.176 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 a21c82d8-f1ce-4c74-9824-3dd1c99fec1f | READ UNCOMMITTED | false     | true                | 2019-08-30 14:20:50.532 |              0 | NULL            | [system, $info_schema@system, $system@system
 9f20bf0e-c926-4f0b-a041-3c2f25563ff6 | READ UNCOMMITTED | false     | true                | 2019-08-29 14:58:25.993 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 a24c83b1-d213-40c9-93f9-f7c646a3afb1 | READ UNCOMMITTED | false     | true                | 2019-08-30 14:20:42.027 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 3e41255e-aea4-473d-85d7-1288e27c67ad | READ UNCOMMITTED | false     | true                | 2019-08-30 14:20:45.545 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 23127584-a460-4e35-819e-5622a83cfe0f | READ UNCOMMITTED | false     | true                | 2019-08-30 14:20:49.987 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 8eab0d82-1211-4888-a24a-c84c6ea17502 | READ UNCOMMITTED | false     | true                | 2019-08-29 15:13:47.390 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 1905dafd-718c-45d4-9ffb-1ad5bfe75814 | READ UNCOMMITTED | false     | true                | 2019-08-30 12:04:01.266 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 0892e64f-9ca3-4c4f-acf7-a2bdd189c8c7 | READ UNCOMMITTED | false     | true                | 2019-08-29 15:12:51.571 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
 5043f6fa-55bb-4d60-9f53-4ebd4f89681a | READ UNCOMMITTED | false     | true                | 2019-08-29 15:02:31.807 |              0 | NULL            | [$info_schema@hive, $system@hive, hive]
(10 rows)

Query 20190830_062050_26191_fqui7, FINISHED, 2 nodes
Splits: 17 total, 17 done (100.00%)
0:02 [10 rows, 1.16KB] [5 rows/s, 622B/s]

postgresql

postgresql.properties

connector.name=postgresql
connection-url=jdbc:postgresql://172.31.134.225:5432/ads
connection-user=ads
connection-password=ads@123

connector配置

hive

hive.properties

connector.name=hive-hadoop2
hive.metastore.uri=thrift://172.31.134.216:9083
#hive.config.resources=/etc/hadoop/2.4.2.0-258/0/core-site.xml,/etc/hadoop/2.4.2.0-258/0/hdfs-site.xml
hive.allow-drop-table=true
hive.allow-add-column=true
hive.allow-drop-column=true

connector配置

presto sql

presto 函数

presto 数据类型

presto 有很多内置的数据类型,也可以通过插件的形式添加自定义数据类型。每一个connector都是不一样的,有的connector没有提供所有的数据类型,需要查看每个connector提供的数据类型文档信息。

presto数据类型

Boolean

这种数据类型包含两种值 true或者false

Integer

TINYINT

八位一字节,数值范围为: -2^7 ~ 2^7 - 1.

SMALLINT

2字节16位,数值范围为: -2^15 ~ 2^15 - 1

INTEGER

4字节32位,数值范围为: -2^31 ~ 2^31 - 1

BIGINT

8字节64位,数值范围为: -2^63 ~ 2^63 - 1

Floating-Point

REAL

A real is a 32-bit inexact, variable-precision implementing the IEEE Standard 754 for Binary Floating-Point Arithmetic.

DOUBLE

A double is a 64-bit inexact, variable-precision implementing the IEEE Standard 754 for Binary Floating-Point Arithmetic.

Fixed-Precision

DECIMAL

A fixed precision decimal number. Precision up to 38 digits is supported but performance is best up to 18 digits.

The decimal type takes two literal parameters:

  • precision - total number of digits
  • scale - number of digits in fractional part. Scale is optional and defaults to 0.

Example type definitions: DECIMAL(10,3), DECIMAL(20)

Example literals: DECIMAL '10.3', DECIMAL '1234567890', 1.1

String

VARCHAR

Variable length character data with an optional maximum length.

Example type definitions: varchar, varchar(20)

CHAR

Fixed length character data. A CHAR type without length specified has a default length of 1. A CHAR(x) value always has x characters. For instance, casting dog to CHAR(7) adds 4 implicit trailing spaces. Leading and trailing spaces are included in comparisons of CHAR values. As a result, two character values with different lengths (CHAR(x) and CHAR(y) where x != y) will never be equal.

Example type definitions: char, char(20)

VARBINARY

Variable length binary data.

JSON

JSON value type, which can be a JSON object, a JSON array, a JSON number, a JSON string, true, false or null.

Date and Time

DATE

Calendar date (year, month, day).

Example: DATE '2001-08-22'

TIME

Time of day (hour, minute, second, millisecond) without a time zone. Values of this type are parsed and rendered in the session time zone.

Example: TIME '01:02:03.456'

TIME WITH TIME ZONE

Time of day (hour, minute, second, millisecond) with a time zone. Values of this type are rendered using the time zone from the value.

Example: TIME '01:02:03.456 America/Los_Angeles'

TIMESTAMP

Instant in time that includes the date and time of day without a time zone. Values of this type are parsed and rendered in the session time zone.

Example: TIMESTAMP '2001-08-22 03:04:05.321'

TIMESTAMP WITH TIME ZONE

Instant in time that includes the date and time of day with a time zone. Values of this type are rendered using the time zone from the value.

Example: TIMESTAMP '2001-08-22 03:04:05.321 America/Los_Angeles'

INTERVAL YEAR TO MONTH

Span of years and months.

Example: INTERVAL '3' MONTH

INTERVAL DAY TO SECOND

Span of days, hours, minutes, seconds and milliseconds.

Example: INTERVAL '2' DAY

Structural

ARRAY

An array of the given component type.

Example: ARRAY[1, 2, 3]

MAP

A map between the given component types.

Example: MAP(ARRAY['foo', 'bar'], ARRAY[1, 2])

ROW

A structure made up of named fields. The fields may be of any SQL type, and are accessed with field reference operator .

Example: CAST(ROW(1, 2.0) AS ROW(x BIGINT, y DOUBLE))

Network Address

IPADDRESS

An IP address that can represent either an IPv4 or IPv6 address. Internally, the type is a pure IPv6 address. Support for IPv4 is handled using the IPv4-mapped IPv6 address range (RFC 4291#section-2.5.5.2). When creating an IPADDRESS, IPv4 addresses will be mapped into that range. When formatting an IPADDRESS, any address within the mapped range will be formatted as an IPv4 address. Other addresses will be formatted as IPv6 using the canonical format defined in RFC 5952.

Examples: IPADDRESS '10.0.0.1', IPADDRESS '2001:db8::1'

HyperLogLog

Calculating the approximate distinct count can be done much more cheaply than an exact count using the HyperLogLog data sketch. See HyperLogLog Functions.

HyperLogLog

A HyperLogLog sketch allows efficient computation of approx_distinct(). It starts as a sparse representation, switching to a dense representation when it becomes more efficient.

P4HyperLogLog

A P4HyperLogLog sketch is similar to HyperLogLog, but it starts (and remains) in the dense representation.

Quantile Digest

QDigest

A quantile digest (qdigest) is a summary structure which captures the approximate distribution of data for a given input set, and can be queried to retrieve approximate quantile values from the distribution. The level of accuracy for a qdigest is tunable, allowing for more precise results at the expense of space.

A qdigest can be used to give approximate answer to queries asking for what value belongs at a certain quantile. A useful property of qdigests is that they are additive, meaning they can be merged together without losing precision.

A qdigest may be helpful whenever the partial results of approx_percentile can be reused. For example, one may be interested in a daily reading of the 99th percentile values that are read over the course of a week. Instead of calculating the past week of data with approx_percentile, qdigests could be stored daily, and quickly merged to retrieve the 99th percentile value.

presto spi开发

spi开发入门

presto 优化

presto可以快速的进行大数据量的查询,但是会涉及到不同的应用场景,针对不同的应用场景,可以采用不同的措施提升presto的性能。本章主要针对就是针对不同的应用场景, 详细描述presto性能优化的手段。

presto 基本优化

合理设置分区

当使用hive作为数据源的时候,hive数据仓库中的数据可以通过某些事件、区域信息的字段进行分区,使用presto查询hive数据仓库中的数据时,通过在查询语句中根据 分区字段限定查询的范围,可以有效的避免读取不必要的数据。这样可以显著的提升查询的性能。

hive> show partitions t_ods_industry_atd;
event_type_id=1/ds=2019-07-07
event_type_id=1/ds=2019-08-30
event_type_id=10/ds=2019-07-07
event_type_id=10/ds=2019-07-14
event_type_id=10/ds=2019-08-30
event_type_id=11/ds=2019-03-29
event_type_id=11/ds=2019-07-07
event_type_id=12/ds=2019-07-07
event_type_id=12/ds=2019-07-08
event_type_id=12/ds=2019-07-10
event_type_id=12/ds=2019-07-11
event_type_id=12/ds=2019-08-30
event_type_id=13/ds=2019-07-07
event_type_id=13/ds=2019-07-08
event_type_id=13/ds=2019-07-10
event_type_id=14/ds=2019-07-07
event_type_id=14/ds=2019-07-08

group by字句优化

合理安排group by字句中字段的顺序可以稍微提升查询性能,尤其是在一个特别大的表中两个字段值数量差别特别打的时候,如果group by子句设置不好的话,会导致查询内存溢出。

优化策略:如果group by子句中包含两个字段,其中column1中的distinct值的数量要远大于column2中distinct值的数量,则需要将column1放在column2的左边,也就是 group by字句中的字段按照每个字段distinct数据的多少进行降序排序。

presto:industry> select approx_distinct(src_ip) from t_ods_industry_atd;
  _col0
---------
 1355237
(1 row)

Query 20190830_073515_32089_fqui7, FINISHED, 5 nodes
Splits: 8,011 total, 8,011 done (100.00%)
0:01 [105M rows, 691MB] [79.2M rows/s, 520MB/s]

presto:industry> select approx_distinct(src_port) from t_ods_industry_atd;
 _col0
-------
 67024
(1 row)

Query 20190830_073523_32111_fqui7, FINISHED, 5 nodes
Splits: 8,011 total, 8,011 done (100.00%)
0:02 [105M rows, 376MB] [63.5M rows/s, 227MB/s]

presto:industry> select src_port,src_ip,count(1) as counter from t_ods_industry_atd group by src_port,src_ip order by counter desc limit 10;
 src_port |     src_ip      | counter
----------+-----------------+---------
 1        | 157.122.62.205  |  137192
 0        | 111.8.88.245    |  119173
 NULL     | NULL            |   84779
 0        | 111.6.87.71     |   62796
 8888     | 157.122.62.205  |   60358
 0        | 93.174.93.195   |   59618
 53       | 59.51.78.211    |   52472
 0        | 94.102.56.215   |   49422
 0        | 117.135.199.242 |   47077
 12080    | 113.240.233.124 |   46464
(10 rows)

Query 20190830_073824_32387_fqui7, FINISHED, 5 nodes
Splits: 8,179 total, 8,179 done (100.00%)
0:17 [105M rows, 1.03GB] [6.06M rows/s, 61.1MB/s]

presto:industry> select src_ip,src_port,count(1) as counter from t_ods_industry_atd group by src_ip,src_port order by counter desc limit 10;
     src_ip      | src_port | counter
-----------------+----------+---------
 157.122.62.205  | 1        |  137192
 111.8.88.245    | 0        |  119173
 NULL            | NULL     |   84779
 111.6.87.71     | 0        |   62796
 157.122.62.205  | 8888     |   60358
 93.174.93.195   | 0        |   59618
 59.51.78.211    | 53       |   52472
 94.102.56.215   | 0        |   49422
 117.135.199.242 | 0        |   47077
 113.240.233.124 | 12080    |   46464
(10 rows)

Query 20190830_073859_32410_fqui7, FINISHED, 5 nodes
Splits: 8,179 total, 8,179 done (100.00%)
0:15 [105M rows, 1.03GB] [6.91M rows/s, 69.6MB/s]

模糊聚合函数

presto提供了一系列的模糊聚合函数,使用这些函数虽然会有一点误差,但是可以获得巨大的性能提升。例如使用模糊聚合函数approx_distinct()函数,你的查询性能相对于 count(distinct x)来说会获得巨大的提升,而且与count(distinct x)查询的结果误差率为2.3%。

presto:industry> select count(1) from t_ods_industry_atd;
   _col0
-----------
 105202099
(1 row)

Query 20190830_070255_29354_fqui7, FINISHED, 5 nodes
Splits: 7,963 total, 7,963 done (100.00%)
0:02 [105M rows, 0B] [52.8M rows/s, 0B/s]

presto:industry> select count(distinct src_ip) from t_ods_industry_atd;
  _col0
---------
 1387045
(1 row)

Query 20190830_070219_29213_fqui7, FINISHED, 5 nodes
Splits: 8,123 total, 8,123 done (100.00%)
0:06 [105M rows, 687MB] [16.3M rows/s, 106MB/s]

presto:industry> select approx_distinct(src_ip) from t_ods_industry_atd;
  _col0
---------
 1355237
(1 row)

Query 20190830_070200_29149_fqui7, FINISHED, 5 nodes
Splits: 7,963 total, 7,963 done (100.00%)
0:02 [105M rows, 691MB] [44.2M rows/s, 291MB/s]

合并多条like字句为一条regexp_like

presto的查询优化器无法对使用很多like字句的查询进行优化。因此,如果你的查询语句中还有很多like字句,那么你的查询执行的性能会非常低下。为了提升性能,可以将 以or语句连接的多个like字句携程一个regexp_like字句。

presto:industry> select count(1) from t_ods_industry_atd where regexp_like(src_port,'80|90|521');
  _col0
---------
 3810439
(1 row)

Query 20190830_070951_30233_fqui7, FINISHED, 5 nodes
Splits: 7,970 total, 7,970 done (100.00%)
0:02 [105M rows, 372MB] [46M rows/s, 163MB/s]

大表放在join字句左边

在默认情况下,presto使用distributed hash join算法,在这种算法中,join左右两边的表都会根据join键的值进行分区。左表的每个分区都会被通过网络传入到右表所在 分区的worker节点上。也就是说,在进行join关联的时候,会先把右表的所有分区全部分布到各个计算节点上保存,然后等待将左表中的各个分区依次通过网络传输stream 到相应的计算节点上进行计算。由于右表的所有分区需要全部分布到各个节点上进行存储,所以有一个限制:就是集群中所有内存的代销一定要大于右表的大小。

如果你在执行join查询的时候看到错误:task exceeded max memory size,那么这经常意味着join连接的右表大于集群所有内存的大小。presto不会自动进行join两边表 顺序的优化,因此在执行join查询的时候,请确保大表放在join的左边,小表放在join右边。

关闭distributed hash join

若数据存在数据倾斜,那么hash join的性能就会急剧下降。若表足够小以至于可以存储在一个节点的内存中(通常这种表小于2gb),那么就可以将小表放在右边,然后用户 通过客户端内置的session参数将distributed hash join关闭。当将distributed hash join关闭之后,两个表均不会在进行进行hash重新分布,会将右表广播到针对与左表 source stage的每个节点上进行join操作。

set distributed_join='false'

使用orc存储

由于orc是列式结构化存储,而且当数据量大时,orc文件的存储空间比lzo和text文件所使用的存储空间都要小。除此之外,presto对与orc文件的读取也做了特定的优化,因此 强力建议在hive中创建presto使用的表时,采用orc格式存储。

create table t (...) store as orc;

采用orc格式存储有以下有点:

  • orc列式存储,因此查询与居住哎读取数据时可以避免读取一行中不会被使用的列中的内容,减少无用数据的读取。
  • orc是结构化存储文件,在文件头中存储了很多文件数据的源数据信息,因此在执行统计性查询的时候,其性能远远高于其他存储格式,例如执行count语句,你会发现orc格式的表的查询性能会非常高。
  • orc文件的存储空间非常小,尤其随着单个文件存储的数据量越来越大,相比其他存储格式的文件,orc的有效存储率会越来越高。

presto 常见问题

使用presto的过程中会由于自己知识点的不足导致经常出现一些不容易解决的问题,这些问题有presto架构设计的问题,有使用方式错误的问题。总之我们需要耐着性子仔细去查看和搜索。
相信自己,总会找到答案的。在这里我把我自己遇到的一些问题整理出来,方便自己查证。

task exceeded max memory

出错原因

presto会将每个查询语句最终分解为在每个节点上运行的task,并且会在配置文件中配置每个task可以使用的最大内存大小。若某个查询语句中的某个task处理的数据超过给task可以使用的最大内存代销,则会跑出上面的错误。

解决方案

修改presto配置文件config.properties,提供其中的配置属性task.max-memory的值。

presto读取hive的parquet表

无法读取 Parquet 文件

使用 Presto 查询 Hive 表,抛异常 com.facebook.presto.spi.PrestoException: Can not read value at 0 in block 0 in file hdfs

Presto 版本 0.211

Spark 版本 2.2.0

导致问题的原因是 Spark 写入到 Hive 表中的 Parquet 文件版本过高,导致 Presto 无法读取

解决办法:

在构建 SparkSession 时,添加配置 spark.sql.parquet.writeLegacyFormat,一个🌰:

SparkSession.builder()
  .master("local")
  .appName(appName)
  .enableHiveSupport()
  .config("spark.sql.parquet.writeLegacyFormat", true) // ①
  .getOrCreate()
① 在 Spark 2.2 中,在写入 Parquet 时,会启动 Legacy 模式,从而可以兼容 Spark 1.4 及之前的版本

Presto 查询到错误的列

Presto 查询文件格式为 Parquet 的 Hive 表,查询其中一个字段却返回另一个字段的数据,数据格式匹配不上,无法查看到最新数据

Presto 版本 0.211

导致该问题的原因是查询 Hive 表 Schema 和底层 Parquet 数据文件 Schema 不一致导致的。改问题有两种解决方式:

修改 Hive 表 Schema 保持与底层 Parquet 数据文件 Schema 一致;
修改 Hive Catalog 配置文件,新增配置 hive.parquet.use-column-names=true
关于 hive.parquet.use-column-names 属性,官方的解释:

By default, columns in Parquet files are accessed by their ordinal position in the Hive table definition. To access columns based on the names recorded in the Parquet file, set hive.parquet.use-column-names=true in your Hive catalog properties file.

Hive 元数据缓存

有时 Hive 底层数据文件发生变化,由于 Presto 缓存了 Hive 元数据。禁用 Hive 元数据缓存,编辑 Hive 配置文件,添加以下配置:

hive.metastore-cache-ttl=0
hive.metastore-refresh-interval=0

prsto查询hive的空文件报错

presto查询hive的connector的表数据的时候,如果hive表中存在空文件会导致presto查询报错。所以我们需要找到hdfs里面的空文件,并且将该空文件删除掉。目前我是编写了一个python
脚本程序进行自动查找空文件和删除空文件。

报错异常

xxxx.parquet file is small or is not a parquet file

解决办法

编写python脚本查找和删除hdfs空文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/7/26 11:36
# @Author  : ganliang
# @File    : hdfsemptytool.py
# @Desc    : HDFS工具類,提供根据文件大小查找文件,删除文件等功能

import logging
import os
import re
import sys

LOGGING_CONFIG = {
    # "filename": "config.log",
    # "filemode": "w",
    "format": "%(asctime)s|%(process)d|%(thread)d|%(filename)s[%(funcName)s:%(lineno)d]|%(levelname)s|%(message)s",
    "level": logging.INFO
}
logging.basicConfig(**LOGGING_CONFIG)


def find_file_bysize(urls=None, empty_files=None, beginsize=0L, endsize=0L):
    """
    根据问价的大小来匹配文件
    :param urls: 需要收集的url列表
    :param empty_files: 收集到的空文件列表
    :param beginsize: 从多少字节开始
    :param endsize: 从多少字节结束
    :return:
    """
    if endsize == 0L: endsize = beginsize
    if endsize < beginsize: raise Exception("endsize is letter than beginsize")

    for url in urls:
        hdfs_cmd = "hdfs dfs -ls {0}".format(url)
        logging.debug(hdfs_cmd)
        std_in, std_out = os.popen2(hdfs_cmd)
        lines = std_out.readlines()

        for line in lines:
            line = line.strip()
            hdfs_attrs = re.split("\s+", line)
            logging.debug(line)
            if len(hdfs_attrs) < 6: continue
            url = hdfs_attrs[-1]
            permissiong = hdfs_attrs[0]
            # 目录
            if permissiong.startswith("d"):
                find_file_bysize([url], empty_files, beginsize, endsize)
            # 文件
            else:
                size = hdfs_attrs[4]
                if int(size) >= beginsize and int(size) <= endsize:
                    empty_files.append(url)
                    logging.info("find:{0}".format(line))
    return empty_files


def remover_file(empty_files=None):
    """
    删除空文件
    :param empty_files: 收集的空文件
    :return:
    """
    for empty_file in empty_files:
        delete_cmd = "hdfs dfs -rm -skipTrash {0}".format(empty_file)
        logging.info(delete_cmd)
        dstd_in, dstd_out = os.popen2(delete_cmd)
        logging.info("".join(dstd_out.readlines()))


def merge_files(url):
    """
    合并目录下的文件
    :param url: 目录文件
    :return:
    """
    pass


if __name__ == "__main__":

    args = sys.argv[1:]

    operation = str(args[0]).upper() if len(args) > 0 else ""


    def _find_hdfs_file_(args):
        urls, beginsize, endsize = args[1], 0L, 0L
        if len(args) >= 3: beginsize = args[2]
        if len(args) >= 4: endsize = args[3]
        hdfs_urls = [url for url in urls.split(",")]
        logging.info("\n".join(hdfs_urls))
        empty_files = find_file_bysize(hdfs_urls, [], beginsize, endsize)
        logging.info("total empty files:{0}".format(len(empty_files)))
        return empty_files


    ##找到匹配的文件
    if operation == "FIND":
        if len(args) < 2:
            logging.error("usage: python hdfstool.py find urls beginsize[0] endsize[0]")
            sys.exit(-1)
        _find_hdfs_file_(args)
    # 删除指定的文件
    elif operation == "REMOVE":
        if len(args) < 2:
            logging.error("usage: python hdfstool.py remover urls beginsize[0] endsize[0]")
            sys.exit(-1)
        empty_files = _find_hdfs_file_(args)
        remover_file(empty_files)
    else:
        logging.error("usage: python hdfstool.py find|remove urls beginsize[0] endsize[0]")