実データで覚える Treasure Client コマンドラインリファンス 〜2.Data Management〜
本シリーズではTreasure Client ツールのコマンドラインリファレンスを以下の5つのレイヤーに分解し,各々について解説していくものとします。
No. | レイヤー | コマンド |
---|---|---|
1. | Data Import |
one-time import bulk import |
2. | Data Management |
db table |
3. | Data Processing |
schema query job |
4. | Data Scheduling |
sched result |
5. | Other |
help status server sample |
本日は 2. Data Management を紹介します。本シリーズでは実際にデータを使用して,その分析の流れに沿いながらコマンドラインを紹介していきます。実際に手を動かすためには,前回(1. Data Import) を参考にデータのインポートをお願いします。
1. db コマンド
db コマンドは現在存在する database の確認および追加削除ができる一般的なコマンドです。
$ td help db
Additional commands, type "td help COMMAND" for more details:
db:list # Show list of tables in a database
db:show <db> # Describe information of a database
db:create <db> # Create a database
db:delete <db> # Delete a database
前回は db名: "carsensor" を作りましたので,その中にあるテーブルを見てみましょう。
$ td db:list
+------------------------------+------------+
| Name | Count |
+------------------------------+------------+
| book_crossing_dataset | 1700014 |
| carsensor | 395927 |
+------------------------------+------------+
$ td db:show carsensor
+-------------------+------+--------+--------------------------------------------------------------------------+
| Table | Type | Count | Schema |
+-------------------+------+--------+--------------------------------------------------------------------------+
| body_master | log | 10 | name:string, code:string |
| brand_master | log | 149 | |
| catalog | log | 84633 | |
| color_master | log | 16 | name:string, code:string |
| country_master | log | 20 | name:string, code:string |
| large_area_master | log | 9 | name:string, code:string |
| pref_master | log | 47 | name:string, large_area_name:string, code:string, large_area_code:string |
| usedcar | log | 311043 | |
+-------------------+------+--------+--------------------------------------------------------------------------+
Schema はテーブル内のデータカラムの型を指定しているものです。Schema の話は次回で紹介します。
2. table コマンド
table コマンドは特定の db に存在するテーブルを確認,および操作するものです。
$ td help table td table help
Additional commands, type "td help COMMAND" for more details:
table:list [db] # Show list of tables
table:show <db> <table> # Describe information of a table
table:create <db> <table> # Create a table
table:delete <db> <table> # Delete a table
table:import <db> <table> <files...> # Parse and import files to a table
table:export <db> <table> # Dump logs in a table to the specified storage
table:swap <db> <table1> <table2> # Swap names of two tables
table:tail <db> <table> # Get recently imported logs
table:partial_delete <db> <table> # Delete logs from the table within the specified time range
table:expire <db> <table> <expire_days> # Expire data in table after specified number of days
ここでは以下のコマンドは別セクションに譲るものとします:
- table:import → Data Import
- table:export → Data Processing
他の代表的なコマンドを見ていきます:
$ td table:list carsensor
+-----------+-------------------+------+---------+---------+---------------------------+---------------------------+
| Database | Table | Type | Count | Size | Last import | Last log timestamp |
+-----------+-------------------+------+---------+---------+---------------------------+---------------------------+
| carsensor | body_master | log | 10 | 0.0 GB | 2014-01-22 17:41:17 +0900 | 2014-01-22 17:39:11 +0900 |
| carsensor | brand_master | log | 149 | 0.0 GB | | |
| carsensor | catalog | log | 84,633 | 0.01 GB | | |
| carsensor | color_master | log | 16 | 0.0 GB | 2014-01-22 17:40:35 +0900 | 2014-01-22 17:39:11 +0900 |
| carsensor | country_master | log | 20 | 0.0 GB | 2014-01-22 17:42:57 +0900 | 2014-01-22 17:39:11 +0900 |
| carsensor | large_area_master | log | 9 | 0.0 GB | 2014-01-22 17:44:24 +0900 | 2014-01-22 17:39:11 +0900 |
| carsensor | pref_master | log | 47 | 0.0 GB | 2014-01-22 17:47:16 +0900 | 2014-01-22 17:39:11 +0900 |
| carsensor | usedcar | log | 311,043 | 0.1 GB | | |
+-----------+-------------------+------+---------+---------+---------------------------+---------------------------+
8 rows in set
$ td table:show carsensor pref_master
Name : carsensor.pref_master
Type : log
Count : 47
Schema : (
name:string
large_area_name:string
code:string
large_area_code:string
)
table コマンドで頻繁に使用するのはそのテーブルに入っているデータの一部「眺める」tail コマンドです。
$ td table:tail carsensor usedcar -n 1 -P
{
"publication_date": 1390356579,
"time": 1390356579,
"shop_datum": "world",
"recycle": "リ済別",
"engine": "ガソリン",
"warranty": "保証付",
"code": "NI",
"maintenance": "法定整備付",
"body_code": "D",
"name": "日産",
"warranty_length_kikan": "1年",
"warranty_distance": "無制限",
"body_name": "ハッチバック",
"shop_pref_code": 13,
"year": 2004,
"warranty_length": "保証期間:1年",
"inspection": "車検整備付",
"shop_pref_name": "東京都",
"odd_km": 27000,
"odd": "2.7万km",
"price": 299000,
"color": "ビーンズ",
"model": "マーチ",
"shop_lat": "35.684586",
"shop_lng": "139.303458",
"id": "CU2261202498",
"grade": "1.2 12c Vセレクション",
"url_mobile": "http://mob.carsensor.net/c/ld.cgi?EK=59&BK=CU2261202498&vos=csr145",
"url_pc": "http://www.carsensor.net/usedcar/detail.php?STID=CS210840&BKKN=CU2261202498&vos=ncsrapis00001",
"url_qr": "http://webservice.recruit.co.jp/common/qr?url=http%3A%2F%2Fmob.carsensor.net%2Fc%2Fld.cgi%3FEK%3D59%26BK%3DCU2261202498%26vos%3Dcsr146",
"main_photo_s": "http://www.carsensor.net/CSphoto/bkkn/220/195/UD0011220195/UD0011220195_001S.JPG",
"main_photo_l": "http://www.carsensor.net/CSphoto/bkkn/220/195/UD0011220195/UD0011220195_001L.JPG",
"shop_name": "日産プリンス西東京販売 レッドステーション八王子楢原店",
"maintenance_comment": "車検整備一式 エンジンオイル&オイルフィルター交換・ワイパーゴム交換",
"warranty_comment": "ワイド保証 保証期間:1年(走行距離無制限)",
"main_caption": "◆◇ご覧頂き有難う御座います。お車の『内容・状態・お支払・ご納車までの流れ』のご質問&不明点等、御座いましたら◆◇お問合せ先 042-625-2800◇◆にお気軽にご連絡下さい。◇◆",
"desc": "人気のコンパクトハッチ かわいいカラーのビーンズです走行距離が魅力なお買い得なお車です"
}
注:-P は pretty print. -n は取得数です。このコマンドで見れるデータは(timeカラムの情報で)直近のデータを tailing してくるので,データのインポートが直近でない場合は何も返ってこない場合が有ります。その場合は --to または --from でスタートおよびエンドのタイムスタンプまたは"yyyy-mm-dd hh:mm:ss"形式を指定して下さい。
一般には直前の時間情報を取れないほうがほとんどですので,
- table:import したテーブルは $td table:list で last log timestamp 項を確認する。
- bulk_import の場合は
$td query -w -d carsensor "SELECT time FROM usedcar LIMIT 10"
を実行する。
最後に,tail コマンドはシェルスクリプトを使用してクエリを叩くことなく簡易集計が可能です。例えば先頭1万件の usedcar テーブルの price 平均を取る場合,
$ td table:tail carsensor usedcar -n 100000 -P | grep "price" | cut -d: -f2 | cut -d, -f1 | awk '{x+=$1;n+=1;}END{print x/n}'
を実行します。他にも色々試して下さい。
今回はライトな内容でしたね。終わり。