Treasure Data API library for Python
Project description
Treasure Data API library for Python
Requirements
td-client supports the following versions of Python.
Python 3.5+
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.
The td-client documentation is hosted at https://tdclient.readthedocs.io/, or you can go directly to the API documentation.
For information on the parameters that may be used when reading particular types of data, see File import parameters.
Listing jobs
Treasure Data API key will be read from environment variable TD_API_KEY, if none is given via apikey= argument passed to tdclient.Client.
Treasure Data API endpoint https://api.treasuredata.com is used by default. You can override this with environment variable TD_API_SERVER, which in turn can be overridden via endpoint= argument passed to tdclient.Client. List of available Treasure Data sites and corresponding API endpoints can be found here.
import tdclient
with tdclient.Client() as td:
for job in td.jobs():
print(job.job_id)
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")
job.wait()
for row in job.result():
print(repr(row))
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):
print(cursor.job_status())
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)
print(repr(data))
We offer another package for pandas named pytd 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.
import sys
import tdclient
import uuid
import warnings
if len(sys.argv) <= 1:
sys.exit(0)
with tdclient.Client() as td:
session_name = "session-{}".format(uuid.uuid1())
bulk_import = td.create_bulk_import(session_name, "mydb", "mytbl")
try:
for file_name in sys.argv[1:]:
part_name = "part-{}".format(file_name)
bulk_import.upload_file(part_name, "json", file_name)
bulk_import.freeze()
except:
bulk_import.delete()
raise
bulk_import.perform(wait=True)
if 0 < bulk_import.error_records:
warnings.warn("detected {} error records.".format(bulk_import.error_records))
if 0 < bulk_import.valid_records:
print("imported {} records.".format(bulk_import.valid_records))
else:
raise(RuntimeError("no records have been imported: {}".format(bulk_import.name)))
bulk_import.commit(wait=True)
bulk_import.delete()
If you want to import data as msgpack format, you can write as follows:
import io
import time
import uuid
import warnings
import tdclient
t1 = int(time.time())
l1 = [{"a": 1, "b": 2, "time": t1}, {"a": 3, "b": 9, "time": t1}]
with tdclient.Client() as td:
session_name = "session-{}".format(uuid.uuid1())
bulk_import = td.create_bulk_import(session_name, "mydb", "mytbl")
try:
_bytes = tdclient.util.create_msgpack(l1)
bulk_import.upload_file("part", "msgpack", io.BytesIO(_bytes))
bulk_import.freeze()
except:
bulk_import.delete()
raise
bulk_import.perform(wait=True)
# same as the above example
Changing how CSV and TSV columns are read
The td-client package will generally make sensible choices on how to read the columns in CSV and TSV data, but sometimes the user needs to override the default mechanism. This can be done using the optional file import parameters dtypes and converters.
For instance, consider CSV data that starts with the following records:
time,col1,col2,col3 1575454204,a,0001,a;b;c 1575454204,b,0002,d;e;f
If that data is read using the defaults, it will produce values that look like:
1575454204, "a", 1, "a;b;c"
1575454204, "b", 2, "d;e;f"
that is, an integer, a string, an integer and another string.
If the user wants to keep the leading zeroes in col2, then they can specify the column datatype as string. For instance, using bulk_import.upload_file to read data from input_data:
bulk_import.upload_file(
"part", "msgpack", input_data,
dtypes={"col2": "str"},
)
which would produce:
1575454204, "a", "0001", "a;b;c"
1575454204, "b", "0002", "d;e;f"
If they also wanted to treat col3 as a sequence of strings, separated by semicolons, then they could specify a function to process col3:
bulk_import.upload_file(
"part", "msgpack", input_data,
dtypes={"col2": "str"},
converters={"col3", lambda x: x.split(";")},
)
which would produce:
1575454204, "a", "0001", ["a", "b", "c"]
1575454204, "b", "0002", ["d", "e", "f"]
Development
Running tests
Run tests.
$ python setup.py 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. Ensure you installed twine.
$ python setup.py bdist_wheel sdist
$ twine upload dist/*
License
Apache Software License, Version 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file td-client-1.2.1.tar.gz
.
File metadata
- Download URL: td-client-1.2.1.tar.gz
- Upload date:
- Size: 60.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73da65036030c701baedbab9920e929c90fa5e9622ebe46ea1b12f7369b564a8 |
|
MD5 | c26d5703bfac9d307d6117620142f81e |
|
BLAKE2b-256 | 425c2596a23fc20f15959685f05cad5f4f9bf7002a8e328aa7b1faf8544f7dc6 |
File details
Details for the file td_client-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: td_client-1.2.1-py3-none-any.whl
- Upload date:
- Size: 86.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac4fff83b50877c269fd796693535c20ca15adcf2fda2262148e2ee04dbe7239 |
|
MD5 | 764b256e08c7a697b90661e71d2d953e |
|
BLAKE2b-256 | 8cdabd6b5571b7da34c29d3f14478af37fd2a7241e7b13114a0bba06b93b75dc |