Transcript HBase

Seven Databases in Seven Weeks
HBase
HBase
HDFS (Hadoop Distributed File System)
DFS
Server
7つのデータベース7つの世界 での構成
1日目:CRUDとテーブル管理
スタンドアロンでHbaseを動かす
テーブルを作る
データの出し入れをする
2日目:ビッグデータを扱う
Wikipedia ダンプを投入する
スクリプト (Not Shell) での操作に慣れる
3日目:クラウドに持っていく
Thrift を使って操作する
今回は扱いません
Whirr を使って EC2 にデプロイする
今回は扱いません
HBase の特徴
自動シャーディング・自動フェールオーバー
テーブルサイズが大きくなった時、自動的に分割する
分割されたシャードは、ノード障害時に自動的にフェールオーバーする
データの一貫性 (CAP:Consistency)
データの更新は反映された瞬間から読出可能
結果的に同じ値が読めるようになる(結果整合性)条件緩和を取らない
Hadoop/HDFS 統合
Hadoop の HDFS 上 に展開できる
Hadoop/MapReduce でAPIを挟まず HBase を入出力の対象にできる
各種インタフェース
Java Native API の他、 Thrift , REST API から利用可能
1日目:HBase をスタンドアロンで展開する
実行コマンド
[root@HBase01
[root@HBase01
[root@HBase01
[root@HBase01
ask]#
opt]#
opt]#
opt]#
cd /opt/
wget http://ftp.meisei-u.ac.jp/mirror/apache/dist/hbase/hbase-0.94.7/hbase-0.94.7.tar.gz
tar zxvf hbase-0.94.7.tar.gz
vi hbase-0.94.7/conf/hbase-site.xml
hbase-site.xml
ファイル実体配置
/var
/files
/hbase
/zookeeper
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///var/files/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/var/files/zookeeper</value>
</property>
</configuration>
単体で可動するための最小限の設定
ファイル設置先の指定で、任意のディレクトリを書き出し先に指定する
xmlで指定できる全項目 : src/main/resources/hbase-default.xml
1日目:HBase をスタンドアロンで展開する
JDKが要求される
[root@HBase01 opt]# hbase-0.94.7/bin/start-hbase.sh
+======================================================================+
|
Error: JAVA_HOME is not set and Java could not be found
|
+----------------------------------------------------------------------+
| Please download the latest Sun JDK from the Sun Java web site
|
|
> http://java.sun.com/javase/downloads/ <
|
|
|
| HBase requires Java 1.6 or later.
|
| NOTE: This script will find Sun Java whether you install using the
|
|
binary or the RPM based installer.
|
+======================================================================+
JDKのバリエーション(以下から選んで導入)
Oracle JDK
Open JDK
1.6
1.6
1.7
1.7
Javaのインストールディレクトリを指定
[root@HBase01 opt]# vi hbase-0.94.7/conf/hbase-env.sh
- # export JAVA_HOME=/usr/java/jdk1.6.0/
+ export JAVA_HOME=/usr/java/latest/
1日目:HBase をスタンドアロンで展開する
起動
[root@HBase01 opt]# hbase-0.94.7/bin/start-hbase.sh
starting master, logging to /opt/hbase-0.94.7/bin/../logs/hbase-root-master-HBase01.db.algnantoka.out
シェル接続
[root@HBase01 opt]# hbase-0.94.7/bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.7, r1471806, Wed Apr 24 18:48:26 PDT 2013
hbase(main):001:0> status
1 servers, 0 dead, 2.0000 average load
停止
[root@HBase01 opt]# hbase-0.94.7/bin/stop-hbase.sh
stopping hbase...........
1日目:HBase の使い方
テーブル作成 : create
hbase(main):009:0> help "create"
Create table; pass table name, a dictionary of specifications per
column family, and optionally a dictionary of table configuration.
Dictionaries are described below in the GENERAL NOTES section.
Examples:
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
create 't1', {NAME => 'f1', VERSIONS => 5}
create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
# The above in shorthand would be the following:
create 't1', 'f1', 'f2', 'f3‘
create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}
create 't1', 'f1', {SPLITS => ['10', '20', '30', '40']}
create 't1', 'f1', {SPLITS_FILE => 'splits.txt'}
# Optionally pre-split the table into NUMREGIONS, using
# SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
基本型
Create ‘TableName’ , {NAME => ‘ColumnFamilyName’, Option => Value …} …
省略表記
Create ‘TableName’ , ‘ColumnFamilyName’, …
1日目:HBase の使い方
レコード挿入 : put
hbase(main):010:0> help "put"
Put a cell 'value' at specified table/row/column and optionally
timestamp coordinates. To put a cell value into table 't1' at
row 'r1' under column 'c1' marked with the time 'ts1', do:
hbase> put 't1', 'r1', 'c1', 'value', ts1
SampleTable : create ‘SampleTable’ , ‘color’ , ‘shape’
put ‘SampleTable’ , ‘first’ , ‘color:red’ , ‘#F00’
put ‘SampleTable’ , ‘first’ , ‘color:blue’ , ‘#00F’
put ‘SampleTable’ , ‘first’ , ‘color:yellow’ , ‘#FF0’
1日目:HBase の使い方
レコード取得 : get
SampleTable
hbase(main):011:0> help "get"
Get row or cell contents; pass table name, row, and optionally
a dictionary of column(s), timestamp, timerange and versions.
Examples:
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
get
get
get
get
get
get
get
get
get
get
get
't1',
't1',
't1',
't1',
't1',
't1',
't1',
't1',
't1',
't1',
't1',
'r1‘
'r1',
'r1',
'r1',
'r1',
'r1',
'r1',
'r1',
'r1',
'r1',
'r1',
{TIMERANGE => [ts1, ts2]}
{COLUMN => 'c1'}
{COLUMN => ['c1', 'c2', 'c3']}
{COLUMN => 'c1', TIMESTAMP => ts1}
{COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
{COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
{FILTER => "ValueFilter(=, 'binary:abc')"}
'c1‘
'c1', 'c2‘
['c1', 'c2']
get ‘SampleTable’ , ‘first’
get ‘SampleTable’ , ‘first’ , ‘color’
get ‘SampleTable’ , ‘first’ , ‘color:blue’
1日目:HBase の使い方
レコード検索 : scan
hbase(main):001:0> help 'scan'
Scan a table; pass table name and optionally a dictionary of scanner
specifications. Scanner specifications may include one or more of:
TIMERANGE, FILTER, LIMIT, STARTROW, STOPROW, TIMESTAMP, MAXLENGTH,
or COLUMNS, CACHE
If no columns are specified, all columns will be scanned.
To scan all members of a column family, leave the qualifier empty as in
'col_family:'.
The filter can be specified in two ways:
1. Using a filterString - more information on this is available in the
Filter Language document attached to the HBASE-4176 JIRA
2. Using the entire package name of the filter.
Some examples:
hbase>
hbase>
hbase>
hbase>
hbase>
hbase>
scan
scan
scan
scan
scan
scan
'.META.'
'.META.', {COLUMNS => 'info:regioninfo'}
't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}
't1', {FILTER => "(PrefixFilter ('row2') AND (QualifierFilter (>=, 'binary:xyz'))) AND (TimestampsFilter ( 12
't1', {FILTER => org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
For experts, there is an additional option -- CACHE_BLOCKS -- which
switches block caching for the scanner on (true) or off (false). By
default it is enabled. Examples:
hbase> scan 't1', {COLUMNS => ['c1', 'c2'], CACHE_BLOCKS => false}
Also for experts, there is an advanced option -- RAW -- which instructs the
scanner to return all cells (including delete markers and uncollected deleted
cells). This option cannot be combined with requesting specific COLUMNS.
Disabled by default. Example:
hbase> scan 't1', {RAW => true, VERSIONS => 10}
1日目:HBase の使い方
TimeStamp
put ‘table’ , ‘first’ , ‘color:red’ , ‘#FFF‘
put ‘table’ , ‘first’ , ‘color:red’ , ‘#000'
put ‘table’ , ‘first’ , ‘color:red’ , ‘#0F0‘
put ‘table’ , ‘first’ , ‘color:red’ , ‘#00F'
put ‘table’ , ‘first’ , ‘color:red’ , ‘#F00'
‘first’ , ‘color:red’
timestamp 5
timestamp 4
#F00
get ‘table’ , ‘first’ , ‘color:red’
#00F
timestamp 3
timestamp 2
#0F0
#000
get ‘table’ , ‘first’ , {COLUMN=>‘color:red’ ,
TIMESTAMP=>4}
timestamp 1
#FFF
get ‘table’ , ‘first’ , {COLUMN=>‘color:red’ ,
VERSIONS=>4}
1日目:HBase の使い方
スキーマ変更: alter
hbase(main):009:0> disable 'table1'
0 row(s) in 2.5190 seconds
alter の対象 Table は
オフラインでなければ
ならない
hbase(main):010:0> get 'table1', 'first','color:red'
COLUMN CELL
ERROR: org.apache.hadoop.hbase.DoNotRetryIOException: table1 is disabled.
hbase(main):012:0> alter 'table1' , { NAME => 'color', VERSIONS => 10}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.3630 seconds
保持するバージョン数の変更
hbase(main):014:0> enable 'table1'
0 row(s) in 2.3000 seconds
alter によるスキーマ変更の手順は以下
1. 新たなスキーマの空テーブルを作る
2. 元テーブルからデータを複製する
3. 元テーブルを破棄する
高コストなので、原則スキーマ変更( ColumnFamily の変更)は行わない
1日目:HBase の使い方
JRubyスクリプティング
hoge.rb
include Java
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.HBaseConfiguration
hbase関係の Javaクラス
def jbytes(*args)
args.map { |arg| arg.to_s.to_java_bytes }
end
table = HTable.new( HBaseConfiguration.new, "table1" )
p = Put.new( *jbytes( "third" ) )
p.add( *jbytes( "color", "black", "#000" ) )
p.add( *jbytes( "shape", "triangle", "3" ) )
p.add( *jbytes( "shape", "square", "4" ) )
table.put( p )
レコード挿入タイミング
実行
[root@HBase01 opt]# hbase-0.94.7/bin/hbase shell hoge.rb
hbase(main):002:0> get 'table1', 'third' ,{COLUMN => ['color','shape']}
COLUMN CELL
color:black timestamp=1369049856405, value=#000
shape:square timestamp=1369049856405, value=4
shape:triangle timestamp=1369049856405, value=3
9 row(s) in 0.0870 seconds
レコードのtimestampが揃う
hbase shell は JRuby インタプリタを拡張したものなので、JRubyが実行できる
Hbaseとは何か
Google の 内部システム
(発表した論文より)
バッチ処理
MapReduce
Hadoop プロジェクト
(Googleクローン)
リアルタイム応答
BigTable
Google File Sytem (GFS)
MapReduce
HBase
Hadoop Distributed
File Sytem (HDFS)
BigTable(ソート済列志向データベース)
スキーマで定義する
RowKey ColumnFamily1
ColumnFamily2
1
Column1 Column2 Column1
Column1 Column2
2
Column2 Column3
ColumnFamily3
Column2 Column3
スキーマレス(自由に追加できる)
必須
ソート済
あるColumn
timestamp 5
timestamp 4
#F00
timestamp 3
timestamp 2
#0F0
#000
timestamp 1
#FFF
#00F
タイムスタンプでバージョニングされる
BigTable(ソート済列志向データベース)
RowKey ColumnFamily1
ColumnFamily2
ColumnFamily3
1
2
3
4
リージョン
リージョン
リージョン
リージョン
リージョン
リージョン
5
6
7
8
9
•
•
•
•
テーブルはリージョンで物理的に分割(シャーディング)される
リージョンはクラスタ中のリージョンサーバが担当する
リージョンは ColumnFamily 毎に作られる
リージョンは ソート済のRowKey を適当なサイズで分割する
BigTable(ソート済列志向データベース)
テーブルスキーマの初期設計超重要
ColumnFamilyはむやみに増やさない → Columnの追加で極力対応
RowKeyは連続アクセスが起きやすい形にしておく
ColumnやColumnFamilyを条件にして検索する構造を取らない
•
•
•
•
テーブルはリージョンで物理的に分割(シャーディング)される
リージョンはクラスタ中のリージョンサーバが担当する
リージョンは ColumnFamily 毎に作られる
リージョンは ソート済のRowKey を適当なサイズで分割する
HBase の特徴
自動シャーディング・自動フェールオーバー
テーブルサイズが大きくなった時、自動的に分割する
分割されたシャードは、ノード障害時に自動的にフェールオーバーする
データの一貫性 (CAP:Consistency)
データの更新は反映された瞬間から読出可能
結果的に同じ値が読めるようになる(結果整合性)条件緩和を取らない
Hadoop/HDFS 統合
Hadoop の HDFS 上 に展開できる
Hadoop/MapReduce でAPIを挟まず HBase を入出力の対象にできる
HBase の特徴
を構成する要素
自動シャーディング・自動フェールオーバー
リージョンの自動分割
??
データの一貫性 (CAP:Consistency)
??
Hadoop/HDFS 統合
HDFS : GFSクローン
Hbase : BigTableクローン
HBase の特徴
を構成する要素
自動フェールオーバー・データの一貫性 (CAP:Consistency)
HDFS
Read
オンメモリ
ストア
ローカルストア
WAL
Region
Write
Master Server
Region Server
replicate
ローカルストア
WAL
ZooKeeper
Region Server
(フェールオーバー先)
Region
2日目:Wikipediaのデータを扱う
力尽きた\(^0^)/
Scan にかかる秒数
250
200
150
100
50
0
text含む
text含まず
圧縮あり
117.751
10.424
圧縮なし
193.735
11.961
圧縮あり
圧縮なし