{"id":999126,"date":"2024-12-27T09:38:49","date_gmt":"2024-12-27T01:38:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999126.html"},"modified":"2024-12-27T09:38:51","modified_gmt":"2024-12-27T01:38:51","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90orc%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999126.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210orc\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074249\/a75bf40b-5800-4589-a01d-9c13ae812a72.webp\" alt=\"python\u5982\u4f55\u751f\u6210orc\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210ORC\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Apache ORC\u5e93\u3001PyArrow\u5e93\u3001Pandas\u5e93\u6765\u5b9e\u73b0\u3002<\/strong> \u8fd9\u4e09\u79cd\u65b9\u6cd5\u90fd\u6709\u5404\u81ea\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002Apache ORC\u5e93\u63d0\u4f9b\u4e86\u5bf9ORC\u683c\u5f0f\u7684\u76f4\u63a5\u652f\u6301\u3001PyArrow\u5e93\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u5e76\u4e0ePandas\u517c\u5bb9\u3001Pandas\u5e93\u5219\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u6982\u8ff0\u5176\u5b83\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528PyArrow\u5e93\u751f\u6210ORC\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u652f\u6301ORC\u683c\u5f0f\uff0c\u8fd8\u80fd\u5904\u7406\u5176\u4ed6\u591a\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u5e76\u4e14\u4e0ePandas\u517c\u5bb9\u3002PyArrow\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5c06Pandas DataFrame\u8f6c\u6362\u4e3aORC\u6587\u4ef6\u683c\u5f0f\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PyArrow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<code>pip install pyarrow<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyArrow\u751f\u6210ORC\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u8bbe\u7f6e\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86PyArrow\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyarrow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u81f3\u5c11\u4e3a3.6\uff0c\u56e0\u4e3aPyArrow\u5728\u8f83\u65b0\u7684Python\u7248\u672c\u4e0a\u652f\u6301\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyArrow\u751f\u6210ORC\u6587\u4ef6<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Pandas\u548cPyArrow\u5e93\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4f7f\u7528Pandas DataFrame\u6765\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7PyArrow\u5c06\u5176\u8f6c\u6362\u4e3aORC\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import pyarrow as pa<\/p>\n<p>import pyarrow.orc as orc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Pandas DataFrame\u3002\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\u6765\u6f14\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c06DataFrame\u8f6c\u6362\u4e3aPyArrow Table<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06DataFrame\u8f6c\u6362\u4e3aORC\u6587\u4ef6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3aPyArrow Table\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = pa.Table.from_pandas(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5199\u5165ORC\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528PyArrow\u7684ORC\u5199\u5165\u529f\u80fd\uff0c\u5c06PyArrow Table\u5199\u5165\u5230ORC\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.orc&#39;, &#39;wb&#39;) as orc_file:<\/p>\n<p>    orc.write_table(table, orc_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u751f\u6210\u7684ORC\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210ORC\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5de5\u5177\u6216\u5e93\u6765\u9a8c\u8bc1\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5176\u5b83\u751f\u6210ORC\u6587\u4ef6\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528PyArrow\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210ORC\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Apache ORC\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Apache ORC\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406ORC\u6587\u4ef6\u683c\u5f0f\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u8bfb\u5199ORC\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7Java\u6216C++\u7684\u63a5\u53e3\u6765\u751f\u6210ORC\u6587\u4ef6\uff0c\u4f46\u5728Python\u4e2d\u76f4\u63a5\u4f7f\u7528\u7684\u652f\u6301\u8f83\u5c11\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408Pandas\u548cHadoop\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u5c06Pandas DataFrame\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528Hadoop\u7684\u5de5\u5177\uff08\u5982Hive\uff09\u5c06CSV\u8f6c\u6362\u4e3aORC\u683c\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728Hadoop\u751f\u6001\u7cfb\u7edf\u4e2d\u5904\u7406\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Spark\u7ed3\u5408PySpark<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u73af\u5883\u4e2d\u5df2\u7ecf\u4f7f\u7528Apache Spark\u8fdb\u884c\u5927\u6570\u636e\u5904\u7406\uff0cPySpark\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210ORC\u6587\u4ef6\u3002\u9996\u5148\u5c06\u6570\u636e\u8f6c\u6362\u4e3aSpark DataFrame\uff0c\u7136\u540e\u4f7f\u7528<code>write.format(&#39;orc&#39;)<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3aORC\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001ORC\u6587\u4ef6\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9ad8\u6548\u7684\u538b\u7f29\u548c\u5b58\u50a8<\/strong><\/p>\n<\/p>\n<p><p>ORC\u6587\u4ef6\u683c\u5f0f\u8bbe\u8ba1\u4e4b\u521d\u5c31\u8003\u8651\u4e86\u9ad8\u6548\u7684\u5b58\u50a8\u548c\u538b\u7f29\uff0c\u80fd\u591f\u663e\u8457\u51cf\u5c11\u5b58\u50a8\u7a7a\u95f4\uff0c\u5e76\u4e14\u5728\u8bfb\u53d6\u6570\u636e\u65f6\u51cf\u5c11I\/O\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5feb\u901f\u7684\u6570\u636e\u8bfb\u53d6<\/strong><\/p>\n<\/p>\n<p><p>ORC\u683c\u5f0f\u652f\u6301\u5206\u5757\u8bfb\u53d6\u548c\u8df3\u8fc7\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u5757\uff0c\u6781\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u8bfb\u53d6\u7684\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e30\u5bcc\u7684\u6570\u636e\u7c7b\u578b\u652f\u6301<\/strong><\/p>\n<\/p>\n<p><p>ORC\u683c\u5f0f\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f7f\u5176\u5728\u5927\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u5177\u6709\u6781\u5f3a\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u7684\u67e5\u8be2\u6027\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5982Hive\u3001Spark\u7b49\u5de5\u5177\u65f6\uff0cORC\u683c\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u5bf9\u6570\u636e\u7684\u7d22\u5f15\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u751f\u6210ORC\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u4f7f\u7528PyArrow\u5e93\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06Pandas DataFrame\u8f6c\u6362\u4e3aPyArrow Table\uff0c\u518d\u5199\u5165ORC\u6587\u4ef6\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u6570\u636e\u7684\u5b58\u50a8\u548c\u5904\u7406\u3002ORC\u683c\u5f0f\u5728\u5927\u6570\u636e\u9886\u57df\u5177\u6709\u663e\u8457\u7684\u4f18\u52bf\uff0c\u5305\u62ec\u9ad8\u6548\u7684\u538b\u7f29\u3001\u5feb\u901f\u7684\u6570\u636e\u8bfb\u53d6\u548c\u4f18\u5316\u7684\u67e5\u8be2\u6027\u80fd\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u751f\u6210ORC\u6587\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5b58\u50a8\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u751f\u6210ORC\u6587\u4ef6\u65f6\u9700\u8981\u7528\u5230\u54ea\u4e9bPython\u5e93\uff1f<\/strong><br \/>\u751f\u6210ORC\u6587\u4ef6\u901a\u5e38\u9700\u8981\u4f7f\u7528Apache\u7684ORC\u5e93\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>pyarrow<\/code>\u6216<code>fastparquet<\/code>\u5e93\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5bf9ORC\u683c\u5f0f\u7684\u652f\u6301\uff0c\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u8f7b\u677e\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3aORC\u683c\u5f0f\u3002\u5efa\u8bae\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install pyarrow fastparquet<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5728\u751f\u6210ORC\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u751f\u6210ORC\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6b63\u786e\u5904\u7406\u6570\u636e\u7c7b\u578b\u81f3\u5173\u91cd\u8981\u3002ORC\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u548c\u590d\u6742\u7c7b\u578b\uff08\u5982\u7ed3\u6784\u4f53\u548c\u6570\u7ec4\uff09\u3002\u5728\u4f7f\u7528<code>pyarrow<\/code>\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Table<\/code>\u5bf9\u8c61\uff0c\u5e76\u5728\u5b9a\u4e49\u5b57\u6bb5\u65f6\u6307\u5b9a\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pa.array()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u786e\u4fdd\u6570\u636e\u5728\u5199\u5165ORC\u6587\u4ef6\u65f6\u4fdd\u6301\u6b63\u786e\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u751f\u6210\u7684ORC\u6587\u4ef6\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u5728\u751f\u6210ORC\u6587\u4ef6\u540e\uff0c\u9a8c\u8bc1\u5176\u6709\u6548\u6027\u662f\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>pyarrow<\/code>\u5e93\u7684<code>orc<\/code>\u6a21\u5757\u6765\u8bfb\u53d6ORC\u6587\u4ef6\uff0c\u5e76\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002\u901a\u8fc7<code>pyarrow.orc.read_table()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u4e0e\u539f\u59cb\u6570\u636e\u8fdb\u884c\u5bf9\u6bd4\uff0c\u786e\u4fdd\u6240\u6709\u8bb0\u5f55\u90fd\u5df2\u6b63\u786e\u5199\u5165\u3002\u5982\u679c\u6570\u636e\u5339\u914d\u4e14\u6ca1\u6709\u9519\u8bef\u4fe1\u606f\uff0c\u901a\u5e38\u53ef\u4ee5\u8ba4\u4e3aORC\u6587\u4ef6\u662f\u6709\u6548\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210ORC\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Apache ORC\u5e93\u3001PyArrow\u5e93\u3001Pandas\u5e93\u6765\u5b9e\u73b0\u3002 \u8fd9\u4e09\u79cd [&hellip;]","protected":false},"author":3,"featured_media":999133,"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\/999126"}],"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=999126"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999126\/revisions"}],"predecessor-version":[{"id":999139,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999126\/revisions\/999139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999133"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}