LoginSignup
2
3

More than 5 years have passed since last update.

Python de TreasureData vol.1

Last updated at Posted at 2017-03-29

Python(Jupyter)からTreasureDataに接続する方法

いつもQiitaで調べお世話になっているので、少しずつ自分で調べたことは書いていきます。
TreasureDataの快適さに現を抜かしつつ。

0.事前準備

エースパイソニスたさんが作ってくれたTDclientを入れます
https://github.com/treasure-data/td-client-python
terminal
$ pip install td-client

あとTreasureDataとpandasを繋ぐpythonライブラリpandas-tdも入れておきます。
https://github.com/treasure-data/pandas-td
terminal
$ pip install pandas-td

1.TreasureDataのAPIキーの取得

コンソールから[users]→[API Keys]と進みパスワードを入力して[view]。
 APIキーを確認します。

スクリーンショット 2017-03-29 23.33.04.png

2.ソースに書くのはあれなので環境変数にAPIキーを設定します。(別にソースに書いてもいいけど)

1.Windows

[コンピューターの詳細設定]→[環境変数の設定]→[ユーザー環境変数]→[新規]追加→
変数名:TD_API_KEY
変数値:(取得したAPIキー)

2.OSX

Terminalで以下実行
terminal
$ export TD_API_KEY=(取得したAPIキー)

3.コード

TD.py
import os
import pandas as pd
import pandas_td as td

#コネクションの作成
con = td.connect(apikey=os.environ["TD_API_KEY"], endpoint='https://api.treasuredata.com/')

#Presto用エンジンの作成クエリ
engine = con.query_engine(database='(Database名)', type='presto')

# tdのキーであるtimeをindexにしてdfに入れます。試しに3行
df = td.read_td_table('(テーブル名)', engine , limit=3, index_col='time', parse_dates={'time: 's'})

取得できたでしょうか。データ載せられなくてすいません。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3