{"id":4782,"date":"2020-02-22T08:46:04","date_gmt":"2020-02-22T00:46:04","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4782"},"modified":"2020-02-22T08:46:04","modified_gmt":"2020-02-22T00:46:04","slug":"hive-udf%e7%9a%84%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4782.html","title":{"rendered":"Hive UDF\u7684\u7f16\u5199"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6709\u65f6\u5019Hive\u63d0\u4f9b\u7684\u5b98\u65b9\u51fd\u6570\u4e0d\u80fd\/\u5f88\u96be\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u91c7\u7528Hive\u63d0\u4f9b\u7684\u66f4\u52a0\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\uff1aUDF\uff08User Defined Function, \u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\uff09\u3002\u5728\u8fd9\u91cc\u7b80\u5355\u6574\u7406\u4e00\u4e0bHive\u4e2d\u7f16\u5199UDF\u7684\u65b9\u6cd5\uff0c\u65b9\u4fbf\u4ee5\u540e\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h5 class=\"wp-block-heading\">\u5e38\u89c1\u76843\u79cd Hive \u81ea\u5b9a\u4e49\u51fd\u6570<\/h5>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\"><strong>\uff081\uff09 UDF(user-defined function) \u4e00\u8fdb\u4e00\u51fa\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\uff0c\u8f93\u51fa\u4e00\u4e2a\u5904\u7406\u540e\u7684\u6570\u636e\u3002<\/strong>\u8bb8\u591aHive\u5185\u7f6e\u5b57\u7b26\u4e32\uff0c\u6570\u5b66\u51fd\u6570\uff0c\u65f6\u95f4\u51fd\u6570\u90fd\u662f\u8fd9\u79cd\u7c7b\u578b\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7f16\u5199\u5bf9\u5e94\u529f\u80fd\u7684\u5904\u7406\u51fd\u6570\u5c31\u80fd\u6ee1\u8db3\u9700\u6c42\u3002\u5982\uff1aconcat, split, length ,rand\u7b49\u3002\u8fd9\u79cdUDF\u4e3b\u8981\u6709\u4e24\u79cd\u5199\u6cd5\uff1a\u7ee7\u627f\u5b9e\u73b0 UDF\u7c7b \u548c \u7ee7\u627fGenericUDF\u7c7b(\u901a\u7528UDF)\u3002<\/p>\n\n\n\n<p><strong>\uff082\uff09 UDAF(user-defined aggregate function) \u591a\u8fdb\u4e00\u51fa\uff0c\u5c5e\u4e8e\u805a\u5408\u51fd\u6570\uff0c\u7c7b\u4f3c\u4e8ecount\u3001sum\u7b49\u51fd\u6570\u3002<\/strong>\u4e00\u822c\u914d\u5408group by\u4f7f\u7528\u3002\u4e3b\u8981\u7528\u4e8e\u7d2f\u52a0\u64cd\u4f5c\uff0c\u5e38\u89c1\u7684\u51fd\u6570\u6709max, min, count, sum, collect_set\u7b49\u3002\u8fd9\u79cdUDF\u4e3b\u8981\u6709\u4e24\u79cd\u5199\u6cd5\uff1a\u7ee7\u627f\u5b9e\u73b0 UDAF\u7c7b \u548c \u7ee7\u627f\u5b9e\u73b0 AbstractGenericUDAFResolver\u7c7b \u3002<\/p>\n\n\n\n<p><strong>\uff083\uff09 UDTF(user-defined table function) \u4e00\u8fdb\u591a\u51fa\uff0c\u5c06\u8f93\u5165\u7684\u4e00\u884c\u6570\u636e\u4ea7\u751f\u591a\u884c\u6570\u636e\u6216\u8005\u5c06\u4e00\u5217\u6253\u6210\u591a\u5217\u3002<\/strong>\u5982explode\u51fd\u6570\u901a\u5e38\u914d\u5408lateral view\u4f7f\u7528\uff0c\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u529f\u80fd\u3002parse_url_tuple\u5c06\u4e00\u5217\u8f6c\u4e3a\u591a\u5217\u3002<\/p>\n\n\n\n<p>\u5728\u51b3\u5b9a\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u770b\u770bHive\u4e2d\u5df2\u7ecf\u6709\u4e86\u54ea\u4e9b\u51fd\u6570\u53ca\u5176\u529f\u80fd\uff1a<\/p>\n\n\n\n<p><code>show functions; --\u67e5\u770b\u5df2\u6709\u51fd\u6570<br>describe function concat; --\u67e5\u770b\u51fd\u6570\u7528\u6cd5<br>describe function extended concat; --\u67e5\u770b\u51fd\u6570\u7684\u7528\u6cd5\u548c\u793a\u4f8b<\/code><\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u901a\u7528UDF\u7684\u7f16\u5199\u793a\u4f8b\u53ca\u8bf4\u660e<\/strong><\/h5>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\">\u901a\u7528UDF\u7684\u5b9e\u73b0\u5f88\u7b80\u5355\uff0c<strong>\u53ea\u9700\u8981\u7ee7\u627fUDF\uff0c\u7136\u540e\u5b9e\u73b0evaluate()\u65b9\u6cd5\u5c31\u884c\u4e86\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.apache.commons.lang.StringUtils; \/\/\u5f15\u7528\u7684\u5176\u5b83\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\n\nimport org.apache.hadoop.hive.ql.exec.UDF; \/\/\u9700\u8981\u7ee7\u627f\u7684\u7c7b\nimport org.apache.hadoop.io.Text; \/\/Hadoop\u7684Text\u7c7b\u578b\u8868\u793a\u6587\u672c\n\npublic class Strip extends UDF {\n    private Text result = new Text();\n\n    public Text evaluate(Text str) {\n        if (str == null) {\n            return null;\n        }\n        result.set(StringUtils.strip(str.toString()));\n        return result;\n    }\n\n    public Text evaluate(Text str, String stripChars) {\n        if (str == null) {\n            return null;\n        }\n        result.set(StringUtils.strip(str.toString(), stripChars));\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\">\u8981\u70b9\u5c31 2 \u4e2a\uff1a<br>\uff081\uff09 \u7ee7\u627f\u7c7b org.apache.hadoop.hive.ql.exec.UDF \uff1b<br>\uff082\uff09 \u81f3\u5c11\u5b9e\u73b0\u4e00\u4e2a evaluate() \u65b9\u6cd5\uff0c evaluate \u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u4e2a\u6570\u548c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p>evaluate()\u65b9\u6cd5\u652f\u6301\u7684\u7c7b\u578b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int evaluate();\npublic int evaluate(int a);\npublic double evaluate(int a, double b);\npublic String evaluate(String a, int b, Text c);\npublic Text evaluate(String a);\npublic String evaluate(List a);\npublic List evaluate(List a);\npublic Map&lt;Integer, String> evaluate(Map&lt;Integer,String> a);<\/code><\/pre>\n\n\n\n<p>\u7f16\u5199\u3001\u6d4b\u8bd5\u597d\u4e86\u4e4b\u540e\u6253\u6210\u4e00\u4e2ajar\u5305\uff0c\u7136\u540e\u4e0a\u4f20\u4f7f\u7528\u5373\u53ef\uff0c\u5177\u4f53\u7684\u6839\u636e\u5404\u81ea\u642d\u5efa\u7684\u73af\u5883\u5bf9\u5e94\u624b\u518c\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u8c03\u7528\u8fc7\u7a0b\uff08\u6dfb\u52a0\u3001\u521b\u5efa\u3001\u8c03\u7528\uff09<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li>\u2460\u6dfb\u52a0 jar \u5305\uff08\u5728Hive\u547d\u4ee4\u884c\u91cc\u9762\u6267\u884c\uff09<\/li><\/ul>\n\n\n\n<p><code>hive&gt; add jar \/path\/to\/StripUDF.jar;<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u2461\u521b\u5efa\u4e34\u65f6\u51fd\u6570<\/li><\/ul>\n\n\n\n<p><code>hive&gt; create temporary function strip as 'com.ixyzero.hive.udf.StripUDF';<\/code><\/p>\n\n\n\n<p>\u4e34\u65f6\u51fd\u6570\u7684\u7f3a\u70b9\u662f\u5728\u4e0b\u6b21\u542f\u52a8Hive\u96c6\u7fa4\u65f6\u81ea\u52a8\u5931\u6548\uff08\u53ef\u4ee5\u6bcf\u6b21\u5728\u7528\u7684\u65f6\u5019\u90fd\u5148add\u518dcreate\uff09\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u6c38\u4e45\u51fd\u6570\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u62c5\u5fc3\u5931\u6548\u7684\u95ee\u9898\u4e86\uff0c\u95ee\u9898\u5728\u4e8e\u521b\u5efa\u8fc7\u7a0b\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u4fee\u6539\u6e90\u7801\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u2462\u8c03\u7528<\/li><\/ul>\n\n\n\n<p><code>hive&gt; select strip('  space will be strip ') as result;<\/code><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>HIVE UDF\u51fd\u6570\u7f16\u5199<br> <a href=\"https:\/\/github.com\/delongwu\/techdoc\/wiki\/HIVE-UDF%E5%87%BD%E6%95%B0%E7%BC%96%E5%86%99\">https:\/\/github.com\/delongwu\/techdoc\/wiki\/HIVE-UDF%E5%87%BD%E6%95%B0%E7%BC%96%E5%86%99<\/a><\/p>\n\n\n\n<p>Hive UDF\u6559\u7a0b\uff08\u4e00\uff09<br> <a href=\"https:\/\/blog.csdn.net\/u010376788\/article\/details\/50532166\">https:\/\/blog.csdn.net\/u010376788\/article\/details\/50532166<\/a><br>Hive UDF\u6559\u7a0b\uff08\u4e8c\uff09<br> <a href=\"https:\/\/blog.csdn.net\/u010376788\/article\/details\/50537984\">https:\/\/blog.csdn.net\/u010376788\/article\/details\/50537984<\/a><br>Hive UDF\u6559\u7a0b\uff08\u4e09\uff09<br> <a href=\"https:\/\/blog.csdn.net\/u010376788\/article\/details\/50540633\">https:\/\/blog.csdn.net\/u010376788\/article\/details\/50540633<\/a><\/p>\n\n\n\n<p>\u5b98\u65b9\u6587\u6863<br> <a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+UDF<\/a><br> <a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/HivePlugins\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/HivePlugins<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/blog.matthewrathbone.com\/2013\/08\/10\/guide-to-writing-hive-udfs.html\">https:\/\/blog.matthewrathbone.com\/2013\/08\/10\/guide-to-writing-hive-udfs.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/snowplowanalytics.com\/blog\/2013\/02\/08\/writing-hive-udfs-and-serdes\/\">https:\/\/snowplowanalytics.com\/blog\/2013\/02\/08\/writing-hive-udfs-and-serdes\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/dzone.com\/articles\/writing-custom-hive-udf-andudaf\">https:\/\/dzone.com\/articles\/writing-custom-hive-udf-andudaf<\/a><\/p>\n\n\n\n<p>Hive\u7b80\u6613\u6559\u7a0b &#8211; \u81ea\u5b9a\u4e49UDF<br> <a href=\"https:\/\/www.jianshu.com\/p\/7f8329443aae\">https:\/\/www.jianshu.com\/p\/7f8329443aae<\/a><\/p>\n\n\n\n<p>Hive\u5165\u95e8&#8211;3.UDF\u7f16\u5199\u4e0e\u4f7f\u7528<br> <a href=\"https:\/\/blog.csdn.net\/u014726937\/article\/details\/51985177\">https:\/\/blog.csdn.net\/u014726937\/article\/details\/51985177<\/a><\/p>\n\n\n\n<p>hive udf\u5f00\u53d1\u8d85\u8be6\u7ec6\u624b\u628a\u624b\u6559\u7a0b<br> <a href=\"https:\/\/blog.csdn.net\/bitcarmanlee\/article\/details\/51249260\">https:\/\/blog.csdn.net\/bitcarmanlee\/article\/details\/51249260<\/a><\/p>\n\n\n\n<p>Hive\u6dfb\u52a0\u81ea\u5b9a\u4e49UDF\u51fd\u6570<br> <a href=\"https:\/\/liam-blog.ml\/2016\/04\/11\/add-udf-to-hive\/\">https:\/\/liam-blog.ml\/2016\/04\/11\/add-udf-to-hive\/<\/a><\/p>\n\n\n\n<p>Hive\u57fa\u7840\u4e4bUDF\u7f16\u7a0b<br> <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/58072588\">https:\/\/zhuanlan.zhihu.com\/p\/58072588<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6709\u65f6\u5019Hive\u63d0\u4f9b\u7684\u5b98\u65b9\u51fd\u6570\u4e0d\u80fd\/\u5f88\u96be\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u91c7\u7528Hive\u63d0\u4f9b\u7684\u66f4\u52a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,7],"tags":[1261,1561,1560,1283],"class_list":["post-4782","post","type-post","status-publish","format-standard","hentry","category-database","category-programing","tag-hive","tag-udaf","tag-udf","tag-udtf"],"views":7005,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4782"}],"version-history":[{"count":3,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4782\/revisions"}],"predecessor-version":[{"id":4789,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4782\/revisions\/4789"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}