LoginSignup
1
2

More than 5 years have passed since last update.

Treasure Data上特定のレコードを削除する

Last updated at Posted at 2017-06-30

timeを活用して特定のレコードを削除する

Treasure Data上のテーブルに格納されているレコードを削除する

  • 期間を指定して削除コマンドを実行(partial_delete)
    • CLI

partial_deleteでデータの作成日(time)の範囲を指定してレコードが削除できます。でもtime以外のカラム例idなどを指定してレコードを削除したいです。

テーブルのレコード

time id name age
1498727491 1 jane 30
1498727491 2 lucy 20
1498727491 3 boby 25
1498727491 4 stepin 26
1498727491 5 lily 28
  • 1498727491はデータの作成日:2017-06-29 18:11:31

やりたいこと

id=4のレコードを削除したいです。

やる方法

partial_deleteでtimeを元に削除するため、timeを活用したら実現できます。
上記のレコード再作成。idをintのままtimeに変換してtimeとして保存する。

time id name age
1 1 jane 30
2 2 lucy 20
3 3 boby 25
4 4 stepin 26
5 5 lily 28

コマンド実行

$ td table:partial_delete example_db original_table  --from 4 --to 5
Time for the -f / --from and -t / --to options must either be a multiple of 3600 (1 hour)
  or be expressed in Ruby time string format where the minutes and seconds are 0

エラーになって、timeのfrom、toは3600 (1 hour)の倍数にしないといけないです。
なるほど、上記のレコードを再作成time=id * 3600

time id name age
3600 1 jane 30
7200 2 lucy 20
10800 3 boby 25
14400 4 stepin 26
18000 5 lily 28

コマンドを再実行

$ td table:partial_delete example_db original_table --from 14400 --to 18000
Partial delete job 17227446 is queued.
Use 'td job:show 17227446' to show the status.

コンソール画面で確認したら、

Duration 00:00:02
SQL Query

DELETE FROM original_table WHERE 14400 <= time AND time < 18000

一瞬で終わり。

結果

time id name age
3600 1 jane 30
7200 2 lucy 20
10800 3 boby 25
18000 5 lily 28

正常に削除できました。

年齢は26~29歳のレコードを削除

同じくageを3600の倍数に変換してtimeとしてレコードを作成する

time id name age
108000 1 jane 30
72000 2 lucy 20
90000 3 boby 25
100800 5 lily 28

コマンドを実行

$ td table:partial_delete example_db original_table --from 93600 --to 108000
Partial delete job 17227447 is queued.
Use 'td job:show 17227447' to show the status.

結果

time id name age
108000 1 jane 30
72000 2 lucy 20
90000 3 boby 25

まとめ

Treasure Data上partial_deleteでレコードを削除できますが、time以外の項目を指定して削除できません。特定のレコードを削除する場合、当カラム(type: int)をtimeとして保存すれば、削除できます。

1
2
1

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
1
2