{"id":26073,"date":"2010-08-12T23:04:03","date_gmt":"2010-08-12T15:04:03","guid":{"rendered":"http:\/\/www.yankay.com\/?p=26073"},"modified":"2010-08-12T23:04:03","modified_gmt":"2010-08-12T15:04:03","slug":"%e7%94%a8mapreduce%e6%9d%a5%e5%81%9a%e5%a5%bd%e5%8f%8b%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"http:\/\/yankay.com\/%e7%94%a8mapreduce%e6%9d%a5%e5%81%9a%e5%a5%bd%e5%8f%8b%e6%8e%a8%e8%8d%90\/","title":{"rendered":"\u7528Map\/Reduce\u6765\u505a\u597d\u53cb\u63a8\u8350"},"content":{"rendered":"
SNS\u7f51\u7ad9\u90fd\u6709\u4e00\u4e2a\u529f\u80fd\uff0c\u5c31\u662f\u597d\u53cb\u63a8\u8350(\u6216\u8005Follower\u63a8\u8350)\u3002\u4f8b\u5982\uff0c\u5728\u4eba\u4eba\u7f51\u4e0a\u51fa\u73b0\u7684\u201c\u4f60\u53ef\u80fd\u8ba4\u8bc6\u7684\u4eba\u201d\u3002\u600e\u4e48\u6765\u5b9e\u73b0\u5462\uff0c\u6709\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u529e\u6cd5\u3002\u5982\u679c\u5c0f\u521a\u548c\u5c0f\u660e\u4e0d\u662f\u597d\u53cb\uff0c\u4f46\u662f\u4ed6\u4eec\u6709\u5f88\u591a\u7684\u5171\u540c\u597d\u53cb\u3002\u90a3\u4e48\u53ef\u4ee5\u8ba4\u4e3a\uff0cA\u548cB\u5f88\u53ef\u80fd\u76f8\u8bc6\u3002<\/p>\n
\u4ece\u56fe\u8bba\u7684\u8bb2\u6cd5\u4e0a\u770b\uff0c\u5c31\u662f\u5148\u5217\u51fa\u4e00\u4e2a\u4eba(\u8bb0\u4e3a\u5c0fA)\u7684\u6240\u6709\u670b\u53cb\u7684\u670b\u53cb\uff0c\u5728\u5bfb\u627e\u5c0fA\u548c\u8fd9\u4e9b\u4eba\u4e4b\u95f4\u6709\u591a\u5c11\u957f\u5ea6\u4e3a2\u7684\u901a\u8def\u3002\u5c06\u8fd9\u4e9b\u901a\u8def\u6570\u6392\u5e8f\uff0c\u5bfb\u627e\u6700\u9ad8\u7684\u90a3\u51e0\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n
\u6240\u4ee5\u6211\u4eec\u7684Map\/Reduce\u7684\u4efb\u52a1\u5c31\u662f\uff1a\u627e\u51fa\u6240\u6709\u4eba\u7684\u5341\u4e2aTop\u201c\u63a8\u8350\u597d\u53cb\u201d\u3002<\/p>\n
\u793e\u4f1a\u5316\u7f51\u7edc\u7684\u56fe\u4e00\u822c\u90fd\u5f88\u7b80\u5355\u3002\u6211\u4eec\u5047\u8bbe\u8f93\u5165\u662f\u6309name\u6392\u5e8f\u7684\u3002<\/p>\n
\n\"ricky\" => [\"jay\", \"peter\", \"phyllis\"]\n\"peter\" => [\"dave\", \"jack\", \"ricky\", \"susan\"]\n<\/pre>\n\u6211\u4eec\u4f7f\u7528\u4e24\u8f6eMap\/Reduce\u4efb\u52a1\u6765\u5b8c\u6210\u8fd9\u4e2a\u64cd\u4f5c\u3002<\/p>\n
\u7b2c\u4e00\u8f6eMR\u4efb\u52a1<\/strong><\/p>\n\u8fd9\u4e2a\u4efb\u52a1\u7684\u76ee\u7684\u662f\u8ba1\u7b97\u6bcf\u4e00\u5bf9\u8ddd\u79bb\u662f2\u7684\u4eba\u4e4b\u95f4\u7684\u901a\u8def\u6570\u3002<\/p>\n
\n- \u5728Map\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5148\u5c06\u6bcf\u5bf9\u670b\u53cb\u505a\u4e00\u4e2a\u7b1b\u5361\u5c14\u4e58\u79ef\uff0c\u8bf4\u7684\u4e0d\u5927\u6e05\u695a\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\n
\n\"ricky\" => [\"jay\", \"john\", \"mitch\"]\n<\/pre>\n\u90a3\u4e48\u7ed3\u679c\u5c31\u662f<\/p>\n
\n [\"jay\", \"john\"], [\"jay\", \"mitch\"], [\"john\", \"mitch\"]\n<\/pre>\n\u4ed6\u4eec\u90fd\u662f\u901a\u8fc7ricky\u7275\u7ebf\u642d\u6865\u8ba4\u8bc6\u7684\u3002\u5c06\u5df2\u7ecf\u662f\u670b\u53cb\u7684\u7ec4\u5408\u7b5b\u9009\u6389\uff0c\u518d\u6392\u597d\u5e8f\u3002\u4f20\u7ed9Reducer\u3002\n<\/li>\n
- \u5728Reduce\u51fd\u6570\u4e2d, \u76f8\u540c\u7684\u7ec4\u5408\u5fc5\u5b9a\u4f1a\u4f20\u7ed9Reducer\u3002\u6240\u4ee5Reducer\u53ea\u8981\u6570\u597d\u6709\u51e0\u4e2a\u76f8\u540c\u7684\u7ec4\u5408\u4f20\u7ed9\u4ed6\u5c31\u884c\u4e86.<\/li>\n<\/ul>\n
Input record ... person -> connection_list\ne.g. \"ricky\" => [\"jay\", \"john\", \"mitch\", \"peter\"]\nalso the connection list is sorted by alphabetical order\n\ndef map(person, connection_list)\n # Compute a cartesian product using nested loops\n for each friend1 in connection_list\n # Eliminate all 2-degree pairs if they already\n # have a one-degree connection\n emit([person, friend1, 0])\n for each friend2 > friend1 in connection_list\n emit([friend1, friend2, 1], 1)\n\ndef partition(key)\n #use the first two elements of the key to choose a reducer\n return super.partition([key[0], key[1]])\n\ndef reduce(person_pair, frequency_list)\n # Check if this is a new pair\n if @current_pair != [person_pair[0], person_pair[1]]\n @current_pair = [person_pair[0], person_pair[1]]\n # Skip all subsequent pairs if these two person\n # already know each other\n @skip = true if person_pair[2] == 0\n\n if !skip\n path_count = 0\n for each count in frequency_list\n path_count += count\n emit(person_pair, path_count)\n\nOutput record ... person_pair => path_count\ne.g. [\"jay\", \"john\"] => 5\n\n<\/pre>\n\u7b2c\u4e8c\u8f6eMR\u4efb\u52a1<\/strong><\/p>\n\u8fd9\u4e00\u8f6e\u7684MR\u4efb\u52a1\u662f\u4e3a\u4e86\u5217\u51fa\u6bcf\u4e2a\u4eba\u8ddd\u79bb\u4e3a2\u7684\u597d\u53cb\uff0c\u67e5\u51fa\u4ed6\u4eec\u76f4\u63a5\u7a76\u7adf\u6709\u51e0\u6761\u8def\u5f84\u3002<\/p>\n
\n- \u5728Map\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u7ec4\u6570\u636e\u91cd\u65b0\u6392\u5217\uff0c\u4fdd\u8bc1\u4e00\u4e2a\u4eba\u4fe1\u606f\u843d\u5728\u4e00\u4e2areducer\u4e0a<\/li>\n
- \u5728Reduce\u51fd\u6570\u4e2d\uff0c\u53ea\u8981\u5c06\u6bcf\u4e2a\u4eba\u7684\u53ef\u80fd\u597d\u53cb\u4e4b\u95f4\u7684\u8def\u5f84\u6570\u6392\u4e2a\u5e8f\u5c31\u53ef\u4ee5\u4e86.<\/li>\n<\/ul>\n
Input record = Output record of round 1\n\ndef map(person_pair, path_count)\n emit([person_pair[0], path_count], person_pair[1])\n\ndef partition(key)\n #use the first element of the key to choose a reducer\n return super.partition(key[0])\n\ndef reduce(connection_count_pair, candidate_list)\n # Check if this is a new person\n if @current_person != connection_count_pair[0]\n emit(@current_person, @top_ten)\n @top_ten = []\n @current_person = connection_count_pair[0]\n\n #Pick the top ten candidates to connect with\n if @top_ten.size < 10\n for each candidate in candidate_list\n @top_ten.append([candidate, connection_count_pair[1]])\n break if @pick_count > 10\n\nOutput record ... person -> candidate_count_list\n\ne.g. \"ricky\" => [[\"jay\", 5], [\"peter\", 3] ...]<\/pre>\nFollower\u63a8\u8350<\/strong>
\n\u5982\u679c\u6211\u60f3\u8981\u505aFollower\u63a8\u8350\u800c\u4e0d\u662f\u597d\u53cb\u63a8\u8350\u600e\u4e48\u529e\u5462\uff1f
\n\u5f88\u7b80\u5355\u3002\u53ea\u8981\u5c06\u7b2c\u4e00\u6b65\u7684MR\u4efb\u52a1\u6539\u4e3a\u6c42\u201cFollow\u5173\u7cfb\u201d\u548c\u201cFollowed\u201d\u5173\u7cfb\u7684\u7b1b\u5361\u5c14\u4e58\u79ef\u5c31\u53ef\u4ee5\u4e86\u3002\u8fd9\u91cc\u5c31\u4e0d\u5217\u4f2a\u7801\u4e86\u3002<\/p>\n