Treasure Data API Library for Python
The `td-client` Python library provides a wrapper for the Treasure Data API (REST API), allowing users to programmatically interact with Treasure Data services. It enables tasks such as managing databases and tables, issuing queries (including asynchronous execution), and performing bulk data imports. The library is currently at version 1.7.0 and is actively maintained with consistent updates.
Warnings
- gotcha Confusing `td-client` with other Python libraries or tools named 'TDClient'. Be aware that 'td-client' (this library) is specifically for the Treasure Data REST API. Other packages like `pytd` offer different functionalities (e.g., pandas integration, direct Trino/Presto access), and 'TDClient' may also refer to a non-Python Student Aid Internet Gateway (SAIG) client.
- gotcha Incorrect API key or endpoint configuration can lead to authentication or connection errors. The client expects the API key to be passed via the `apikey` argument or read from the `TD_API_KEY` environment variable. The endpoint can be configured via the `endpoint` argument or `TD_API_SERVER` environment variable.
- gotcha When importing CSV or TSV data, proper parsing of column types is crucial. The `upload_file` and `create_bulk_import` methods accept `dtypes` and `converters` parameters to specify how individual columns should be interpreted. The default behavior is 'guess', which might not always be accurate.
- gotcha SSL certificate verification is important for secure communication but can sometimes cause issues in certain environments. The library documentation recommends installing `certifi` for more robust SSL verification.
Install
-
pip install td-client -
pip install certifi
Imports
- Client
from tdclient import Client
- tdclient
import tdclient
Quickstart
import os
import tdclient
# Ensure TD_API_KEY environment variable is set with your Treasure Data API key.
# Optionally, set TD_API_SERVER to override the default endpoint (https://api.treasuredata.com).
# Example: export TD_API_KEY="YOUR_TREASURE_DATA_API_KEY"
# Example: export TD_API_SERVER="https://api.us01.treasuredata.com"
api_key = os.environ.get("TD_API_KEY", "")
api_server = os.environ.get("TD_API_SERVER", "https://api.treasuredata.com")
if not api_key:
raise ValueError("TD_API_KEY environment variable not set. Please set it to your Treasure Data API key.")
try:
with tdclient.Client(apikey=api_key, endpoint=api_server) as client:
print("Listing recent jobs:")
# Retrieve and print details of the 20 most recent jobs
for job in client.jobs():
print(f" Job ID: {job.job_id}, Type: {job.type}, Status: {job.status}, Created At: {job.created_at}")
except Exception as e:
print(f"An error occurred: {e}")