はじめに
Pythonでhbaseをいじる際には happybaseを用いることが多いと思います。
ただ、happybaseで対応してないコマンドもあり、その場合はhbaseのshellコマンドを直接用いることしかないと思います。
このページでは、hbase上でよくやる作業をまとめておきたいと思います。乱雑ですが、ご容赦ください。
tableの一覧表示
happybaseでできるのだろうか?
hbaseのshellコマンドで行なった。
$ hbase shell
> list
ns_1:table_1
ns_1:table_2
ns_2:table_1
ns_1,ns_2はネームスペース。table_1、table_2はテーブル名。
ネームスペースが異なれば、テーブル名が被っても問題ない。
namespaceの作成
happybaseではできなそう。
> create_namespace "ns_3"
参考:http://www.task-notes.com/entry/20160321/1458538973
tableの作成
happybaseで行える。
families = {
'cf1': dict(max_versions=10),
'cf2': dict(max_versions=1, block_cache_enabled=False),
'cf3': dict(), # use defaults
}
connection.create_table('ns_3:table_1', families)
ただし、namespaceを指定する場合(上記のns_3)は、事前にhbaseのshellコマンドで作成しておく必要がある。
参考:http://happybase.readthedocs.io/en/latest/api.html#happybase.Connection.create_table
tableの削除
happybaseで行える。
connection.delete_table("ns_1:table_1", disable=True)
hbaseはtableを削除するには、テーブルの停止→削除と二段階踏む必要がある。disable=Trueはテーブルの停止も同時に行うオプション。
tableのfamiliesの確認
table = connection.table("ns_1:table_1")
table.families()
table内の特定familiesの削除
hbaseではあるカラムに入ったデータを一括で削除することはできないようだ。
だが、families単位であれば、一括で削除が可能なようだ。
参考:http://www.ne.jp/asahi/hishidama/home/tech/apache/hbase/shell.html#h_alter
だが、happybaseにはその機能は見当たらない。
そのため、複数カラムを一括で削除したい場合には、テーブルを分けておき、テーブルごと削除→再作成するのが良さそう。