├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── README.md ├── docker-compose.yml ├── etc ├── catalog │ ├── hive.properties │ ├── mongodb.properties │ ├── mysql.properties │ ├── postgres.properties │ └── tcph.properties ├── config.properties ├── jvm.config └── node.properties └── jupyter ├── Dockerfile └── f8_demo.ipynb /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | Facebook has adopted a Code of Conduct that we expect project participants to adhere to. 4 | Please read the [full text](https://code.fb.com/codeofconduct/) 5 | so that you can understand what actions will and will not be tolerated. 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to f8-2019-demo 2 | 3 | We want to make contributing to this project as easy and transparent as 4 | possible. 5 | 6 | ## Our Development Process 7 | 8 | This is a demo that present an introduction to get started with Presto. To 9 | contribute please fork the repository and open a pull-request. 10 | 11 | ## Pull Requests 12 | 13 | We actively welcome your pull requests. 14 | 15 | 1. Fork the repo and create your branch from `master`. 16 | 4. Ensure `docker-compose up` succeeds 17 | 6. If you haven't already, complete the Contributor License Agreement ("CLA"). 18 | 19 | ## Contributor License Agreement ("CLA") 20 | 21 | In order to accept your pull request, we need you to submit a CLA. You only need 22 | to do this once to work on any of Facebook's open source projects. 23 | 24 | Complete your CLA here: 25 | 26 | ## Issues 27 | 28 | We use GitHub issues to track public bugs. Please ensure your description is 29 | clear and has sufficient instructions to be able to reproduce the issue. 30 | 31 | Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe 32 | disclosure of security bugs. In those cases, please go through the process 33 | outlined on that page and do not file a public issue. 34 | 35 | ## Coding Style 36 | 37 | * 2 spaces for indentation rather than tabs 38 | * 80 character line length 39 | 40 | ## License 41 | 42 | By contributing to f8-2019-demo, you agree that your contributions will be 43 | licensed under the LICENSE file in the root directory of this source tree. 44 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | FROM openjdk:8-jre 16 | EXPOSE 8080 17 | 18 | MAINTAINER Greg Leclercq "ggreg@fb.com" 19 | ARG PRESTO_VERSION=0.218 20 | ENV PRESTO_PKG presto-server-$PRESTO_VERSION.tar.gz 21 | ENV PRESTO_PKG_URL https://repo1.maven.org/maven2/com/facebook/presto/presto-server/$PRESTO_VERSION/$PRESTO_PKG 22 | 23 | ENV PRESTO_CLI_JAR_URL https://repo1.maven.org/maven2/com/facebook/presto/presto-cli/$PRESTO_VERSION/presto-cli-$PRESTO_VERSION-executable.jar 24 | 25 | 26 | # Install python to run the launcher script 27 | RUN apt-get update 28 | RUN apt-get install -y python less 29 | 30 | # Download Presto package 31 | # Use curl rather ADD to leverage RUN caching 32 | # Let curl show progress bar to prevent Travis from thinking the job is stalled 33 | RUN curl -o /$PRESTO_PKG $PRESTO_PKG_URL 34 | RUN tar -zxf /$PRESTO_PKG 35 | 36 | # Create directory for Presto data 37 | RUN mkdir -p /var/lib/presto/data 38 | 39 | # Add Presto configuration 40 | WORKDIR /presto-server-$PRESTO_VERSION 41 | RUN mkdir etc 42 | ADD etc/jvm.config etc/ 43 | ADD etc/config.properties etc/ 44 | ADD etc/node.properties etc/ 45 | ADD etc/catalog etc/catalog 46 | 47 | # Download Presto CLI 48 | RUN mkdir -p bin 49 | RUN curl -o bin/presto-cli $PRESTO_CLI_JAR_URL 50 | RUN chmod +x bin/presto-cli 51 | 52 | CMD bin/launcher.py run 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Presto 2 | 3 | ## Introduction 4 | 5 | This repository contains the files used to present the F8 2019 classroom demo: 6 | *Getting Started with Presto: Run SQL at any Scale*. They will setup a Presto cluster 7 | with MySQL and MongoDB instances that you can query from Presto. 8 | 9 | ## Requirements 10 | 11 | - Docker (you can use [Docker CE](https://docs.docker.com/install/)) 12 | - Docker [Compose](https://docs.docker.com/compose/install/) 13 | 14 | The demo was develop on Mac OS X. 15 | 16 | ## Quick Start 17 | 18 | To start the Presto cluster and its dependencies: 19 | ``` 20 | docker-compose up 21 | ``` 22 | 23 | Then open the Jupyter Notebook at [`localhost:8888`](http://localhost:8888). 24 | 25 | The Jupyter Notebook password is `demo`. You can override it by changing the 26 | SHA1 hash of `--NotebookApp.password='sha1:2017f6d8a65d:4f2115202d4cd8c081f1c135bc2b41292bcb4ec4'` 27 | in `docker-compose.yml`. 28 | 29 | The Presto UI is available at [`localhost:8080`](http://localhost:8080). 30 | 31 | To run the Presto CLI: 32 | 33 | ``` 34 | docker exec -it f8-2019-demo_presto_1 bin/presto-cli 35 | ``` 36 | 37 | You can find Presto's documentation on [prestodb.io/docs/current/](http://prestodb.github.io/docs/current/). 38 | 39 | If you update a `Dockerfile`, use `docker-compose up --build` to ignore cache 40 | and rebuild the images. 41 | 42 | ## Next Steps 43 | 44 | In this demo, we run all Presto nodes on the same machine. Presto has two types of nodes: 45 | - The coordinator is the main server that compiles SQL And manages its execution 46 | - The worker is a node that executes tasks scheduled by the coordinator 47 | 48 | To run Presto in a distributed way, you will create a new instance with 49 | `coordinator=false` in `etc/config.properties`. 50 | 51 | - More about [deploying](http://prestodb.github.io/docs/current/installation/deployment.html) Presto 52 | - Configuration properties [reference](http://prestodb.github.io/docs/current/admin/properties.html) 53 | - [Developing](http://prestodb.github.io/docs/current/develop.html) extensions and integrations for Presto 54 | 55 | ## References 56 | 57 | - https://prestodb.io 58 | - https://github.com/prestodb/presto 59 | - http://tinyurl.com/presto-paper 60 | - https://prestodb.io/resources.html#libraries 61 | - https://jupyter.org 62 | 63 | ## Contributing 64 | 65 | See the [CONTRIBUTING](CONTRIBUTING.md) file for how to help out. 66 | 67 | ## License 68 | 69 | f8-2019-demo is Apache 2.0 licensed, as found in the LICENSE file. 70 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | --- 15 | version: '3' 16 | services: 17 | presto: 18 | build: . 19 | links: 20 | - mongodb 21 | - mysql 22 | depends_on: 23 | - mongodb 24 | - mysql 25 | ports: 26 | - "8080:8080" 27 | 28 | mysql: 29 | image: mysql 30 | environment: 31 | MYSQL_ROOT_PASSWORD: 'mysql' 32 | MYSQL_USER: 'presto' 33 | MYSQL_PASSWORD: 'mysql' 34 | ports: 35 | - "3306:3306" 36 | 37 | mongodb: 38 | image: mongo 39 | ports: 40 | - "27017:27017" 41 | 42 | jupyter: 43 | build: jupyter 44 | links: 45 | - mongodb 46 | - mysql 47 | ports: 48 | - "8888:8888" 49 | 50 | depends_on: 51 | - presto 52 | 53 | command: start-notebook.sh --NotebookApp.password='sha1:2017f6d8a65d:4f2115202d4cd8c081f1c135bc2b41292bcb4ec4' 54 | -------------------------------------------------------------------------------- /etc/catalog/hive.properties: -------------------------------------------------------------------------------- 1 | connector.name=hive-hadoop2 2 | hive.metastore.uri=thrift://hive:9083 3 | -------------------------------------------------------------------------------- /etc/catalog/mongodb.properties: -------------------------------------------------------------------------------- 1 | connector.name=mongodb 2 | mongodb.seeds=mongodb 3 | 4 | -------------------------------------------------------------------------------- /etc/catalog/mysql.properties: -------------------------------------------------------------------------------- 1 | connector.name=mysql 2 | connection-url=jdbc:mysql://mysql:3306 3 | connection-user=root 4 | connection-password=mysql 5 | -------------------------------------------------------------------------------- /etc/catalog/postgres.properties: -------------------------------------------------------------------------------- 1 | connector.name=postgresql 2 | connection-url=jdbc:postgresql://postgres:5432 3 | connection-user=root 4 | connection-password=example 5 | -------------------------------------------------------------------------------- /etc/catalog/tcph.properties: -------------------------------------------------------------------------------- 1 | connector.name=tpch 2 | -------------------------------------------------------------------------------- /etc/config.properties: -------------------------------------------------------------------------------- 1 | coordinator=true 2 | node-scheduler.include-coordinator=true 3 | http-server.http.port=8080 4 | discovery-server.enabled=true 5 | discovery.uri=http://localhost:8080 6 | 7 | query.max-memory=500MB 8 | query.max-memory-per-node=200MB 9 | -------------------------------------------------------------------------------- /etc/jvm.config: -------------------------------------------------------------------------------- 1 | -Xmx1G 2 | -XX:+UseG1GC 3 | -XX:G1HeapRegionSize=32M 4 | -XX:+UseGCOverheadLimit 5 | -XX:+ExplicitGCInvokesConcurrent 6 | -------------------------------------------------------------------------------- /etc/node.properties: -------------------------------------------------------------------------------- 1 | node.environment=test 2 | node.id=test 3 | node.data-dir=/var/lib/presto/data 4 | -------------------------------------------------------------------------------- /jupyter/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | FROM jupyter/scipy-notebook:latest 16 | USER root 17 | RUN apt-get update -y && apt-get install -qy graphviz libmysqlclient-dev mongodb-clients mysql-client wget host 18 | RUN pip install presto-python-client pyhive graphviz mysqlclient mongo 19 | COPY f8_demo.ipynb /home/jovyan/ 20 | -------------------------------------------------------------------------------- /jupyter/f8_demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Connecting to Presto\n", 8 | "\n", 9 | "Presto ships with a CLI. Run `docker exec -it classroom-presto_presto_1 bin/presto-cli` do access it from the Presto container. the Presto CLI supports autocompletion, history, progress bars and other useful features. For quickly testing queries the CLI is very helpful.\n", 10 | "\n", 11 | "In a environment like a Jupyter notebook, we can use a Presto Python client. The Presto client library implements the Python DBAPI2.0 interface that is used by common database client libraries for querying MySQL, PostgreSQL and SQLite.\n", 12 | "\n", 13 | "DBAPI2.0 defines a API with a `Connection`. Queries then happen with a `cursor`. Presto supports transaction. The level of isolation depends on the connectors involved in a query.\n", 14 | "\n", 15 | "The three mandatory arguments to create a connection are *host*, *port*, and *user*.\n", 16 | "Other arguments such as *source* allow to identify the origin of the query. A common use case is to use it to tell which service, tool, or code sent the query.\n", 17 | "\n", 18 | "Let's create a connection:" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "" 30 | ] 31 | }, 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "import prestodb.dbapi as presto\n", 39 | "\n", 40 | "conn = presto.Connection(host=\"presto\", port=8080, user=\"demo\")\n", 41 | "cur = conn.cursor()\n", 42 | "cur" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## Configuration\n", 50 | "\n", 51 | "Presto's general configuration is documented in the [deployment](https://prestodb.github.io/docs/current/installation/deployment.html) page. There are 4 types of configuration files:\n", 52 | "- Node Properties: to configure the coordinator (main server) and worker nodes.\n", 53 | "- JVM Config: command line options for the Java Virtual Machine that runs Presto.\n", 54 | "- Config Properties: configuration for the Presto server\n", 55 | "- Catalog Properties: configuration for Connectors (data sources)\n", 56 | "\n", 57 | "In the repository, the configuration is in `etc/`. The main file to configure Presto is `config.properties`:\n", 58 | "\n", 59 | "```\n", 60 | "coordinator=true\n", 61 | "node-scheduler.include-coordinator=true\n", 62 | "http-server.http.port=8080\n", 63 | "discovery-server.enabled=true\n", 64 | "discovery.uri=http://localhost:8080\n", 65 | "```\n", 66 | "\n", 67 | "The discovery is what allows worker nodes to find the coordinator and register themselves. Then they will participate in the execution of queries." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Catalogs\n", 75 | "\n", 76 | "A catalog is mapped to a connector. The name of configuration file for a catalgo defines the catalog's name. Here `etc/catalog/mysql.properties` configures the `mysql` catalog. We could name it `events` or `users`:\n", 77 | "\n", 78 | "```\n", 79 | "connector.name=mysql\n", 80 | "connection-url=jdbc:mysql://mysql:3306\n", 81 | "connection-user=USER\n", 82 | "connection-password=PASSWORD\n", 83 | "```\n", 84 | "\n", 85 | "We did the same with `etc/catalog/mongodb.properties`:\n", 86 | "\n", 87 | "```\n", 88 | "connector.name=mongodb\n", 89 | "mongodb.seeds=mongodb\n", 90 | "```\n", 91 | "\n", 92 | "Adding a catalog is a simple as adding a file with the catalog properties and named after the catalog's name.\n", 93 | "\n", 94 | "\n", 95 | "Below we list the available catalogs on the Presto cluster we are running:" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 3, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "[['hive'], ['mongodb'], ['mysql'], ['system'], ['tcph']]" 107 | ] 108 | }, 109 | "execution_count": 3, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "cur.execute(\"SHOW catalogs\")\n", 116 | "cur.fetchall()" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "## How Does Presto Execute a Query?\n", 124 | "\n", 125 | "If you are curious about what Presto translate a SQL query to and what it will run, you can you `EXPLAIN`:" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 11, 131 | "metadata": { 132 | "scrolled": true 133 | }, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "image/svg+xml": [ 138 | "\n", 139 | "\n", 141 | "\n", 143 | "\n", 144 | "\n", 146 | "\n", 147 | "distributed_plan\n", 148 | "\n", 149 | "\n", 150 | "cluster_0\n", 151 | "\n", 152 | "SINGLE\n", 153 | "\n", 154 | "\n", 155 | "cluster_1\n", 156 | "\n", 157 | "tcph:orders:150000000\n", 158 | "\n", 159 | "\n", 160 | "cluster_2\n", 161 | "\n", 162 | "tcph:orders:15000000\n", 163 | "\n", 164 | "\n", 165 | "\n", 166 | "plannode_1\n", 167 | "\n", 168 | "Output[orderkey, partkey, suppkey, linenumber, quantity, extendedprice, discount, tax, returnflag, linestatus, shipdate, commitdate, \n", 169 | "receiptdate, shipinstruct, shipmode, comment, orderkey, custkey, orderstatus, totalprice, orderdate, orderpriority, clerk, \n", 170 | "shippriority, comment]\n", 171 | "\n", 172 | "\n", 173 | "\n", 174 | "plannode_2\n", 175 | "\n", 176 | "Exchange 1:N\n", 177 | "\n", 178 | "\n", 179 | "\n", 180 | "plannode_1->plannode_2\n", 181 | "\n", 182 | "\n", 183 | "\n", 184 | "\n", 185 | "\n", 186 | "plannode_3\n", 187 | "\n", 188 | "InnerJoin\n", 189 | "\n", 190 | "("orderkey" = "orderkey_0")\n", 191 | "\n", 192 | "\n", 193 | "\n", 194 | "plannode_2->plannode_3\n", 195 | "\n", 196 | "\n", 197 | "\n", 198 | "\n", 199 | "\n", 200 | "plannode_4\n", 201 | "\n", 202 | "Project\n", 203 | "\n", 204 | "$hashvalue := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("orderkey"), 0))\n", 205 | "\n", 206 | "\n", 207 | "\n", 208 | "plannode_3->plannode_4\n", 209 | "\n", 210 | "\n", 211 | "\n", 212 | "\n", 213 | "\n", 214 | "plannode_6\n", 215 | "\n", 216 | "ExchangeNode[REPARTITION]\n", 217 | "\n", 218 | ""orderkey_0"\n", 219 | "\n", 220 | "\n", 221 | "\n", 222 | "plannode_3->plannode_6\n", 223 | "\n", 224 | "\n", 225 | "\n", 226 | "\n", 227 | "\n", 228 | "plannode_5\n", 229 | "\n", 230 | "TableScan[tcph:lineitem:sf100.0]\n", 231 | "\n", 232 | "\n", 233 | "\n", 234 | "plannode_4->plannode_5\n", 235 | "\n", 236 | "\n", 237 | "\n", 238 | "\n", 239 | "\n", 240 | "plannode_7\n", 241 | "\n", 242 | "Exchange 1:N\n", 243 | "\n", 244 | "\n", 245 | "\n", 246 | "plannode_6->plannode_7\n", 247 | "\n", 248 | "\n", 249 | "\n", 250 | "\n", 251 | "\n", 252 | "plannode_8\n", 253 | "\n", 254 | "Project\n", 255 | "\n", 256 | "$hashvalue_106 := "combine_hash"(bigint '0', COALESCE("$operator$hash_code"("orderkey_0"), 0))\n", 257 | "\n", 258 | "\n", 259 | "\n", 260 | "plannode_7->plannode_8\n", 261 | "\n", 262 | "\n", 263 | "\n", 264 | "\n", 265 | "\n", 266 | "plannode_9\n", 267 | "\n", 268 | "TableScan[tcph:orders:sf10.0]\n", 269 | "\n", 270 | "\n", 271 | "\n", 272 | "plannode_8->plannode_9\n", 273 | "\n", 274 | "\n", 275 | "\n", 276 | "\n", 277 | "\n" 278 | ], 279 | "text/plain": [ 280 | "" 281 | ] 282 | }, 283 | "execution_count": 11, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "conn = presto.Connection(host=\"presto\", port=8080, user=\"demo\", catalog=\"tcph\", schema=\"sf10\")\n", 290 | "cur = conn.cursor()\n", 291 | "cur.execute(\"explain (type distributed, format graphviz) select * from tcph.sf100.lineitem l join orders o on l.orderkey = o.orderkey\")\n", 292 | "plan = cur.fetchall()\n", 293 | "\n", 294 | "import graphviz\n", 295 | "\n", 296 | "graphviz.Source(plan[0][0])" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "Here we asked Presto to return the query plan in graphviz format.\n", 304 | "\n", 305 | "Each box is a *stage* and the boundaries delimit when Presto has to exchange data between nodes." 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "## Create a Table in MySQL\n", 313 | "\n", 314 | "Let's use the MySQL client to create the table. Then we will switch to Presto to manipulate the data:" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [ 323 | "import MySQLdb\n", 324 | "\n", 325 | "mysql = MySQLdb.connect(host=\"mysql\", user=\"root\", passwd=\"mysql\")\n", 326 | "cur = mysql.cursor()\n", 327 | "cur.execute(\"CREATE DATABASE IF NOT EXISTS presto\")\n", 328 | "cur.fetchall()\n", 329 | "cur.execute(\"\"\"\n", 330 | " CREATE TABLE IF NOT EXISTS presto.events (event LONGTEXT)\n", 331 | " CHARSET utf8mb4 ENGINE=InnoDB\n", 332 | "\"\"\")\n", 333 | "cur.fetchall()\n", 334 | "cur.execute(\"DESC presto.events\")\n", 335 | "for row in cur.fetchall():\n", 336 | " print(\"{table}: [{props}]\".format(\n", 337 | " table=row[0],\n", 338 | " props=', '.join(str(i) for i in row[1:])))" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "## Load Data in MySQL\n", 346 | "\n", 347 | "Let's now load data from [GH Archive](http://www.gharchive.org/) into MySQL and MongoDB.\n", 348 | "Each file from GH Archive contains lines of JSON structs that represent events from the public GitHub timeline, for example repository creation or code push.\n", 349 | "\n", 350 | "Now that the table is create in MySQL, we can insert rows with Presto by using the existing `conn` object created above. You can open http://localhost:8080 to see the execution Presto queries." 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [ 359 | "import gzip\n", 360 | "import io\n", 361 | "import json\n", 362 | "import re\n", 363 | "import requests\n", 364 | "\n", 365 | "# Load events happening between 4-5pm.\n", 366 | "# Feel free to load more hours or more days.\n", 367 | "# We limit the dataset to one hour here to not overload\n", 368 | "# the machine that will run the queries as this tutorial\n", 369 | "# is expected to run on a laptop.\n", 370 | "# It is going to take some time. For the demo, i pre-loaded\n", 371 | "# the data with the mysql client to avoid the overhead of creating\n", 372 | "# Python objects.\n", 373 | "zdata = requests.get(\"https://data.gharchive.org/2015-04-28-16.json.gz\")\n", 374 | "data = gzip.decompress(zdata.content)\n", 375 | "rows = []\n", 376 | "\n", 377 | "# load ``ROW_COUNT`` rows. Feel free to set a greater value if it\n", 378 | "# works well in your environment. Using a small value on purpose\n", 379 | "# to avoid loading data for a long time.\n", 380 | "ROW_COUNT = 1000\n", 381 | "cur = conn.cursor()\n", 382 | "for n, line in enumerate(io.BytesIO(data)):\n", 383 | " row = line.strip().decode('utf8')\n", 384 | " sql = \"INSERT INTO mysql.presto.events (event) VALUES ('{}')\".format(row.replace(\"'\", \"''\"))\n", 385 | " cur.execute(sql)\n", 386 | " cur.fetchall()\n", 387 | " if n == ROW_COUNT - 1:\n", 388 | " break" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 32, 394 | "metadata": {}, 395 | "outputs": [ 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "[['\"CreateEvent\"']]" 400 | ] 401 | }, 402 | "execution_count": 32, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "cur = conn.cursor()\n", 409 | "cur.execute(\"SELECT json_extract(json_parse(event), '$.type') FROM mysql.presto.events TABLESAMPLE BERNOULLI (1) LIMIT 1\")\n", 410 | "cur.fetchall()" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 3, 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "name": "stdout", 420 | "output_type": "stream", 421 | "text": [ 422 | "[['PushEvent', 'pizzadealer/data', 25], ['PushEvent', 'sigma-1/kernel_samsung_lt03wifi', 5], ['PushEvent', 'wjcquking/fuzzyJoin', 3], ['PushEvent', 'EU-OSHA/napo', 3], ['PushEvent', 'Luiqes/My-Penguin', 2], ['PushEvent', 'Creadous/Creadous.github.io', 2], ['PushEvent', 'ashking/phnest', 2], ['PushEvent', 'baojianzhou/PyHW', 2], ['PushEvent', 'TenSheep/click-to-buy', 2], ['PushEvent', 'adrianvinuelas/X-Nav-OAuth-GitHub-Fichero', 2]]\n" 423 | ] 424 | }, 425 | { 426 | "data": { 427 | "text/html": [ 428 | "
\n", 429 | "\n", 442 | "\n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | "
012
0PushEventpizzadealer/data25
1PushEventsigma-1/kernel_samsung_lt03wifi5
2PushEventwjcquking/fuzzyJoin3
3PushEventEU-OSHA/napo3
4PushEventLuiqes/My-Penguin2
5PushEventCreadous/Creadous.github.io2
6PushEventashking/phnest2
7PushEventbaojianzhou/PyHW2
8PushEventTenSheep/click-to-buy2
9PushEventadrianvinuelas/X-Nav-OAuth-GitHub-Fichero2
\n", 514 | "
" 515 | ], 516 | "text/plain": [ 517 | " 0 1 2\n", 518 | "0 PushEvent pizzadealer/data 25\n", 519 | "1 PushEvent sigma-1/kernel_samsung_lt03wifi 5\n", 520 | "2 PushEvent wjcquking/fuzzyJoin 3\n", 521 | "3 PushEvent EU-OSHA/napo 3\n", 522 | "4 PushEvent Luiqes/My-Penguin 2\n", 523 | "5 PushEvent Creadous/Creadous.github.io 2\n", 524 | "6 PushEvent ashking/phnest 2\n", 525 | "7 PushEvent baojianzhou/PyHW 2\n", 526 | "8 PushEvent TenSheep/click-to-buy 2\n", 527 | "9 PushEvent adrianvinuelas/X-Nav-OAuth-GitHub-Fichero 2" 528 | ] 529 | }, 530 | "metadata": {}, 531 | "output_type": "display_data" 532 | } 533 | ], 534 | "source": [ 535 | "cur = conn.cursor()\n", 536 | "cur.execute(\"\"\"\n", 537 | " SELECT ev_type, repo_name, count(*) FROM (\n", 538 | " SELECT\n", 539 | " TRY(json_extract_scalar(ev, '$.repo.name')) as repo_name,\n", 540 | " TRY(json_extract_scalar(ev, '$.type')) as ev_type FROM (\n", 541 | " SELECT try(json_parse(event)) as ev FROM mysql.presto.events))\n", 542 | " WHERE repo_name is not null and ev_type = 'PushEvent'\n", 543 | " GROUP BY ev_type, repo_name\n", 544 | " ORDER BY 3 DESC\n", 545 | " LIMIT 10\n", 546 | "\"\"\")\n", 547 | "rows = cur.fetchall()\n", 548 | "import pandas as pd\n", 549 | "from IPython.display import display\n", 550 | "\n", 551 | "print(rows)\n", 552 | "df = pd.DataFrame(sorted(rows, key=lambda x: x[2], reverse=True))\n", 553 | "display(df)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 4, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "data": { 563 | "text/plain": [ 564 | "[[1001]]" 565 | ] 566 | }, 567 | "execution_count": 4, 568 | "metadata": {}, 569 | "output_type": "execute_result" 570 | } 571 | ], 572 | "source": [ 573 | "cur = conn.cursor()\n", 574 | "cur.execute(\"CREATE TABLE mongodb.events.all AS SELECT * FROM mysql.presto.events\")\n", 575 | "cur.fetchall()" 576 | ] 577 | } 578 | ], 579 | "metadata": { 580 | "kernelspec": { 581 | "display_name": "Python 3", 582 | "language": "python", 583 | "name": "python3" 584 | }, 585 | "language_info": { 586 | "codemirror_mode": { 587 | "name": "ipython", 588 | "version": 3 589 | }, 590 | "file_extension": ".py", 591 | "mimetype": "text/x-python", 592 | "name": "python", 593 | "nbconvert_exporter": "python", 594 | "pygments_lexer": "ipython3", 595 | "version": "3.7.3" 596 | } 597 | }, 598 | "nbformat": 4, 599 | "nbformat_minor": 2 600 | } 601 | --------------------------------------------------------------------------------