本文档主要介绍如何在文件存储HDFS上搭建及使用Presto。

背景信息

Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持GB到PB字节。Presto支持在线数据查询,包括Hive、Cassandra、关系数据库以及专有数据存储。
说明 在本文档中Presto是通过连接Hive的元数据服务来读取文件存储HDFS上的数据,在文件存储HDFS上使用Presto时需要额外配置一些依赖包,详细操作步骤请参见配置Presto

准备工作

在文件存储HDFS上搭建和使用Presto,需要先完成以下准备工作。

  1. 开通文件存储HDFS服务并创建文件系统实例和挂载点,详情请参见HDFS快速入门
  2. 在Hadoop集群所有节点上安装JDK。
    版本不能低于1.8。
  3. 在Hadoop集群中配置文件存储HDFS实例,详情请参见挂载文件系统
  4. 在Hadoop集群中安装Apache Hive,本文档中使用的Apache Hive版本为1.2.1。
  5. 下载Presto压缩包和presto-cli-xxx-executable.jar。
    Presto下载地址:官方链接,在本文档使用Presto的版本为0.227。

配置Presto

您可以参见以下步骤配置Presto,Presto官方配置文档请参见Deploying Presto

  1. 解压Presto压缩包到指定文件夹。
    tar -zxvf presto-server-0.227.tar.gz -C /usr/local/
  2. 在Presto解压目录下创建etc目录。
    mkdir /usr/local/presto-server-0.227/etc
  3. 配置Node Properties。
    1. 创建etc/node.properties文件。
      vim /usr/local/presto-server-0.227/etc/node.properties
    2. etc/node.properties文件中添加如下内容。
      node.environment=production
      node.id=ffffffff-ffff-ffff-ffff-ffffffffffff
      node.data-dir=/var/presto/data
  4. 配置JVM Config。
    1. 创建etc/jvm.config文件。
      vim /usr/local/presto-server-0.227/etc/jvm.config
    2. etc/jvm.config文件中添加如下内容。
      -server
      -Xmx8G
      -XX:+UseG1GC
      -XX:G1HeapRegionSize=32M
      -XX:+UseGCOverheadLimit
      -XX:+ExplicitGCInvokesConcurrent
      -XX:+HeapDumpOnOutOfMemoryError
      -XX:+ExitOnOutOfMemoryError
  5. 配置Config Properties。

    在本文档中将coordinator和worker配置在同一台机器上,您可以参见Presto官方文档将coordinator和worker配置到不同的机器。

    1. 创建etc/config.properties文件。
      vim /usr/local/presto-server-0.227/etc/config.properties
    2. etc/config.properties中添加如下内容。
      coordinator=true
      node-scheduler.include-coordinator=true
      http-server.http.port=8080
      query.max-memory=5GB
      query.max-memory-per-node=1GB
      query.max-total-memory-per-node=2GB
      discovery-server.enabled=true
      discovery.uri=http://xx.xx.xx.xx:8080 #xx.xx.xx.xx为当前机器的IP地址
  6. 配置日志级别。
    1. 创建etc/log.properties文件。
      vim /usr/local/presto-server-0.227/etc/log.properties
    2. etc/log.properties文件中添加如下内容。
      com.facebook.presto=INFO
  7. 配置Catalog Properties。
    1. 创建etc/catalog文件夹。
      mkdir /usr/local/presto-server-0.227/etc/catalog
    2. 创建etc/catalog/hive.properties文件。
      vim /usr/local/presto-server-0.227/etc/catalog/hive.properties
    3. etc/catalog/hive.properties文件中添加如下内容。
      connector.name=hive-hadoop2
      hive.metastore.uri=thrift://xxxx:9083 #xxxx为启动hive元数据服务的IP地址
      hive.config.resources=/usr/local/hadoop-2.7.6/etc/hadoop/core-site.xml #配置为您的Hadoop集群中core-site.xml文件的地址
  8. 编译并替换jar包。

    Presto中以maven-shade-plugin的方式引入了Hadoop,使用relocation的方式对引入的Hadoop jar包地址进行重命名,因为文件存储HDFS的sdk与Hadoop共用了protobuf-xxx.jar包,在Presto通过hive metastore读取文件存储HDFS上的数据时,文件存储HDFS的sdk会获取不到Presto进行重命名地址的protobuf-xxx.jar包。为了避免兼容性问题,文件存储HDFS的sdk需要作为Presto的Hadoop的依赖项,并对Presto中引入的Hadoop的jar包hadoop-apache2-xxx.jar进行重新编译。

    在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第1张在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第2张
    1. 查看您安装的Presto中的presto-hadoop-apache2版本。

      在0.227版本的presto中对应的presto-hadoop-apache2版本为hadoop-apache2-2.7.4-5。

      find  /usr/local/presto-server-0.227/ -name hadoop-apache2*
    2. 下载presto-hadoop-apache2对应版本的源码,下载地址:官方链接
      git clone https://github.com/prestodb/presto-hadoop-apache2.git
    3. 打开源码中的/root/presto-hadoop-apache2-2.7.4-5/pom.xml文件。
      vim /root/presto-hadoop-apache2-2.7.4-5/pom.xml
    4. /root/presto-hadoop-apache2-2.7.4-5/pom.xml文件中添加文件存储HDFS sdk的依赖项。本文档中使用的sdk的版本为 1.0.3。
      <dependency>
            <groupId>com.aliyun.dfs</groupId>
            <artifactId>aliyun-sdk-dfs</artifactId>
            <version>1.0.3</version>
      </dependency>
      在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第3张
    5. 编译presto-hadoop-apache2。
      cd /root/presto-hadoop-apache2-2.7.4-5
      mvn clean package -DskipTests
    6. 查看生成的hadoop-apache2-2.7.4-5.jar。
      cd ~/presto-hadoop-apache2-2.7.4-5/target
      ll -h
      在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第4张
    7. 删除旧的hadoop-apache2-2.7.4-5.jar依赖包。
      rm -f /usr/local/presto-server-0.227/plugin/raptor/hadoop-apache2-2.7.4-5.jar /usr/local/presto-server-0.227/plugin/accumulo/hadoop-apache2-2.7.4-5.jar /usr/local/presto-server-0.227/plugin/hive-hadoop2/hadoop-apache2-2.7.4-5.jar
    8. 将新的hadoop-apache2-2.7.4-5.jar依赖包拷贝到对应的目录下。
      cp ~/presto-hadoop-apache2-2.7.4-5/target/hadoop-apache2-2.7.4-5.jar  /usr/local/presto-server-0.227/plugin/raptor/
      cp ~/presto-hadoop-apache2-2.7.4-5/target/hadoop-apache2-2.7.4-5.jar  /usr/local/presto-server-0.227/plugin/accumulo/
      cp ~/presto-hadoop-apache2-2.7.4-5/target/hadoop-apache2-2.7.4-5.jar  /usr/local/presto-server-0.227/plugin/hive-hadoop2/
  9. 将presto-cli-xxx-executable.jar复制到Presto安装的bin目录下重命名并赋予可执行权限。
    cp ~/presto-cli-0.227-executable.jar  /usr/local/presto-server-0.227/bin/
    mv /usr/local/presto-server-0.227/bin/presto-cli-0.227-executable.jar  /usr/local/presto-server-0.227/bin/presto
    chmod +x /usr/local/presto-server-0.227/bin/presto

