' using org.apache.pig.piggybank.storage.avro.AvroStorage();\r\n-- A: {key: chararray,value: (fields: (data: map[]))}\r\n\r\nB = foreach A generate value.fields.data#'dvc' as dvc, value.fields.data#'appInstall' as ins:map[], value.fields.data#'appUse' as use:map[];\r\n-- B: {dvc: bytearray,ins: map[],use: map[]}\r\n\r\nC = foreach B generate dvc, KEYSET(ins) as insk, KEYSET(use) as usek;\r\n-- C: {dvc: bytearray,insk: {(chararray)},usek: {(chararray)}}\r\n<\/to><\/path><\/pre>\n\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cload \u6570\u636e\u8f6c\u6362\u5f97\u5230bag\u7c7b\u578b\u7684app-set\uff08insk\u4e0eusek\uff09\uff1b\u4f46\u662f\uff0c\u5e94\u5982\u4f55\u904d\u5386bag\u4e2d\u7684tuple\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u505a\u5339\u914d\u5462\uff1f\u7b54\u6848\u662fUDF\u3002<\/p>\n
Apache DataFu Pig \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684UDF\uff0c\u5176\u4e2d\u5173\u4e8ebags\u7684UDF\u53ef\u4ee5\u53c2\u770b\u8fd9\u91cc\u3002TupleFromBag \u63d0\u4f9b\u6839\u636eindex\u4ecebag\u63d0\u53d6tuple\uff0c\u652f\u6301\u4e09\u4e2a\u8f93\u5165\u53c2\u6570\u3002\u4f9d\u846b\u82a6\u753b\u74e2\uff0c\u904d\u5386bag\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684UDF\u5982\u4e0b\uff1a<\/p>\n
\r\npackage com.pig.udf.bag;\r\n\r\n\/**\r\n * This UDF will return true if one tuple from a bag matches regex.\r\n * \r\n * There are two input parameter:\r\n * 1. DataBag\r\n * 2. Regex String\r\n *\/\r\npublic class BagMatchRegex extends FilterFunc {\r\n\r\n @Override\r\n public Boolean exec(Tuple tinput) throws IOException {\r\n\r\n try{\r\n DataBag samples = (DataBag) tinput.get(0);\r\n String regex = (String) tinput.get(1);\r\n for (Tuple tuple : samples) {\r\n if(((String) tuple.get(0)).matches(regex)){\r\n return true;\r\n }\r\n }\r\n }\r\n catch (Exception e) {\r\n return false;\r\n }\r\n return false;\r\n }\r\n}\r\n<\/pre>\n\u5176\u4e2d\uff0cFilterFunc\u4e3a\u8fc7\u6ee4UDF\u7684\u57fa\u7c7b\uff0c\u7ee7\u627f\u4e8eEvalFunc\uff0c\u5373exec(Tuple tinput)\u7684\u8fd4\u56de\u503c\u5fc5\u4e3aBoolean\u7c7b\u578b\u3002bag\u6b63\u5219\u5339\u914d\u7684pig\u811a\u672c\u5982\u4e0b\uff1a<\/p>\n\r\nREGISTER ..\/piglib\/udf-0.0.1-SNAPSHOT-jar-with-dependencies.jar\r\ndefine BagMatchRegex com.pig.udf.bag.BagMatchRegex();\r\nA = load '\/user\/..\/current\/*.avro' using org.apache.pig.piggybank.storage.avro.AvroStorage();\r\nB = foreach A generate value.fields.data#'dvc' as dvc, value.fields.data#'appInstall' as ins:map[], value.fields.data#'appUse' as use:map[];\r\nC = foreach B generate dvc, KEYSET(ins) as insk, KEYSET(use) as usek;\r\nD = filter C by BagMatchRegex(insk, '\u652f\u4ed8\u5b9d|\u4eac\u4e1c|\u6dd8\u5b9d|\u5929\u732b') or BagMatchRegex(usek, '\u652f\u4ed8\u5b9d|\u4eac\u4e1c|\u6dd8\u5b9d|\u5929\u732b');\r\n<\/pre>\n3. \u4f18\u5316<\/strong><\/div>\n\u8fd8\u6709\u6ca1\u6709\u53ef\u4ee5\u505a\u4f18\u5316\u7684\u5730\u65b9\u5462\uff1f\u6211\u4eec\u5148\u6765\u770b\u770bpig\u4e2d\u7684KEYSET\u5b9e\u73b0\uff1a<\/p>\n
\r\npackage org.apache.pig.builtin;\r\n\r\npublic class KEYSET extends EvalFunc {\r\n private static final TupleFactory TUPLE_FACTORY = TupleFactory.getInstance();\r\n\r\n @SuppressWarnings(\"unchecked\")\r\n @Override\r\n public DataBag exec(Tuple input) throws IOException {\r\n if(input == null || input.size() == 0) {\r\n return null;\r\n }\r\n\r\n Map m = null;\r\n \/\/ Input must be of type Map. This is verified at compile time\r\n m = (Map)(input.get(0));\r\n if(m == null) {\r\n return null;\r\n }\r\n\r\n DataBag bag = new NonSpillableDataBag(m.size());\r\n for (String s : m.keySet()) {\r\n Tuple t = TUPLE_FACTORY.newTuple(s);\r\n bag.add(t);\r\n }\r\n\r\n return bag;\r\n }\r\n ...\r\n}\r\n<\/databag><\/pre>\n\u9700\u8981\u6307\u51fa\u7684\u4e00\u70b9\u2014\u2014pig\u7684map\u6570\u636e\u7c7b\u578b\u662f\u7531Java\u7c7bMap\u5b9e\u73b0\u7684\u3002\u4eceKEYSET\u6e90\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\u5728\u8c03\u7528\u65f6\u5df2\u7ecf\u5c06map\u904d\u5386\u4e86\u4e00\u6b21\uff0c\u7136\u540e\u5728\u8c03\u7528BagMatchRegex\u65f6\u53c8\u9700\u8981\u5c06key-set\u7684bag\u518d\u904d\u5386\u4e00\u6b21\u3002\u5176\u5b9e\uff0c\u5b8c\u5168\u53ef\u4ee5\u53ea\u7528\u4e00\u6b21\u904d\u5386\u505amap-key\u503c\u7684\u6b63\u5219\u5339\u914d\uff1a<\/p>\n\r\npackage com.pig.udf.map;\r\n\r\n\/**\r\n * This UDF will return true if map's key matches regex.\r\n * \r\n * There are two input parameter:\r\n * 1. Map\r\n * 2. Regex String\r\n *\/\r\npublic class KeyMatchRegex extends FilterFunc {\r\n \r\n @SuppressWarnings(\"unchecked\")\r\n @Override\r\n public Boolean exec(Tuple input) throws IOException\r\n {\r\n try{\r\n Map m = null;\r\n \/\/ Input must be of type Map. This is verified at compile time\r\n m = (Map)(input.get(0));\r\n \r\n String regex = (String) input.get(1);\r\n for (String key : m.keySet()) {\r\n if(key.matches(regex)){\r\n return true;\r\n }\r\n }\r\n }\r\n catch (Exception e) {\r\n return false;\r\n }\r\n return false;\r\n }\r\n}\r\n<\/pre>\n<\/boolean><\/p>\n","protected":false},"excerpt":{"rendered":"
\u6536\u96c6\u65e5\u5fd7avro\u6570\u636e\u4e2d\u6709\u4e24\u4e2aMap\u5b57\u6bb5appInstall\u3001appUse\u5206\u522b\u8868\u793a\u5df2\u5b89\u88c5\u7684app\u3001\u6b63\u5728\u4f7f\u7528\u7684ap […]<\/p>\n","protected":false},"author":1470,"featured_media":173021,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-173062","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173062"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173062\/revisions"}],"predecessor-version":[{"id":173069,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173062\/revisions\/173069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/173021"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}