{"id":173062,"date":"2020-03-02T10:47:11","date_gmt":"2020-03-02T02:47:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173062"},"modified":"2020-02-24T09:52:13","modified_gmt":"2020-02-24T01:52:13","slug":"pig-keyword-matching","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/pig-keyword-matching.html","title":{"rendered":"Pig \u5b9e\u73b0\u5173\u952e\u8bcd\u5339\u914d"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPig\u662f\u4e00\u79cd\u6570\u636e\u6d41\u8bed\u8a00\u548c\u8fd0\u884c\u73af\u5883\uff0c\u7528\u4e8e\u68c0\u7d22\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u3002\u4e3a\u5927\u578b\u6570\u636e\u96c6\u7684\u5904\u7406\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\u3002Pig\u5305\u62ec\u4e24\u90e8\u5206\uff1a\u4e00\u662f\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u6d41\u7684\u8bed\u8a00\uff0c\u79f0\u4e3aPig Latin\uff1b\u4e8c\u662f\u7528\u4e8e\u8fd0\u884cPig Latin\u7a0b\u5e8f\u7684\u6267\u884c\u73af\u5883\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1. \u95ee\u9898\u63cf\u8ff0<\/strong><\/div>\n

\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\u7684app\uff0c\u4e14key\u503c\u4e3aapp\u7684\u540d\u79f0\uff0cvalue\u503c\u4e3aapp\u4f7f\u7528\u4fe1\u606f\u3002\u73b0\u5728\u8981\u5f97\u5230\u4e00\u4efd\u5339\u914d\u4e0a\u8d2d\u7269\u7c7bapp\u652f\u4ed8\u5b9d|\u4eac\u4e1c|\u6dd8\u5b9d|\u5929\u732b\u7684\u7528\u6237\u540d\u5355\uff1bMapReduce \u89e3\u51b3\u529e\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\npublic static class M extends Mapper {\r\n    Text text = new Text();\r\n    \r\n    @SuppressWarnings(\"unchecked\")\r\n    @Override\r\n    protected void map(String key, Pair value, Context context) throws IOException, InterruptedException {\r\n        Map data = value.fields.data;\r\n        \r\n        String dvc = data.get(\"dvc\").toString();\r\n        Map appInstall = (Map) data.get(\"appInstall\");\r\n        Map appUse = (Map) data.get(\"appUse\");\r\n        \r\n        for(String app: appInstall.keySet()) {\r\n            if(app.matches(\"\u652f\u4ed8\u5b9d|\u4eac\u4e1c|\u6dd8\u5b9d|\u5929\u732b\")) {\r\n                text.set(appInstall.keySet().toString());\r\n                context.write(dvc, text);\r\n                return;\r\n            }\r\n        }\r\n        \r\n        for(String app: appUse.keySet()) {\r\n            if(app.matches(\"\u652f\u4ed8\u5b9d|\u4eac\u4e1c|\u6dd8\u5b9d|\u5929\u732b\")) {\r\n                text.set(appUse.keySet().toString());\r\n                context.write(dvc, text);\r\n                return;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n

\u4f46\u662f\uff0c\u5982\u679c\u8981\u5339\u914d\u6e38\u620f\u7c7b\u7684app\u3001\u91d1\u878d\u7c7b\u7684app\u7c7b\u5462\uff1f\u5982\u679c\u5339\u914d\u5173\u952e\u8bcd\u53d1\u751f\u4e86\u53d8\u5316\u5462\uff1f\u663e\u7136\uff0c\u6211\u4eec\u5e94\u8be5\u5c06\u5339\u914d\u5173\u952e\u8bcd\u5f00\u653e\u6210API\uff0c\u53ef\u4ee5\u81ea\u7531\u5730\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8fd9\u65f6\uff0cpig\u6d3e\u4e0a\u4e86\u7528\u573a\u3002<\/p>\n

2. Bag\u6b63\u5219\u5339\u914d<\/strong><\/div>\n
\r\nA = load '\/\/' 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>\n
3. \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}]}}