ColaboratoryでTreasure Data にクエリを投げる


Colaboratoryというhosted Jupyter + Google Driveなど連携などがされた便利なサービスがfree to useで提供されています。

Is it free to use?
Yes. Colaboratory is a research project that is free to use.
https://research.google.com/colaboratory/faq.html

なので、下記の手順でTreasure Dataにクエリを投げれます。

Colaboratoryでは任意のライブラリをインストールするには、!pip installを利用します。
つまり、

!pip install td-client

で、https://github.com/treasure-data/td-client-python のインストールが可能です。

あとは、下記のようにしてクエリが投げれますね。簡単。

import tdclient

with tdclient.Client(apikey='<YOUR APIKEY>') as td:
    job = td.query("sample_datasets", "SELECT COUNT(1) FROM www_access", type="presto")
    job.wait()
    for row in job.result():
        print(repr(row))