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