hive undocumented feature
Post on 16-Jun-2015
7.995 Views
Preview:
TRANSCRIPT
@tamtam180
Name: tamtam (タムタム) Twitter : @tamtam180 Blog: http://mt.orz.at/ Works: Software Enginner
SIer -> Video Game, Web Developer -> ???
HiveCLIコマンド Hook機能 PerfLog PDK 自動でUDF登録 HTTP-REST独自サーバ おまけ
./hive
./hive --service cli
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
Hadoop DFSコマンドが使えます
※ドキュメント ありました
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
※ドキュメント ありました
$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
シェルスクリプトのsourceと同じ
シェルの実行(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
※ドキュメント ありました
show locks msck …
叩くな危険!
StatementHook HiveDriverRunHook
AnalyzerHook (HiveSemanticAnalyzerHook)
ExecuteWithHookContext ▪ Old: PreExecute, PostExecute
MetadataHook(Listener) MetaStorePreEventListener
MetaStoreEventListener
EndFunctionListener ClientStatsPublisher
Driver
[pre]hive.exec.driver.run.hooks
Command run
compile
execute
[post]hive.exec.driver.run.hooks
[pre] hive.exec.pre.hooks
[post] hive.exec.pre.hooks
[failure] hive.exec.pre.hooks
[pre]hive.semantic.analyzer.hook
[post]hive.semantic.analyzer.hook
HiveDriverRunHookを実装 hive.exec.driver.run.hooks version 0.9 ~ Driver#run
HiveDriverRunHook#preDriverRun
▪ HiveDriverRunHookContext
HiveDriverRunHok#postDriverRun
▪ HiveDriverRunHookContext
AbstractSemanticAnalyzerHookを継承 ※ HiveSemanticAnalyzerHookを実装してもNG
hive.semantic.analyzer.hook Driver#compile
preAnalyze ▪ HiveSemanticAnalyzerHookContext
▪ ASTNode
postAnalyze ▪ HiveSemanticAnalyzerHookContext
▪ List<Task<? Extends Serializable>>
ExecuteWithHookContextを実装 hive.exec.pre.hooks
HookContext
hive.exec.post.hooks
HookContext
hive.exec.failure.hooks (hive-0.9)
HookContext
Driver#execute
QueryPlan HiveConf TaskList Inputs Outputs LineageInfo UserGrpupInformation HookType InputPathToContentSummary …
MetaStorePreEventListenerを継承する hive.metastore.pre.event.listeners HMSHandler
Metastore操作前をHook
MetaStoreEventListenerを継承する hive.metastore.event.listeners HMSHandler
Metastore操作後をHook
MetaStoreEndFunctionListenerを継承する hive.metastore.end.function.listeners HMSHandler
Metastoreへの操作の終了時にHook
Databaseの取得/作成/削除/変更 テーブルの(ry インデックスの(ry GRANT などなど..
基本は単体テスト用。 使うことはないと思います。
進捗情報のコールバック ClientStatsPublisherを実装する
ジョブIDとカウンタ一覧が取得できる
hive.client.stats.publishers
DEMO
パフォーマンスログ Hive-0.8 ~ LogLevel=INFO Format=XMLみたいな書式 様々な所に仕込まれている プラガブル
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
(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>
PerfLoggerを継承する 以下のメソッドをオーバーライドする
PerfLogBegin
PerfLogEnd
▪ Must returns duration(endTime - startTime).
close
▪ Called before postDriverRun(Hook) at Driver#run
hive.exec.perf.logger
Plugin Development Kit
Hive-0.8~ UDF関数の追加を楽にするためのSDK hive-builtins.jarもこれで作られている
このせいで…後述
ディレクトリ構成
Project-Root build.xml src/ java-source-files. test/ setup.sql cleanup.sql any datafiles needed by your tests.
アノテーションの指定
@UDFType
▪ deterministic
▪ stateful
▪ distinctLike
@Description
▪ name 関数名
▪ value 説明概要
▪ extended 説明
アノテーションの指定
@HivePdkUnitTests
▪ setup テストの最初の実行されるHQL
▪ cleanup テストの最後に実行されるHQL
▪ cases それぞれのテストケース
@HivePdkUnitTest
▪ query テストするHQLクエリ
▪ result その結果
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が作成される
作成された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
作成されたJARファイル のXML
<ClassList> <Class javaname="at.orz.hive.ext.udf.UDFHello" sqlname="hello" /> </ClassList>
CREATE TEMPORARY FUNCTION hello AS 'at.orz.hive.ext.udf.UDFHello';
class-info.xml
class-registration.sql
SessionState生成時に以下のコードで登録される
Class<?> pluginClass = Utilities.getBuiltinUtilsClass();
URL jarLocation =
pluginClass.getProtectionDomain().getCodeSource().getLocation();
add_builtin_resource(ResourceType.JAR, jarLocation.toString());
FunctionRegistry.registerFunctionsFromPluginJar(jarLocation,
pluginClass.getClassLoader());
org.apache.hive.builtins.BuiltinUtilsを検索
JARファイルの場所特定
ADD JAR
CREATE TEMPORARY FUNCTION
SessionStateが生成されるタイミング サーバ
HiveServerHandler生成時
▪ Connection接続時
クライアント
CliSessionState生成時
▪ CliDriver#run
▪ HIVE CLI起動時
▪ HWIセッション生成時
ADD JAR /path/to/your-hive-udf.jar; CREATE TEMPORARY FUNCTION hello AS ‘hoge.UDFHello’;
これらのHookやPDKを使えば..
出来ません!!
auxJarに追加する
設定のhive.aux.jars.path に入っていればOK
▪ HiveCLI起動時にHIVE_AUX_JARS_PATHを指定 ▪ 環境変数
▪ --auxpath オプション
▪ $HIVE_HOME/auxlib/に配置
HiveCLIの場合、
起動時にスクリプトを自動実行する方法ならある。
ファイルの実行
▪ -i オプション
▪ $HIVE_HOME/bin/.hiverc
▪ ~/.hiverc
クエリの実行
hiveServerの場合、
そのオプションは無い
Connection生成時にSessionStateが生成される
SessionState生成時にbuiltin.jarの関数が登録される
▪ builtin.jarはhive-builtin.jar固定っぽい..
[tamtam@tamsvr20 lib]$ jar tf hive-builtins-0.9.0.jar META-INF/ META-INF/MANIFEST.MF org/ org/apache/ org/apache/hive/ 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
HIVE_HOME/ lib/ hive-builtins-*.jar
これが検索される
HTTP RESTサーバ Concurrency
HiveServerはConcurrencyではない
HiveServer2はConcurrency (HIVE-2395)
自動UDF登録対応 設定やLIBはそのまま利用
起動方法: ./hive –service myserver
ソースコードは200行程度
$ 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"]
$ 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"]
$ 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.HadoopDefaultAuthenticator", "hive.exec.plan":"", "hive.metastore.batch.retrieve.max":300, "hive.optimize.index.groupby":false, "datanucleus.cache.level2.type":"none", "hive.enforce.sorting":false, ……….
IN句 INSERT INTO (hive-0.8) Import/Export (hive-0.8)
IMPORT FROM path
EXPORT TABLE xxx TO path
0.6 0.7 0.71
+ any patches -> CDH3
0.8 0.81
+ any patches -> CDH4
0.9 (stable) 0.10 (trunk)
Feature
UDF
str_to_map, covar_samp, corr, percentile_approx
collect_set, ngrams, context_ngrams, reflect
create_union, sentences
json_tuple, parsae_url_tuple
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
Feature UDF
<=>
Between
java_method (reflect)
sort_array
printf
JDBC
Timestamp
executeUpdate( 0.71->OK, 0.8 -> NG)
Etc..
Like UDF
“¥” escapes is incollect.
Simple match is OK.
▪ Single wildcard
▪ Simple range match
Hook機能は普通使わない PerfLOGを見ることはほとんどない PDKは使えない Timestampは使ってはいけない
ドキュメント化されていないものは それなりに理由がある
top related