import tiledb
import tiledb.cloud
import warnings
"ignore")
warnings.filterwarnings(import tiledb.sql
import pandas as pd
import os.path
# You should set the appropriate environment variables with
# your TileDB Cloud token and username.
= os.environ["TILEDB_REST_TOKEN"]
token = os.environ["TILEDB_REST_USERNAME"]
username
# Get the bucket from an environment variable
= os.environ["S3_BUCKET"]
s3_bucket
# Login to TileDB Cloud
=token)
tiledb.cloud.login(token
# Set URIs
= "my_table"
table_name = "tiledb://" + username + "/" + table_name
table_uri = "tiledb://" + username + "/" + s3_bucket + "/" + table_name
table_reg_uri = (
csv_uri "s3://tiledb-inc-demo-data/examples/notebooks/nyc_yellow_tripdata/taxi_first_10.csv"
)
# Define config values and context
= {"vfs.s3.no_sign_request": "true", "vfs.s3.region": "us-east-1"}
cfg = tiledb.cloud.Ctx(cfg)
ctx
# Clean up past data
if tiledb.array_exists(table_uri):
tiledb.Array.delete_array(table_uri)
Running Locally
This tutorial explains how to install TileDB and its SQL package (via MariaDB), as well as how to use it on TileDB Cloud.
Installation
You need to install the following libraries and programs:
- TileDB-MariaDB, which offers a SQL API to TileDB.
- TileDB-Py, the Python wrapper of TileDB Embedded.
- NumPy, to handle data with Python.
- pandas, to create and manipulate DataFrames.
- Apache Arrow, to boost performance via zero-copy to pandas.
Conda and mamba are the preferred mechanisms for installing TileDB-VCF.
# enter the following two lines if you are on a M1 Mac
CONDA_SUBDIR=os
conda config --env --set subdir osx-64
# create the conda environment
conda create -n tiledb-tables "python<3.10"
conda activate tiledb-tables
# mamba is a faster and more reliable alternative to conda
conda install -c conda-forge mamba
# Install the required libraries
mamba install -y -c conda-forge -c tiledb tiledb-py libtiledb-sql-py pandas pyarrow numpy
# Install the TileDB Cloud Python client
pip install tiledb-cloud
Local storage
Using TileDB’s tabular offering on local storage is similar to the Quickstart tutorial, as well as the other various tutorials.
Storage on S3
Using TileDB’s tabular offering to interact with Amazon S3 from your local storage is similar to the Tutorials: Basic S3 Example tutorial.
Storage on TileDB Cloud
Take note of the following when interacting with TileDB Cloud outside of the notebook environments in the TileDB Cloud service:
- You need to create a REST API token from the TileDB Cloud console.
- You need to set the REST API token in an environment variable and login using the token value.
- When creating a new table, you need to use a URI in the form
tiledb://<your_username>/<S3_path>/<table_name>
, whereS3_path
is the location on S3 where you wish to physically store the table. - When referring to the table after creating it (for example, when submitting queries), use a URI in the form
tiledb://<your_username>/<table_name>
(that is, no need to specify the S3 physical path anymore).
Start by importing the necessary libraries, loading the appropriate token and username, logging in, setting up the URIs and cleaning up any already-created table with the same name.
Ingest some data, using the URI that incorporates the S3 physical path as explained earlier:
Run a SQL query to see that it works, but now you can use the TileDB URI that doesn’t incorporate the S3 physical path.
Clean up.