Skip to main content

Treasure Data API library for Python

Project description

# Treasure Data API library for Python

[![Build Status](](
[![Build status](](
[![Coverage Status](](
[![Code Health](](
[![PyPI version](](

Treasure Data API library for Python

## Requirements

`td-client` supports the following versions of Python.

* Python 2.7+
* Python 3.3+
* PyPy

## Install

You can install the releases from [PyPI](

$ pip install td-client

It'd be better to install [certifi]( to enable SSL certificate verification.

$ pip install certifi

## Examples

Please see also the examples at [Treasure Data Documentation](

### Listing jobs

TreasureData API key will be read from environment variable `TD_API_KEY`, if none is given via arguments to `tdclient.Client`.

import tdclient

with tdclient.Client() as td:
for job in

### Running jobs

Running jobs on Treasure Data.

import tdclient

with tdclient.Client() as td:
job = td.query("sample_datasets", "SELECT COUNT(1) FROM www_access", type="hive")
for row in job.result():

### Running jobs via DBAPI2

td-client-python implements [PEP 0249]( Python Database API v2.0.
You can use td-client-python with external libraries which supports Database API such like [pandas](

import pandas
import tdclient

def on_waiting(cursor):

with tdclient.connect(db="sample_datasets", type="presto", wait_callback=on_waiting) as td:
data = pandas.read_sql("SELECT symbol, COUNT(1) AS c FROM nasdaq GROUP BY symbol", td)

We offer another package for pandas named [pandas-td]( with some advanced features.
You may prefer it if you need to do complicated things, such like exporting result data to Treasure Data, printing job's
progress during long execution, etc.

### Importing data

Importing data into Treasure Data in streaming manner, as similar as [fluentd]( is doing.

import sys
import tdclient

with tdclient.Client() as td:
for file_name in sys.argv[:1]:
td.import_file("mydb", "mytbl", "csv", file_name)

### Bulk import

Importing data into Treasure Data in batch manner.

from __future__ import print_function
import sys
import tdclient
import time
import warnings

if len(sys.argv) <= 1:

with tdclient.Client() as td:
session_name = "session-%d" % (int(time.time()),)
bulk_import = td.create_bulk_import(session_name, "mydb", "mytbl")
for file_name in sys.argv[1:]:
part_name = "part-%s" % (file_name,)
bulk_import.upload_file(part_name, "json", file_name)
if 0 < bulk_import.error_records:
warnings.warn("detected %d error records." % (bulk_import.error_records,))
if 0 < bulk_import.valid_records:
print("imported %d records." % (bulk_import.valid_records,))
raise(RuntimeError("no records have been imported: %s" % (repr(,)))

## Development

### Running tests

Run tests.

$ python test

### Running tests (tox)

You can run tests against all supported Python versions. I'd recommend you to install [pyenv]( to manage Pythons.

$ pyenv shell system
$ for version in $(cat .python-version); do [ -d "$(pyenv root)/versions/${version}" ] || pyenv install "${version}"; done
$ pyenv shell --unset

Install [tox](

$ pip install tox

Then, run `tox`.

$ tox

### Release

Release to PyPI.

$ python bdist_wheel --universal sdist upload

## Version History

See [](

## License

Apache Software License, Version 2.0

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
td_client-0.12.0-py2.py3-none-any.whl (73.9 kB) Copy SHA256 hash SHA256 Wheel 3.6
td-client-0.12.0.tar.gz (49.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page