LoginSignup
11
9

More than 5 years have passed since last update.

TreasureDataに既存データ(TSV形式)をバルクインポートしてみた

Posted at

背景

既にあるデータをTreasureDataにアップロードしたいなと思い、Treasuredataが提供しているCLIツール(TD-CLI)を使ってアップロードしてみたのでメモとして残しておく。

以下前提条件。

  • TD-CLIがインストールされていること。

TD-CLIのインストール方法や基本的な使い方はTreasureData使ってみたを参照のこと。

以下環境情報。

  • ubuntu: 12.04
  • TD-CLI: 0.11.3

データ準備

まずはアップロードしたいデータを準備する。
今回はApacheログ形式である以下のデータを準備した。

/tmp/www_access.tsv
$ cat /tmp/www_access.tsv | head -2
host    path    method  referer code    agent   time
48.45.67.130    /category/computers GET -   200 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1    2013-05-14 16:53:22
$ cat /tmp/www_access.tsv | wc -l
1001
$

DB/Table作成

データの投げ先であるTreasureData上にDBTableを作成しておく。

td_create
$ td account -f # 念のためログイン
Enter your Treasure Data credentials.
Email: td@treasuredata.com
Password (typing will be hidden): 
Authenticated successfully.
Use 'td db:create <db_name>' to create a database.
$
$ td db:create apache_sample
Database 'apache_sample' is created.
Use 'td table:create apache_sample <table_name>' to create a table.
$ td table:create apache_sample access
Table 'apache_sample.access' is created.
$

ここではapache_sampleというDBaccessというTableを作成している。

データアップロード

さてこれでデータアップロードする準備ができたので以下のコマンドでアップロードする。

Options

td_import
$ td import:auto --format tsv --auto-create apache_sample.access --parallel 8 --prepare-parallel 8 --column-header -o /ext4/td_out/ --time-column 'time' --time-format "%Y-%m-%d %H:%M:%S" /tmp/www_access.tsv 

上記のオプションのざっとした説明は以下。

Option 説明
import:atuo TD-CLIのアップロードにはいくつかのステップがありautoオプションを指定することでその全てのステップを自動的に実行してくれる。
--fromt tsv アップロードデータがTSV形式であることを意味する。CSV形式の場合はここをcsvとする。
--auto-create なんだっけ・・・忘れたorz
apache_sample.access データアップロード先
--parallel 8 データアプロードの並行スレッド数
--prepare-parallel 8 データをMessagePack形式に加工する際の並行スレッド数
--column-header TreasureData上でのカラム名をアップロード対象ファイルの一行目の値を利用するように指定する。
-o /ext4/td_out MessagePack形式ファイルの出力先Directory
--time-column 'time' 時刻カラムの指定
--time-format "%Y-%m-%d %H:%M:%S" 指定した時刻カラムのフォーマット
/tmp/www_access.tsv アップロード対象ファイル

TreasureDataの中で時刻というのは重要なため--time-columnは必須であるため注意すること。
--parallelを指定することでデータアップロードの並行数が増えるのでアップロード時間がある程度短縮される。

実行ログ

コマンドを実行すると以下のような実行ログが出力される。

td_import_log
Uploading prepared sources
  Session    : apache_sample_access_2014_09_10_1410349386586
  Source     : /tmp/www_access.tsv (193315 bytes)

Converting '/tmp/www_access.tsv'...
sample row: {"time":1368518002,"host":"48.45.67.130","path":"\/category\/computers","method":"GET","code":200,"referer":"-","agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko\/20100101 Firefox\/10.0.1"}
Uploading /ext4/td_out/www_access_tsv_0.msgpack.gz (19489 bytes)...
....省略
Prepare status:
  Elapsed time: 9 sec.
  Source     : /tmp/www_access.tsv
    Status          : SUCCESS
    Read lines      : 1001
    Valid rows      : 1000
    Invalid rows    : 0
    Converted Files : /ext4/td_out/www_access_tsv_0.msgpack.gz (19489 bytes)


Next steps:
  => execute following 'td import:upload' command. if the bulk import session is not created yet, please create it with 'td import:create <session> <database> <table>' command.
     $ td import:upload <session> '/ext4/td_out/www_access_tsv_0.msgpack.gz'


