|
| 1 | +--- |
| 2 | +title: Apache Trino |
| 3 | +pcx_content_type: example |
| 4 | +--- |
| 5 | + |
| 6 | +import { Steps } from "~/components"; |
| 7 | + |
| 8 | +Below is an example of using [Apache Trino](https://trino.io/) to connect to R2 Data Catalog. For more information on connecting to R2 Data Catalog with Trino, refer to [Trino documentation](https://trino.io/docs/current/connector/iceberg.html). |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Sign up for a [Cloudflare account](https://dash.cloudflare.com/sign-up/workers-and-pages). |
| 13 | +- [Create an R2 bucket](/r2/buckets/create-buckets/) and [enable the data catalog](/r2/data-catalog/manage-catalogs/#enable-r2-data-catalog-on-a-bucket). |
| 14 | +- [Create an R2 API token, key, and secret](/r2/api/tokens/) with both [R2 and data catalog permissions](/r2/api/tokens/#permissions). |
| 15 | +- Install [Docker](https://docs.docker.com/get-docker/) to run the Trino container. |
| 16 | + |
| 17 | +## Setup |
| 18 | +Create a local directory for the catalog configuration and change directories to it |
| 19 | +```bash |
| 20 | +mkdir -p trino-catalog && cd trino-catalog/ |
| 21 | +``` |
| 22 | + |
| 23 | +Create a configuration file called `r2.properties` for your R2 Data Catalog connection: |
| 24 | + |
| 25 | +```properties |
| 26 | +# r2.properties |
| 27 | +connector.name=iceberg |
| 28 | + |
| 29 | +# R2 Configuration |
| 30 | +fs.native-s3.enabled=true |
| 31 | +s3.region=auto |
| 32 | +s3.aws-access-key=<Your R2 access key> |
| 33 | +s3.aws-secret-key=<Your R2 secret> |
| 34 | +s3.endpoint=<Your R2 endpoint> |
| 35 | +s3.path-style-access=true |
| 36 | + |
| 37 | +# R2 Data Catalog Configuration |
| 38 | +iceberg.catalog.type=rest |
| 39 | +iceberg.rest-catalog.uri=<Your R2 Data Catalog URI> |
| 40 | +iceberg.rest-catalog.warehouse=<Your R2 Data Catalog warehouse> |
| 41 | +iceberg.rest-catalog.security=OAUTH2 |
| 42 | +iceberg.rest-catalog.oauth2.token=<Your R2 authentication token> |
| 43 | +``` |
| 44 | + |
| 45 | +## Example usage |
| 46 | + |
| 47 | +<Steps> |
| 48 | +1. Start Trino with the R2 catalog configuration: |
| 49 | + |
| 50 | + ```bash |
| 51 | + # Create a local directory for the catalog configuration |
| 52 | + mkdir -p trino-catalog |
| 53 | + |
| 54 | + # Place your r2.properties file in the catalog directory |
| 55 | + cp r2.properties trino-catalog/ |
| 56 | + |
| 57 | + # Run Trino with the catalog configuration |
| 58 | + docker run -d \ |
| 59 | + --name trino-r2 \ |
| 60 | + -p 8080:8080 \ |
| 61 | + -v $(pwd)/trino-catalog:/etc/trino/catalog \ |
| 62 | + trinodb/trino:latest |
| 63 | + ``` |
| 64 | + |
| 65 | +2. Connect to Trino and query your R2 Data Catalog: |
| 66 | + |
| 67 | + ```bash |
| 68 | + # Connect to the Trino CLI |
| 69 | + docker exec -it trino-r2 trino |
| 70 | + ``` |
| 71 | + |
| 72 | +3. In the Trino CLI, run the following commands: |
| 73 | + |
| 74 | + ```sql |
| 75 | + -- Show all schemas in the R2 catalog |
| 76 | + SHOW SCHEMAS IN r2; |
| 77 | + |
| 78 | + -- Show all schemas in the R2 catalog |
| 79 | + CREATE SCHEMA r2.example_schema |
| 80 | + |
| 81 | + -- Create a table with some values in it |
| 82 | + CREATE TABLE r2.example_schema.yearly_clicks ( |
| 83 | + year, |
| 84 | + clicks |
| 85 | + ) |
| 86 | + WITH ( |
| 87 | + partitioning = ARRAY['year'] |
| 88 | + ) |
| 89 | + AS VALUES |
| 90 | + (2021, 10000), |
| 91 | + (2022, 20000); |
| 92 | + |
| 93 | + -- Show tables in a specific schema |
| 94 | + SHOW TABLES IN r2.example_schema; |
| 95 | + |
| 96 | + -- Query your Iceberg table |
| 97 | + SELECT * FROM r2.example_schema.yearly_clicks; |
| 98 | + ``` |
| 99 | +</Steps> |
0 commit comments