環境
- MacOS10.8.5
- スタンドアロンモード
ダウンロード
wget http://ftp.riken.jp/net/apache/hbase/hbase-0.94.18/hbase-0.94.18.tar.gz
展開
tar -zvzf hbase-0.94.18.tar.gz
cd hbase-0.94.18/
JAVA_HOMEの設定
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
export PATH="$JAVA_HOME/bin:$PATH"
起動
bin/start-hbase.sh
確認
下記にアクセスして画面が表示されていればOK
HBaseにアクセス
HBase shellを起動
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.18, r1577788, Sat Mar 15 04:46:47 UTC 2014
hbase(main):001:0>
テーブル作成
create "table", "column_family"
データの挿入
put "table", "row_1", "column_family:column_1", "val_1"
put "table", "row_1", "column_family:column_2", "val_2"
put "table", "row_2", "column_family:column_1", "val_1"
put "table", "row_2", "column_family:column_2", "val_2"
put "table", "row_2", "column_family:column_3", "val_3"
まとめて挿入する場合(JRubyのIRBなのでRubyの文法が使える)
1000.times { |i| put 'table', "row_#{i}", "column_family:column_1", "val_#{i}"}
1000.times { |i| put 'table', "row_#{i}", "column_family:column_2", "val_#{i}"}
1000.times { |i| put 'table', "row_#{i}", "column_family:column_3", "val_#{i}"}
データの取得
get "table", "row_1"
データの削除
delete "table", "row_1", "column_family:column_1"
データのスキャン
全部
scan "table"
範囲を指定
scan "table", {STARTROW => "row_998"}
scan "table", {STARTROW => "row_960", STOPROW => "row_980"}
タイムスタンプを指定
scan "table", {FILTER => "TimestampsFilter(1398219062910)"}
終了
bin/stop-hbase.sh
Javaから取得してみる
pom.xml
下記を追加
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.94.18</version>
<scope>provided</scope>
</dependency>
App.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class App {
public static void main(String[] args) {
try {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
HTable table = new HTable(conf, "table");
byte[] column_family = Bytes.toBytes("column_family");
byte[] column_1 = Bytes.toBytes("column_1");
byte[] row_1 = Bytes.toBytes("row_1");
Get get = new Get(row_1);
Result result = table.get(get);
System.out.println(Bytes.toString(result.getValue(column_family, column_1)));
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}