验证Presto

  1. 启动Hive的元数据服务。
    /usr/local/apache-hive-1.2.1-bin/bin/hive --service metastore
  2. 创建测试数据并加载到Hive中。
    1. 创建测试数据。
      echo -e "test1\ntest2\ntest1\ntest2\ntest3\ntest4\ntest4\ntest5" > ~/test.txt
    2. 将测试数据上传到文件存储HDFS上。
      hadoop fs -put ~/test.txt /presto
    3. 使用默认的数据创建test_data并加载数据。
      hive> create external table test_data(word string) row format delimited fields terminated by '\n' stored as textfile location '/presto';
    4. 查看数据是否加载成功。
      hive> select * from test_data;

      如果显示如下类似信息,则表示数据加载成功。

      在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第5张
  3. 使用Presto通过Hive读取文件存储HDFS上的数据并进行计算。
    1. 启动presto server。
      /usr/local/presto-server-0.227/bin/launcher start
    2. 使用presto连接Hive。
      /usr/local/presto-server-0.227/bin/presto  --server localhost:8080 --catalog hive --schema default
    3. 读取文件存储HDFS上的数据。
      presto:default> select * from test_data;
      在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第6张
    4. 进行word count计算。
      presto:default> select word, count(*) from test_data group by word;
      在文件存储HDFS上使用Presto_最佳实践_文件存储 HDFS 阿里云技术文档 第7张