{"id":1011731,"date":"2024-12-27T11:32:36","date_gmt":"2024-12-27T03:32:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011731.html"},"modified":"2024-12-27T11:32:39","modified_gmt":"2024-12-27T03:32:39","slug":"hadoop%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8cpython%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011731.html","title":{"rendered":"hadoop\u5982\u4f55\u8fd0\u884cPython\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085741\/97a8dbb9-8da3-4788-9321-5ecc8299dd04.webp\" alt=\"hadoop\u5982\u4f55\u8fd0\u884cPython\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Hadoop\u8fd0\u884cPython\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7Hadoop Streaming\u3001Apache Spark\u6216\u5c06Python\u4ee3\u7801\u6253\u5305\u6210\u53ef\u6267\u884c\u7684Hadoop\u4efb\u52a1\u7b49\u65b9\u5f0f\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Hadoop Streaming<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f7f\u7528\u4efb\u4f55\u53ef\u6267\u884c\u7684\u811a\u672c\u4f5c\u4e3aMapReduce\u4f5c\u4e1a\u7684mapper\u548creducer\u3002<strong>Apache Spark<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u80fd\u529b\uff0c\u5e76\u4e14\u5bf9Python\u7684\u652f\u6301\u66f4\u52a0\u53cb\u597d\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<strong>Hadoop Pipes<\/strong>\u6216\u5176\u4ed6\u5de5\u5177\u5c06Python\u4ee3\u7801\u4e0eHadoop\u96c6\u6210\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001HADOOP STREAMING<\/p>\n<\/p>\n<p><p>Hadoop Streaming\u662fHadoop\u81ea\u5e26\u7684\u4e00\u4e2a\u5b9e\u7528\u5de5\u5177\uff0c\u5b83\u652f\u6301\u4f7f\u7528\u4efb\u4f55\u53ef\u6267\u884c\u7a0b\u5e8f\uff08\u5305\u62ecPython\u811a\u672c\uff09\u4f5c\u4e3aMapReduce\u4f5c\u4e1a\u7684mapper\u548creducer\u3002\u4f7f\u7528Hadoop Streaming\u8fd0\u884cPython\u6587\u4ef6\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51c6\u5907Python\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u7f16\u5199\u4e24\u4e2aPython\u811a\u672c\uff1a\u4e00\u4e2a\u7528\u4e8emap\u4efb\u52a1\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8ereduce\u4efb\u52a1\u3002map\u811a\u672c\u8d1f\u8d23\u8bfb\u53d6\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u952e\u503c\u5bf9\uff1breduce\u811a\u672c\u8d1f\u8d23\u5bf9map\u8f93\u51fa\u7684\u952e\u503c\u5bf9\u8fdb\u884c\u805a\u5408\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u7b80\u5355\u7684word count\u7a0b\u5e8f\u7684mapper\u548creducer\u811a\u672c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mapper.py<\/p>\n<p>import sys<\/p>\n<p>for line in sys.stdin:<\/p>\n<p>    for word in line.strip().split():<\/p>\n<p>        print(f&quot;{word}\\t1&quot;)<\/p>\n<h2><strong>reducer.py<\/strong><\/h2>\n<p>import sys<\/p>\n<p>from collections import defaultdict<\/p>\n<p>count_dict = defaultdict(int)<\/p>\n<p>for line in sys.stdin:<\/p>\n<p>    word, count = line.strip().split(&#39;\\t&#39;)<\/p>\n<p>    count_dict[word] += int(count)<\/p>\n<p>for word, count in count_dict.items():<\/p>\n<p>    print(f&quot;{word}\\t{count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4ea4Hadoop Streaming\u4f5c\u4e1a<\/strong><\/p>\n<\/p>\n<p><p>\u5c06Python\u811a\u672c\u4e0a\u4f20\u5230HDFS\uff08Hadoop Distributed File System\uff09\uff0c\u7136\u540e\u4f7f\u7528Hadoop Streaming\u547d\u4ee4\u63d0\u4ea4\u4f5c\u4e1a\u3002\u5047\u8bbe\u60a8\u7684\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728HDFS\u7684<code>\/user\/input<\/code>\u8def\u5f84\u4e0b\uff0c\u8f93\u51fa\u7ed3\u679c\u5c06\u5b58\u50a8\u5728<code>\/user\/output<\/code>\u8def\u5f84\u4e2d\u3002\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">hadoop jar \/path\/to\/hadoop-streaming.jar \\<\/p>\n<p>  -input \/user\/input \\<\/p>\n<p>  -output \/user\/output \\<\/p>\n<p>  -mapper &quot;python mapper.py&quot; \\<\/p>\n<p>  -reducer &quot;python reducer.py&quot; \\<\/p>\n<p>  -file mapper.py \\<\/p>\n<p>  -file reducer.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u547d\u4ee4\u4e2d\uff0c<code>-file<\/code>\u9009\u9879\u7528\u4e8e\u5c06\u672c\u5730\u7684Python\u811a\u672c\u4e0a\u4f20\u5230Hadoop\u8282\u70b9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/p>\n<\/p>\n<p><p>\u4f5c\u4e1a\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Hadoop\u547d\u4ee4\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">hdfs dfs -cat \/user\/output\/part-00000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001APACHE SPARK<\/p>\n<\/p>\n<p><p>Apache Spark\u662f\u4e00\u4e2a\u5feb\u901f\u7684\u901a\u7528\u96c6\u7fa4\u8ba1\u7b97\u7cfb\u7edf\uff0c\u5b83\u4e0eHadoop\u517c\u5bb9\uff0c\u5e76\u4e14\u5bf9Python\u652f\u6301\u975e\u5e38\u53cb\u597d\u3002\u4f7f\u7528Spark\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u6570\u636e\u4efb\u52a1\uff0c\u5e76\u4e14\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u914d\u7f6eSpark<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u96c6\u7fa4\u4e0a\u5df2\u5b89\u88c5\u5e76\u914d\u7f6e\u4e86Spark\u3002\u60a8\u53ef\u4ee5\u4eceSpark\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u5e76\u6309\u7167\u6587\u6863\u5b8c\u6210\u914d\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Spark\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>Spark\u63d0\u4f9b\u4e86PySpark\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7f16\u5199Spark\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PySpark\u8fdb\u884cword count\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyspark import SparkContext<\/p>\n<p>sc = SparkContext(&quot;local&quot;, &quot;Word Count&quot;)<\/p>\n<p>text_file = sc.textFile(&quot;hdfs:\/\/\/user\/input&quot;)<\/p>\n<p>counts = (text_file.flatMap(lambda line: line.split())<\/p>\n<p>          .map(lambda word: (word, 1))<\/p>\n<p>          .reduceByKey(lambda a, b: a + b))<\/p>\n<p>counts.saveAsTextFile(&quot;hdfs:\/\/\/user\/output&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4ea4Spark\u4f5c\u4e1a<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5e94\u7528\u7a0b\u5e8f\u4fdd\u5b58\u4e3aPython\u6587\u4ef6\uff08\u4f8b\u5982<code>word_count.py<\/code>\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>spark-submit<\/code>\u547d\u4ee4\u63d0\u4ea4\u4f5c\u4e1a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">spark-submit --master yarn word_count.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8Spark\u4f5c\u4e1a\uff0c\u5e76\u5728HDFS\u4e0a\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5c06PYTHON\u4ee3\u7801\u6253\u5305\u4e3aHADOOP\u4efb\u52a1<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u9700\u8981\u5c06Python\u4ee3\u7801\u6253\u5305\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7Hadoop\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51c6\u5907Python\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u6216\u5176\u4ed6\u5de5\u5177\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3a\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u5e94\u7528\u7a0b\u5e8f\u4f5c\u4e3aHadoop\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Hadoop\u7684<code>-libjars<\/code>\u3001<code>-files<\/code>\u3001<code>-archives<\/code>\u7b49\u9009\u9879\uff0c\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u4f9d\u8d56\u9879\u4e0a\u4f20\u5230Hadoop\u96c6\u7fa4\uff0c\u7136\u540e\u901a\u8fc7Hadoop\u4efb\u52a1\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001HADOOP PIPES<\/p>\n<\/p>\n<p><p>Hadoop Pipes\u662fHadoop\u63d0\u4f9b\u7684C++ API\uff0c\u4f46\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7SWIG\uff08Simplified Wrapper and Interface Generator\uff09\u5de5\u5177\uff0c\u5c06Python\u4ee3\u7801\u5305\u88c5\u4e3aHadoop Pipes\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528SWIG\u751f\u6210\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u60a8\u7684Python\u4ee3\u7801\u751f\u6210C++\u63a5\u53e3\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3aHadoop Pipes\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4ea4Pipes\u4f5c\u4e1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Hadoop\u547d\u4ee4\u63d0\u4ea4Pipes\u4f5c\u4e1a\uff0c\u5e76\u6307\u5b9a\u60a8\u7684Python\u63a5\u53e3\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5176\u4ed6\u5de5\u5177\u548c\u6846\u67b6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u5de5\u5177\u548c\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728Hadoop\u4e0a\u8fd0\u884cPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>Luigi<\/strong>\uff1a\u4e00\u4e2aPython\u6a21\u5757\uff0c\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u7ba1\u9053\u4f5c\u4e1a\u3002<\/li>\n<li><strong><a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rflow<\/strong>\uff1a\u4e00\u4e2a\u5e73\u53f0\u6765\u7f16\u6392\u590d\u6742\u7684\u8ba1\u7b97\u5de5\u4f5c\u6d41\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u76d1\u63a7\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cHadoop\u8fd0\u884cPython\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u3001\u96c6\u7fa4\u914d\u7f6e\u4ee5\u53ca\u4efb\u52a1\u590d\u6742\u5ea6\u3002<strong>Hadoop Streaming<\/strong>\u9002\u5408\u7b80\u5355\u7684MapReduce\u4efb\u52a1\uff0c\u800c<strong>Apache Spark<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u9ad8\u6548\u7684\u5904\u7406\u80fd\u529b\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u5c06Python\u4ee3\u7801\u6253\u5305\u6216\u4f7f\u7528\u5176\u4ed6\u6846\u67b6\u8fdb\u884c\u96c6\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Hadoop\u96c6\u7fa4\u4e2d\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Hadoop\u96c6\u7fa4\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Hadoop Streaming\uff0c\u8fd9\u662f\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u4efb\u610f\u53ef\u6267\u884c\u7a0b\u5e8f\uff08\u5982Python\uff09\u4f5c\u4e3aMapper\u548cReducer\u7684\u5de5\u5177\u3002\u60a8\u9700\u8981\u5c06\u60a8\u7684Python\u811a\u672c\u51c6\u5907\u597d\u5e76\u786e\u4fdd\u5b83\u4eec\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002\u4f7f\u7528Hadoop\u547d\u4ee4\u884c\u5de5\u5177\u63d0\u4ea4\u4f5c\u4e1a\u65f6\uff0c\u6307\u5b9aPython\u811a\u672c\u4f5c\u4e3aMapper\u6216Reducer\u5373\u53ef\u3002<\/p>\n<p><strong>\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u4f9d\u8d56\u9879\u624d\u80fd\u5728Hadoop\u4e0a\u8fd0\u884cPython\uff1f<\/strong><br \/>\u5728Hadoop\u73af\u5883\u4e2d\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u786e\u4fdd\u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u5b89\u88c5\u4e86\u76f8\u5e94\u7684Python\u7248\u672c\u53ca\u5176\u4f9d\u8d56\u5e93\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u5728\u6bcf\u4e2a\u8282\u70b9\u4e0a\u5b89\u88c5Python\u4ee5\u53ca\u60a8\u811a\u672c\u6240\u9700\u7684\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982NumPy\u6216Pandas\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u60a8\u7684Hadoop\u7248\u672c\u652f\u6301Streaming\uff0c\u5e76\u9075\u5faa\u76f8\u5e94\u7684\u914d\u7f6e\u8981\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Hadoop\u4e2dPython\u811a\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Hadoop\u4e2d\uff0cPython\u811a\u672c\u7684\u8f93\u5165\u548c\u8f93\u51fa\u901a\u5e38\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u8f93\u51fa\uff08stdin\/stdout\uff09\u8fdb\u884c\u5904\u7406\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Hadoop\u547d\u4ee4\u884c\u9009\u9879\u6307\u5b9a\u8f93\u5165\u548c\u8f93\u51fa\u8def\u5f84\u3002\u8f93\u5165\u6570\u636e\u5c06\u88ab\u4f20\u9012\u7ed9Python\u811a\u672c\uff0c\u811a\u672c\u7684\u8f93\u51fa\u5c06\u81ea\u52a8\u5199\u5165\u6307\u5b9a\u7684\u8f93\u51fa\u76ee\u5f55\u3002\u786e\u4fdd\u5728\u811a\u672c\u4e2d\u9002\u5f53\u5730\u5904\u7406\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u7ed3\u679c\u683c\u5f0f\u5316\u4e3aHadoop\u53ef\u8bc6\u522b\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Hadoop\u8fd0\u884cPython\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7Hadoop Streaming\u3001Apache Spark\u6216\u5c06Pyth [&hellip;]","protected":false},"author":3,"featured_media":1011738,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011731"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1011731"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011731\/revisions"}],"predecessor-version":[{"id":1011740,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011731\/revisions\/1011740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011738"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}