hive undocumented feature

60
@tamtam180

Upload: tamtam180

Post on 16-Jun-2015

7.995 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Hive undocumented feature

@tamtam180

Page 2: Hive undocumented feature

Name: tamtam (タムタム) Twitter : @tamtam180 Blog: http://mt.orz.at/ Works: Software Enginner

SIer -> Video Game, Web Developer -> ???

Page 3: Hive undocumented feature

HiveCLIコマンド Hook機能 PerfLog PDK 自動でUDF登録 HTTP-REST独自サーバ おまけ

Page 4: Hive undocumented feature

./hive

./hive --service cli

Page 5: Hive undocumented feature

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コマンドが使えます

※ドキュメント ありました

Page 6: Hive undocumented feature

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

※ドキュメント ありました

Page 7: Hive undocumented feature

$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と同じ

Page 8: Hive undocumented feature

シェルの実行(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

※ドキュメント ありました

Page 9: Hive undocumented feature

show locks msck …

叩くな危険!

Page 10: Hive undocumented feature
Page 11: Hive undocumented feature

StatementHook HiveDriverRunHook

AnalyzerHook (HiveSemanticAnalyzerHook)

ExecuteWithHookContext ▪ Old: PreExecute, PostExecute

MetadataHook(Listener) MetaStorePreEventListener

MetaStoreEventListener

EndFunctionListener ClientStatsPublisher

Page 12: Hive undocumented feature

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

Page 13: Hive undocumented feature

HiveDriverRunHookを実装 hive.exec.driver.run.hooks version 0.9 ~ Driver#run

HiveDriverRunHook#preDriverRun

▪ HiveDriverRunHookContext

HiveDriverRunHok#postDriverRun

▪ HiveDriverRunHookContext

Page 14: Hive undocumented feature

AbstractSemanticAnalyzerHookを継承 ※ HiveSemanticAnalyzerHookを実装してもNG

hive.semantic.analyzer.hook Driver#compile

preAnalyze ▪ HiveSemanticAnalyzerHookContext

▪ ASTNode

postAnalyze ▪ HiveSemanticAnalyzerHookContext

▪ List<Task<? Extends Serializable>>

Page 15: Hive undocumented feature

ExecuteWithHookContextを実装 hive.exec.pre.hooks

HookContext

hive.exec.post.hooks

HookContext

hive.exec.failure.hooks (hive-0.9)

HookContext

Driver#execute

Page 16: Hive undocumented feature

QueryPlan HiveConf TaskList Inputs Outputs LineageInfo UserGrpupInformation HookType InputPathToContentSummary …

Page 17: Hive undocumented feature

MetaStorePreEventListenerを継承する hive.metastore.pre.event.listeners HMSHandler

Metastore操作前をHook

Page 18: Hive undocumented feature

MetaStoreEventListenerを継承する hive.metastore.event.listeners HMSHandler

Metastore操作後をHook

Page 19: Hive undocumented feature

MetaStoreEndFunctionListenerを継承する hive.metastore.end.function.listeners HMSHandler

Metastoreへの操作の終了時にHook

Page 20: Hive undocumented feature

Databaseの取得/作成/削除/変更 テーブルの(ry インデックスの(ry GRANT などなど..

Page 21: Hive undocumented feature

基本は単体テスト用。 使うことはないと思います。

Page 22: Hive undocumented feature

進捗情報のコールバック ClientStatsPublisherを実装する

ジョブIDとカウンタ一覧が取得できる

hive.client.stats.publishers

Page 23: Hive undocumented feature

DEMO

Page 24: Hive undocumented feature
Page 25: Hive undocumented feature

パフォーマンスログ Hive-0.8 ~ LogLevel=INFO Format=XMLみたいな書式 様々な所に仕込まれている プラガブル

Page 26: Hive undocumented feature

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

Page 27: Hive undocumented feature

(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>

Page 28: Hive undocumented feature

PerfLoggerを継承する 以下のメソッドをオーバーライドする

PerfLogBegin

PerfLogEnd

▪ Must returns duration(endTime - startTime).

close

▪ Called before postDriverRun(Hook) at Driver#run

hive.exec.perf.logger

Page 29: Hive undocumented feature

Plugin Development Kit

Page 30: Hive undocumented feature

Hive-0.8~ UDF関数の追加を楽にするためのSDK hive-builtins.jarもこれで作られている

このせいで…後述

Page 31: Hive undocumented feature

ディレクトリ構成

Project-Root build.xml src/ java-source-files. test/ setup.sql cleanup.sql any datafiles needed by your tests.

Page 32: Hive undocumented feature

アノテーションの指定

@UDFType

▪ deterministic

▪ stateful

▪ distinctLike

@Description

▪ name 関数名

▪ value 説明概要

▪ extended 説明

Page 33: Hive undocumented feature

アノテーションの指定

@HivePdkUnitTests

▪ setup テストの最初の実行されるHQL

▪ cleanup テストの最後に実行されるHQL

▪ cases それぞれのテストケース

@HivePdkUnitTest

▪ query テストするHQLクエリ

▪ result その結果

Page 34: Hive undocumented feature

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が作成される

Page 35: Hive undocumented feature

作成された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

Page 36: Hive undocumented feature

作成された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

Page 37: Hive undocumented feature

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

Page 38: Hive undocumented feature

SessionStateが生成されるタイミング サーバ

HiveServerHandler生成時

▪ Connection接続時

クライアント

CliSessionState生成時

▪ CliDriver#run

▪ HIVE CLI起動時

▪ HWIセッション生成時

Page 39: Hive undocumented feature
Page 40: Hive undocumented feature

ADD JAR /path/to/your-hive-udf.jar; CREATE TEMPORARY FUNCTION hello AS ‘hoge.UDFHello’;

Page 41: Hive undocumented feature

これらのHookやPDKを使えば..

Page 42: Hive undocumented feature

出来ません!!

Page 43: Hive undocumented feature

auxJarに追加する

設定のhive.aux.jars.path に入っていればOK

▪ HiveCLI起動時にHIVE_AUX_JARS_PATHを指定 ▪ 環境変数

▪ --auxpath オプション

▪ $HIVE_HOME/auxlib/に配置

Page 44: Hive undocumented feature

HiveCLIの場合、

起動時にスクリプトを自動実行する方法ならある。

ファイルの実行

▪ -i オプション

▪ $HIVE_HOME/bin/.hiverc

▪ ~/.hiverc

クエリの実行

Page 45: Hive undocumented feature

hiveServerの場合、

そのオプションは無い

Connection生成時にSessionStateが生成される

SessionState生成時にbuiltin.jarの関数が登録される

▪ builtin.jarはhive-builtin.jar固定っぽい..

Page 46: Hive undocumented feature

[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

これが検索される

Page 47: Hive undocumented feature
Page 48: Hive undocumented feature

HTTP RESTサーバ Concurrency

HiveServerはConcurrencyではない

HiveServer2はConcurrency (HIVE-2395)

自動UDF登録対応 設定やLIBはそのまま利用

起動方法: ./hive –service myserver

ソースコードは200行程度

Page 49: Hive undocumented feature

$ 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"]

Page 50: Hive undocumented feature

$ 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"]

Page 51: Hive undocumented feature

$ 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, ……….

Page 52: Hive undocumented feature
Page 53: Hive undocumented feature

IN句 INSERT INTO (hive-0.8) Import/Export (hive-0.8)

IMPORT FROM path

EXPORT TABLE xxx TO path

Page 54: Hive undocumented feature

0.6 0.7 0.71

+ any patches -> CDH3

0.8 0.81

+ any patches -> CDH4

0.9 (stable) 0.10 (trunk)

Page 55: Hive undocumented feature

Feature

UDF

str_to_map, covar_samp, corr, percentile_approx

collect_set, ngrams, context_ngrams, reflect

create_union, sentences

json_tuple, parsae_url_tuple

Page 56: Hive undocumented feature

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

Page 57: Hive undocumented feature

Feature UDF

<=>

Between

java_method (reflect)

sort_array

printf

Page 58: Hive undocumented feature

JDBC

Timestamp

executeUpdate( 0.71->OK, 0.8 -> NG)

Etc..

Like UDF

“¥” escapes is incollect.

Simple match is OK.

▪ Single wildcard

▪ Simple range match

Page 59: Hive undocumented feature
Page 60: Hive undocumented feature

Hook機能は普通使わない PerfLOGを見ることはほとんどない PDKは使えない Timestampは使ってはいけない

ドキュメント化されていないものは それなりに理由がある