{"id":1428,"date":"2016-08-06T09:37:50","date_gmt":"2016-08-06T09:37:50","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1428"},"modified":"2016-08-06T09:37:50","modified_gmt":"2016-08-06T09:37:50","slug":"user-defined-function-pig","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/user-defined-function-pig\/","title":{"rendered":"User Defined Function Pig"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n package com.mugil.pig;\r\n\r\nimport java.io.IOException;\r\n\r\nimport org.apache.pig.FilterFunc;\r\nimport org.apache.pig.data.Tuple;\r\n\r\npublic class FilterType  extends FilterFunc {\r\n\t\r\n\t@Override\r\n\tpublic Boolean exec(Tuple tuple) throws IOException {\r\n\t\t\r\n\t\tif(tuple == null || tuple.size() == 0)\t\t\r\n\t\t return false;\r\n\t\t\r\n\t\ttry {\r\n\t\t\tObject obj = tuple.get(0);\r\n\t\t\t\r\n\t\t\tif(obj == null)\t\t\t\r\n\t\t\t return false;\r\n\t\t\t\r\n\t\t\tString Type = (String)obj;\r\n\t\t\t\r\n\t\t\tif(Type.equals(&quot;Kitchen&quot;))\r\n\t\t\t return true;\r\n\t\t\t\r\n\t\t} catch (Exception e) {\r\n\t\t\tthrow new IOException(&quot;Caught exception processing input row &quot; + e.getMessage(), e);\r\n\t\t}\r\n\t\t\t\r\n\t\treturn false;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Registering UDF Function<\/strong><\/p>\n<pre>\r\ngrunt> REGISTER  \/usr\/local\/pig-0.15.0\/FilterByType3.jar;                  \r\ngrunt> DEFINE FilterType com.mugil.pig.FilterType();         \r\ngrunt> filtered_records = FILTER records BY FilterType(Type);\r\ngrunt> DUMP filtered_records;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package com.mugil.pig; import java.io.IOException; import org.apache.pig.FilterFunc; import org.apache.pig.data.Tuple; public class FilterType extends FilterFunc { @Override public Boolean exec(Tuple tuple) throws IOException { if(tuple == null || tuple.size() == 0) return false; try { Object obj = tuple.get(0); if(obj == null) return false; String Type = (String)obj; if(Type.equals(&quot;Kitchen&quot;)) return true; } catch (Exception e) { throw&hellip; <a href=\"https:\/\/codethataint.com\/blog\/user-defined-function-pig\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[],"class_list":["post-1428","post","type-post","status-publish","format-standard","hentry","category-pig"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1428"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1428\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1428\/revisions\/1429"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}