H base the_definitive_guide_chapter_04_part3

18
HBase: The Definitive Guide CHAPTER 4 Client API: Advanced Features

Transcript of H base the_definitive_guide_chapter_04_part3

Page 1: H base the_definitive_guide_chapter_04_part3

HBase: The Definitive Guide

CHAPTER 4

Client API: Advanced Features

Page 2: H base the_definitive_guide_chapter_04_part3

この章の内容

• Filters ※1回目はここまで。

• Counters ※2回目はここから。。

• Coprocessors ※2回目はここの途中まで。今回(3回目)はここから。。

• HTablePool

• Connection Handling

Page 3: H base the_definitive_guide_chapter_04_part3

RegionObserverの各メソッドの引数 (p183) • 書籍内では簡略化のため第1引数を省略 • void

postPut(ObserverContext<RegionCoprocessorEnvironment> c, Put put, WALEdit edit, boolean writeToWAL)

↓ ↓ ↓

– void postPut (...)

• RegionCoprocessorEnvironment – Table 4-12 (p185)

• #getRegionServerServices() – RegionServerServices

» Table 4-13 (p186)

• ObserverContext

Page 4: H base the_definitive_guide_chapter_04_part3

ObserverContext(p186~) • CoprocessorEnvironmentにアクセス

• コールバック処理の中で何をすべきかフレームワークに指示

• execution chainの中で同じインスタンスが使われる。

• Table 4-14(p187)

– bypass()

• デフォルトの処理を止める

• 戻り値を通常の処理とは別に、自分で定義した値に変更。

– Example 4-21(p189)の例

– complete()

• execution chainの残りのcoprocessorsをスキップ

public void preSplit(ObserverContext<RegionCoprocessorEnvironment> e) { e.bypass(); }

Page 5: H base the_definitive_guide_chapter_04_part3

RegionObserverの例(p188~) • BaseRegionObserver (RegionObserver をimplementsした空の実装クラス)を extends

• Example 4-20 (p188)

– 仕様:get メソッドで、RowKeyに“@@@GETTIME@@@“を指定され

たら、サーバの現在日時を返す

• “@@@GETTIME@@@“ は任意の文字列。何でも良い。仕様次第。

• サーバの現在日時を、value にセットして返す。

• Shellで、getメソッド使用で、上記valueが返ってくる

new KeyValue(get.getRow() // row key , FIXED_ROW // family , FIXED_ROW // qualifier , Bytes.toBytes(System.currentTimeMillis())); // value

> get 'testtable', '@@@GETTIME@@@' COLUMN CELL @@@GETTIME@@@:@@@GETTIME@@@ timestamp=9223372036854775807, ¥ value=¥x00¥x00¥x01/s@3¥xD8

Page 6: H base the_definitive_guide_chapter_04_part3

RegionObserverの例(p189)

• row key=‘@@@GETTIME@@@’のデータが存在する場合

– 実データと、Observerでセットした現在日時、の2レコードが返る。

– Example 4-21 (p189)

• e.bypass() を追加

> get 'testtable2', '@@@GETTIME@@@' COLUMN CELL @@@GETTIME@@@:@@@GETTIME@@@ timestamp=9223372036854775807, ¥ value= ¥x00¥x00¥x01/sJ¥xBC¥xEC colfam1:qual1 timestamp=1303309353184, value=Hello there!

> get 'testtable', '@@@GETTIME@@@' COLUMN CELL @@@GETTIME@@@:@@@GETTIME@@@ timestamp=9223372036854775807, ¥ value= ¥x00¥x00¥x01/s]¥x1D4

Page 7: H base the_definitive_guide_chapter_04_part3

MasterObserver(p190)

• DDL処理 – void preCreateTable(...) / void postCreateTable(...)

– (※以下略)

• MasterCoprocessorEnvironment (p191)

– RegionCoprocessorEnvironment とRegionObserverの関係に相似

– Table 4-15 (p191)

• #getMasterServices

–MasterServices

»Table 4-16 (P191)

Page 8: H base the_definitive_guide_chapter_04_part3

MasterObserverの例(p192~) • BaseMasterObserver (MasterObserver をimplementsした空の実装クラス)を extends

• Example 4-22 (p192)

– 仕様:テーブルが作成される度に、”テーブル名-blobs”というディレクトリをHDFS上に作成する。 • HTableDescriptorからテーブル名を取得

• ObserverContext#getEnvironment() で MasterCoprocessorEnvironment を取得。

• MasterCoprocessorEnvironment#getMasterServices() で MasterFileSystem を取得。

• MasterFileSystem#getFileSystem() で org.apache.hadoop.fs.FileSystem を取得。

• HDFS上にディレクトリを作成。

• Shell上でテーブル作成 ->ディレクトリが作成されていることを確認

> create 'testtable', 'colfam1' $ bin/hadoop dfs –ls Found 1 items drwxr-xr-x - larsgeorge supergroup 0 ... /user/larsgeorge/testtable-blobs

Page 9: H base the_definitive_guide_chapter_04_part3

Endpoints (p193~) • クライアントから全てのリージョンにリクエスト