Upload status:
  Elapsed time: 18 sec.
  Source  : /ext4/td_out/www_access_tsv_0.msgpack.gz
    Status          : SUCCESS
    Part name       : www_access_tsv_0_msgpack_gz
    Size            : 19489
    Retry count     : 0


Next Steps:
  => execute 'td import:perform apache_sample_access_2014_09_10_1410349386586'.

Freeze bulk import session apache_sample_access_2014_09_10_1410349386586
Perform bulk import session apache_sample_access_2014_09_10_1410349386586
Show status of bulk import session apache_sample_access_2014_09_10_1410349386586
  Performing job ID : 40448
  Name              : apache_sample_access_2014_09_10_1410349386586
  Status            : performing

Wait apache_sample_access_2014_09_10_1410349386586 bulk import session performing...
Show error records of bulk import session apache_sample_access_2014_09_10_1410349386586
  Performing job ID : 40448
  Valid parts       : 1
  Error parts       : 0
  Valid records     : 1000
  Error records     : 0

いくつかのStepを踏んで処理をしていることがわかる。

確認してみる

以下のコマンドでデータが正常に入っているか確認する。

$ td table:show apache_sample access
Name        : apache_sample.access
Type        : log
Count       : 1000
Schema      : (
    host:string
    path:string
    method:string
    referer:string
    code:long
    agent:string
)
$
$ td query -w -t hive -d apache_sample "SELECT * FROM access LIMIT 10"
...省略
+-----------------+------------------------+--------+--------------------------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| host            | path                   | method | referer                  | code | agent                                                                                                                                                                     | v                                                                                                                                                                                                                                                                                                   | time       |
+-----------------+------------------------+--------+--------------------------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------+
| 48.45.67.130    | /category/computers    | GET    | -                        | 200  | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1                                                                                              | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1","referer":"-","path":"/category/computers","host":"48.45.67.130"}                                                                                           | 1368518002 |
| 124.192.166.98  | /search/?c=Giftcards   | POST   | /category/sports         | 200  | Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)                                                                                                    | {"code":"200","time":"1368518002","method":"POST","agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)","referer":"/category/sports","path":"/search/?c=Giftcards","host":"124.192.166.98"}                                                                              | 1368518002 |
| 176.183.137.115 | /category/finance      | GET    | -                        | 200  | Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)                                                                                                  | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)","referer":"-","path":"/category/finance","host":"176.183.137.115"}                                                                                              | 1368518002 |
| 192.183.92.55   | /item/software/4777    | GET    | -                        | 200  | Mozilla/5.0 (iPad; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3                                           | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (iPad; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3","referer":"-","path":"/item/software/4777","host":"192.183.92.55"}                                       | 1368518002 |
| 48.66.40.214    | /category/networking   | GET    | -                        | 200  | Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1                                                                                                     | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1","referer":"-","path":"/category/networking","host":"48.66.40.214"}                                                                                                 | 1368518002 |
| 144.132.188.212 | /item/electronics/2649 | GET    | /item/electronics/1996   | 200  | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1                                                                                       | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1","referer":"/item/electronics/1996","path":"/item/electronics/2649","host":"144.132.188.212"}                                                         | 1368518002 |
| 36.171.84.59    | /category/finance      | GET    | -                        | 200  | Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11                                                               | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11","referer":"-","path":"/category/finance","host":"36.171.84.59"}                                                              | 1368518002 |
| 204.204.116.114 | /item/toys/3282        | GET    | -                        | 200  | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1                                                                                              | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1","referer":"-","path":"/item/toys/3282","host":"204.204.116.114"}                                                                                            | 1368518002 |
| 100.27.141.84   | /item/software/2012    | GET    | /category/cameras?from=0 | 200  | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1                                                                                              | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1","referer":"/category/cameras?from=0","path":"/item/software/2012","host":"100.27.141.84"}                                                                   | 1368518002 |
| 96.195.97.82    | /category/garden       | GET    | -                        | 200  | Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; YTB730; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C) | {"code":"200","time":"1368518002","method":"GET","agent":"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; YTB730; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)","referer":"-","path":"/category/garden","host":"96.195.97.82"} | 1368518002 |
+-----------------+------------------------+--------+--------------------------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------+
10 rows in set
$

timeカラムにUnix時間で値が格納されていることがわかる。
正常っぽい。

TSV以外の形式のインポートについてはTreasureDataDoccumentサイトを参照のこと。

MySQLからはJDBC経由で直接データを入れられるらしい。

おしまい。

11
9
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
11
9