More Related Content Similar to Hive undocumented feature (20) Hive undocumented feature2. Name: tamtam (タムタム)
Twitter : @tamtam180
Blog: http://mt.orz.at/
Works: Software Enginner
SIer -> Video Game, Web Developer -> ???
3. HiveCLIコマンド
Hook機能
PerfLog
PDK
自動でUDF登録
HTTP-REST独自サーバ
おまけ
5. Hadoop DFSコマンドが使えます
hive> dfs -ls /;
Found 3 items
drwxr-xr-x - tamtam supergroup 0 2012-04-28 23:03 /home
drwxr-xr-x - tamtam supergroup 0 2012-05-20 14:17 /tmp
drwxr-xr-x - tamtam supergroup 0 2012-04-28 20:43 /user
※ドキュメント
ありました
6. ADDしたリソース一覧取得
list [FILE|JAR|ARCHIVE] [<value> [<value>]*]
hive> list jar;
file:/home/tamtam/hadoop/hives/hive-0.9.0-bin/lib/hive-builtins-0.9.0.jar
※ドキュメント
ありました
7. シェルスクリプトのsourceと同じ
$cat hoge.q
show tables;
list jar;
hive> source hoge.q;
{"tables":["file_hdfs_to_hive_custom_no_stage_tgt","hoge1","tweet"]}
Time taken: 4.226 seconds
file:/home/tamtam/hadoop/hives/hive-0.9.0-bin/lib/hive-builtins-0.9.0.jar
8. シェルの実行(viの!と同じ)
hive> !uname -a;
Linux tamsvr20 3.1.0-7.fc16.x86_64 #1 SMP Tue Nov 1 21:10:48 UTC 2011 x86_64
x86_64 x86_64 GNU/Linux
※ドキュメント
ありました
9. show locks
msck
…
叩くな危険!
11. StatementHook
HiveDriverRunHook
AnalyzerHook (HiveSemanticAnalyzerHook)
ExecuteWithHookContext
▪ Old: PreExecute, PostExecute
MetadataHook(Listener)
MetaStorePreEventListener
MetaStoreEventListener
EndFunctionListener
ClientStatsPublisher
12. Driver
[pre]hive.exec.driver.run.hooks
Command run
[pre]hive.semantic.analyzer.hook
compile
[post]hive.semantic.analyzer.hook
[pre] hive.exec.pre.hooks
execute [failure] hive.exec.pre.hooks
[post] hive.exec.pre.hooks
[post]hive.exec.driver.run.hooks
13. HiveDriverRunHookを実装
hive.exec.driver.run.hooks
version 0.9 ~
Driver#run
HiveDriverRunHook#preDriverRun
▪ HiveDriverRunHookContext
HiveDriverRunHok#postDriverRun
▪ HiveDriverRunHookContext
14. AbstractSemanticAnalyzerHookを継承
※ HiveSemanticAnalyzerHookを実装してもNG
hive.semantic.analyzer.hook
Driver#compile
preAnalyze
▪ HiveSemanticAnalyzerHookContext
▪ ASTNode
postAnalyze
▪ HiveSemanticAnalyzerHookContext
▪ List<Task<? Extends Serializable>>
15. ExecuteWithHookContextを実装
hive.exec.pre.hooks
HookContext
hive.exec.post.hooks
HookContext
hive.exec.failure.hooks (hive-0.9)
HookContext
Driver#execute
16. QueryPlan
HiveConf
TaskList
Inputs
Outputs
LineageInfo
UserGrpupInformation
HookType
InputPathToContentSummary
…
17. MetaStorePreEventListenerを継承する
hive.metastore.pre.event.listeners
HMSHandler
Metastore操作前をHook
18. MetaStoreEventListenerを継承する
hive.metastore.event.listeners
HMSHandler
Metastore操作後をHook
19. MetaStoreEndFunctionListenerを継承する
hive.metastore.end.function.listeners
HMSHandler
Metastoreへの操作の終了時にHook
20. Databaseの取得/作成/削除/変更
テーブルの(ry
インデックスの(ry
GRANT
などなど..
21. 基本は単体テスト用。
使うことはないと思います。
22. 進捗情報のコールバック
ClientStatsPublisherを実装する
ジョブIDとカウンタ一覧が取得できる
hive.client.stats.publishers
25. パフォーマンスログ
Hive-0.8 ~
LogLevel=INFO
Format=XMLみたいな書式
様々な所に仕込まれている
プラガブル
26. org.apache.hadoop.hive.ql.Driver
acquireReadWriteLocks
compile
doAuthorization
Driver.execute
releaseLocks
PreHook
PostHook
FailureHook
Driver.run
org.apache.hadoop.hive.ql.optimizer.ppr.PartitionPruner
prune-listing
partition-retrieving
27. (PerfLogger.java:PerfLogBegin(99)) - <PERFLOG
method=PostHook.at.orz.hive.ext.hook.MyHookPost1>
(PerfLogger.java:PerfLogEnd(126)) - </PERFLOG
method=PostHook.at.orz.hive.ext.hook.MyHookPost1 start=1338242232
(PerfLogger.java:PerfLogEnd(126)) - </PERFLOG method=Driver.execute
start=1338242228182 end=1338242232117 durat
(SessionState.java:printInfo(391)) - OK
(PerfLogger.java:PerfLogBegin(99)) - <PERFLOG method=releaseLocks>
(PerfLogger.java:PerfLogEnd(126)) - </PERFLOG method=releaseLocks
start=1338242232118 end=1338242232118 duratio
(PerfLogger.java:PerfLogEnd(126)) - </PERFLOG method=Driver.run start=1338242227893
end=1338242232119 duration=
(FileInputFormat.java:listStatus(199)) - Total input paths to process : 1
(SessionState.java:printInfo(391)) - Time taken: 4.332 seconds
(PerfLogger.java:PerfLogBegin(99)) - <PERFLOG method=Driver.run>
(PerfLogger.java:PerfLogBegin(99)) - <PERFLOG method=compile>
28. PerfLoggerを継承する
以下のメソッドをオーバーライドする
PerfLogBegin
PerfLogEnd
▪ Must returns duration(endTime - startTime).
close
▪ Called before postDriverRun(Hook) at Driver#run
hive.exec.perf.logger
30. Hive-0.8~
UDF関数の追加を楽にするためのSDK
hive-builtins.jarもこれで作られている
このせいで…後述
31. ディレクトリ構成
Project-Root
build.xml
src/
java-source-files.
test/
setup.sql
cleanup.sql
any datafiles needed by your tests.
32. アノテーションの指定
@UDFType
▪ deterministic
▪ stateful
▪ distinctLike
@Description
▪ name 関数名
▪ value 説明概要
▪ extended 説明
33. アノテーションの指定
@HivePdkUnitTests
▪ setup テストの最初の実行されるHQL
▪ cleanup テストの最後に実行されるHQL
▪ cases それぞれのテストケース
@HivePdkUnitTest
▪ query テストするHQLクエリ
▪ result その結果
34. build.xmlを以下のように作成
<project name="myudf" default="package">
<property name="hive.install.dir" value="*****"/>
<property name="plugin.libname" value="my-udf"/>
<property name="plugin.title" value="My UDF Library"/>
<property name="plugin.version" value="0.1"/>
<property name="plugin.vendor" value="Apache Software Foundation"/>
<property name="function.sql.prefix" value=""/>
<import file="${hive.install.dir}/scripts/pdk/build-plugin.xml"/>
</project>
→ my-udf-0.1.jarが作成される
35. 作成されたJARファイル
$ jar tvf lib/_my-udf-0.1.jar
0 Tue May 22 02:32:46 JST 2012 META-INF/
235 Tue May 22 02:32:44 JST 2012 META-INF/MANIFEST.MF
0 Tue May 22 02:28:10 JST 2012 at/
0 Tue May 22 02:28:10 JST 2012 at/orz/
0 Tue May 22 02:28:10 JST 2012 at/orz/hive/
0 Tue May 22 02:28:10 JST 2012 at/orz/hive/ext/
0 Tue May 22 02:28:10 JST 2012 at/orz/hive/ext/udf/
685 Tue May 22 02:28:10 JST 2012 at/orz/hive/ext/udf/UDFHello.class
95 Tue May 22 02:32:46 JST 2012 META-INF/class-info.xml
67 Tue May 22 02:32:46 JST 2012 META-INF/class-registration.sql
36. 作成されたJARファイル のXML
class-info.xml
<ClassList>
<Class javaname="at.orz.hive.ext.udf.UDFHello" sqlname="hello" />
</ClassList>
class-registration.sql
CREATE TEMPORARY FUNCTION hello AS 'at.orz.hive.ext.udf.UDFHello';
37. SessionState生成時に以下のコードで登録
される org.apache.hive.builtins.BuiltinUtilsを検索
Class<?> pluginClass = Utilities.getBuiltinUtilsClass();
URL jarLocation = JARファイルの場所特定
pluginClass.getProtectionDomain().getCodeSource().getLocation();
ADD JAR
add_builtin_resource(ResourceType.JAR, jarLocation.toString());
FunctionRegistry.registerFunctionsFromPluginJar(jarLocation,
pluginClass.getClassLoader()); CREATE TEMPORARY FUNCTION
38. SessionStateが生成されるタイミング
サーバ
HiveServerHandler生成時
▪ Connection接続時
クライアント
CliSessionState生成時
▪ CliDriver#run
▪ HIVE CLI起動時
▪ HWIセッション生成時
43. auxJarに追加する
設定のhive.aux.jars.path に入っていればOK
▪ HiveCLI起動時にHIVE_AUX_JARS_PATHを指定
▪ 環境変数
▪ --auxpath オプション
▪ $HIVE_HOME/auxlib/に配置
44. HiveCLIの場合、
起動時にスクリプトを自動実行する方法な
らある。
ファイルの実行
▪ -i オプション
▪ $HIVE_HOME/bin/.hiverc
▪ ~/.hiverc
クエリの実行
45. hiveServerの場合、
そのオプションは無い
Connection生成時にSessionStateが生成され
る
SessionState生成時にbuiltin.jarの関数が登
録される
▪ builtin.jarはhive-builtin.jar固定っぽい..
46. [tamtam@tamsvr20 lib]$ jar tf hive-builtins-0.9.0.jar
META-INF/
META-INF/MANIFEST.MF HIVE_HOME/
org/ lib/
org/apache/
org/apache/hive/ hive-builtins-*.jar
org/apache/hive/builtins/
org/apache/hive/builtins/BuiltinUtils.class
org/apache/hive/builtins/UDAFUnionMap$Evaluator.class
org/apache/hive/builtins/UDAFUnionMap$State.class
org/apache/hive/builtins/UDAFUnionMap.class
META-INF/class-info.xml
META-INF/class-registration.sql
これが検索される
48. HTTP RESTサーバ
Concurrency
HiveServerはConcurrencyではない
HiveServer2はConcurrency (HIVE-2395)
自動UDF登録対応
設定やLIBはそのまま利用
起動方法: ./hive –service myserver
ソースコードは200行程度
49. $ curl --data @- -X POST --dump - "http://127.0.0.1:9998/s/query"
SHOW TABLES
HTTP/1.1 200 OK
Content-Length: 68
Server: Jetty(6.1.26)
[{"name":"tab_name","type":"string"}]
["hoge2"]
["hoge3"]
["hoge4"]
50. $ curl --data @- -X POST --dump - "http://127.0.0.1:9998/s/query"
select * from table5
HTTP/1.1 200 OK
Content-Length: 88
Server: Jetty(6.1.26)
[{"name":"col1","type":"string"}]
["111222333"]
["222333444"]
["333444555"]
51. $ curl -X GET --dump - "http://127.0.0.1:9998/s/dump/conf"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 11286
Server: Jetty(6.1.26)
{"hive.security.authenticator.manager":"org.apache.hadoop.hive.ql.security.HadoopDefau
"hive.exec.plan":"",
"hive.metastore.batch.retrieve.max":300,
"hive.optimize.index.groupby":false,
"datanucleus.cache.level2.type":"none",
"hive.enforce.sorting":false,
……….
53. IN句
INSERT INTO (hive-0.8)
Import/Export (hive-0.8)
IMPORT FROM path
EXPORT TABLE xxx TO path
54. 0.6
0.7
0.71
+ any patches -> CDH3
0.8
0.81
+ any patches -> CDH4
0.9 (stable)
0.10 (trunk)
55. Feature
UDF
str_to_map, covar_samp, corr, percentile_approx
collect_set, ngrams, context_ngrams, reflect
create_union, sentences
json_tuple, parsae_url_tuple
56. Feature
timestamp type. (Don’t use to 0.10)
Bitmap Index
UDF
ewah_bitmap, ewah_bitmap_and, ewah_bitmap_or
Ewah_bitmap_empty
timestamp, binary
assert_true
map_keys, map_values, named_struct, in_file
stack, from_utc_timestamp, to_utc_timestamp
57. Feature
UDF
<=>
Between
java_method (reflect)
sort_array
printf
58. JDBC
Timestamp
executeUpdate( 0.71->OK, 0.8 -> NG)
Etc..
Like UDF
“¥” escapes is incollect.
Simple match is OK.
▪ Single wildcard
▪ Simple range match
60. Hook機能は普通使わない
PerfLOGを見ることはほとんどない
PDKは使えない
Timestampは使ってはいけない
ドキュメント化されていないものは
それなりに理由がある