• 各リージョンはローカルで集計処理

• 全リージョンから各集計結果がクライアントに返る

• 例

– 1,000リージョン

– 10,000カラム

– クライアントは1,000の集計結果を取得 → それをさらに集計

– worst-case scenario • クライアントAPIで全レコードをScanして集計

Page 10: H base the_definitive_guide_chapter_04_part3

CoprocessorProtocol (p194)

• Single region – Row key を指定 -> Regionが一意に決まる

• HTable#coprocessorProxy

• Range of regions – start key, end key を指定

– #coprocessorExec • HTable#coprocessorExec(Class<T> protocol,

, byte[] startKey , byte[] endKey , Batch.Call<T,R> callable)

• HTable#coprocessorExec(Class<T> protocol,

, byte[] startKey , byte[] endKey , Batch.Call<T,R> callable , Batch.Callback<R> callback)

– クライアント側はBatch.Callを実装 -> 中でCoprocessorProtocol インスタンスのメソッドをcall。

Page 11: H base the_definitive_guide_chapter_04_part3

Endpointsの例(p195~) • 2steps

– 1. CoprocessorProtocol を継承したinterfaceを定義

– 2. BaseEndpointCoprocessor を継承して、且つ、1をimplementsする

• Example 4-23 (p195) – Step1. CoprocessorProtocolを継承したinterface。クライアントからremote callするメソッドを定義。

• Example 4-24 (p195) – Step2. BaseEndpointCoprocessorを継承かつ上のinterfaceをimplements

public interface RowCountProtocol extends CoprocessorProtocol { long getRowCount() throws IOException; long getRowCount(Filter filter) throws IOException; long getKeyValueCount() throws IOException; }

public class RowCountEndpoint extends BaseEndpointCoprocessor implements RowCountProtocol { (※中略) }

Page 12: H base the_definitive_guide_chapter_04_part3

Endpointsの例(p195~) • Example 4-24 (p195~p196)

– サーバ側(RowCountEndpoint.java)の処理 • (1)FirstKeyOnlyFilter を使用して、Row カウントを取得

• (2)KeyValueの全カウントを取得

• Example 4-25 (p196~p197)

– クライアント側の実装

Map<byte[], Long> results = table.coprocessorExec( RowCountProtocol.class, // ① null, null, // ② new Batch.Call<RowCountProtocol, Long>() { // ③ @Override public Long call(RowCountProtocol counter) throws IOException return counter.getRowCount(); // ④ } });

Page 13: H base the_definitive_guide_chapter_04_part3

Endpointsの例(p195~)

• Example 4-25 (p197)

– ① 第1引数にCoprocessorProtocol interfaceを実装したクラス指定

– ② 第2、3引数にstart key, end key を指定。Nullの場合は全て。

– ③ 全てのリージョンサーバに送信される、Batch.Call の匿名クラス

– ④ The call() method is executing the endpoint functions.

(結果)

Region: testtable,,1303417572005.51f9e2251c29ccb2...cbcb0c66858f., Count: 2 Region: testtable,row3,1303417572005.7f3df4dcba3f...dbc99fce5d87., Count: 3 Total Count: 5

Page 14: H base the_definitive_guide_chapter_04_part3

Endpointsの例(p196~p197)

• Example 4-26 (p197)

– Batch#forMethod 使用

• リフレクションを使って、メソッド名を文字列で指定

• Example 4-27 (p198)

– Batch.Call の中で、2つのメソッド呼び出し • # getRowCount

• # getKeyValueCount

(結果)

Region: testtable,,1303420252525.9c336bd2b294a...0647a1f2d13b., Count: {2,4} Region: testtable,row3,1303420252525.6d7c95de8a7...386cfec7f2., Count: {3,6} Total Row Count: 5 Total KeyValue Count: 10

Page 15: H base the_definitive_guide_chapter_04_part3

Endpointsの例(p198~)

• Example 4-28 (p198)

– HTable#coprocessorProxy 使用

– Row Key 指定

– client-side proxy of the endpoint

– クライアントからCoprocessorProtocol実装のremote functionを呼び出し。

RowCountProtocol protocol = table.coprocessorProxy( RowCountProtocol.class, Bytes.toBytes("row4")); long rowsInRegion = protocol.getRowCount();

Page 16: H base the_definitive_guide_chapter_04_part3

Figure 4-5. parallel <-> a single region only (p199)

Page 17: H base the_definitive_guide_chapter_04_part3

HTablePool (p199)

• Htableの再利用

– 高コスト

• 高負荷環境(qps 1000とか)で毎回インスタンス化するのは止めた方がいい。

• HTable スレッドセーフではない

– 複数スレッドで1つのHTableを利用はお勧めしない

• 1スレッドにつき1つ

• それを解決するのがHTablePoolクラスを使用すること。

Page 18: H base the_definitive_guide_chapter_04_part3

Connection Handling (p203)

• Hconnection

• HConnectionManager

• すみません、、、後は口頭で。。

Configuration conf = HBaseConfiguration.create(); HTable table1 = new HTable(conf, "table1"); //... HTable table2 = new HTable(conf, "table2");