{"id":1160062,"date":"2025-01-13T19:00:32","date_gmt":"2025-01-13T11:00:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160062.html"},"modified":"2025-01-13T19:00:34","modified_gmt":"2025-01-13T11:00:34","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e4%b8%8ekettle%e7%bb%93%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160062.html","title":{"rendered":"\u7528python\u5982\u4f55\u4e0ekettle\u7ed3\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201628\/229af53c-8e36-4dfe-a13a-cd76077ee6c6.webp\" alt=\"\u7528python\u5982\u4f55\u4e0ekettle\u7ed3\u5408\" \/><\/p>\n<p><p> <strong>\u7528Python\u4e0eKettle\u7ed3\u5408\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528Py4J\u5e93\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Kettle\u4f5c\u4e1a\u3001\u4f7f\u7528Kettle\u7684REST API\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528Py4J\u5e93<\/strong> \u662f\u4e00\u79cd\u8f83\u4e3a\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8Python\u4ee3\u7801\u4e0eJava\u4ee3\u7801\u4e92\u64cd\u4f5c\u3002Py4J\u5e93\u4f7f\u5f97Python\u7a0b\u5e8f\u80fd\u591f\u8c03\u7528Java\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14\u80fd\u591f\u8bbf\u95eeJava\u4e2d\u7684\u7c7b\u548c\u65b9\u6cd5\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Py4J\u5e93\u6765\u7ed3\u5408Python\u4e0eKettle\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Py4J\u5e93\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Py4J\u5e93\u9700\u8981\u9996\u5148\u8fdb\u884c\u5b89\u88c5\u548c\u914d\u7f6e\u3002\u5728Python\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Py4J\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u786e\u4fddKettle\u7684Java\u73af\u5883\u5df2\u7ecf\u914d\u7f6e\u5b8c\u6bd5\uff0c\u5e76\u4e14\u80fd\u591f\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cKettle\u76f8\u5173\u7684Java\u7c7b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Py4J\u5e93\u6765\u8c03\u7528Kettle\u7684Java\u7c7b\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6eKettle\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Py4J\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fddKettle\u7684\u6240\u6709\u4f9d\u8d56\u5e93\u5df2\u7ecf\u6dfb\u52a0\u5230Java\u7684\u7c7b\u8def\u5f84\u4e2d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cKettle\u7684\u4f9d\u8d56\u5e93\u4f4d\u4e8eKettle\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684<code>lib<\/code>\u6587\u4ef6\u5939\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\u7c7b\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export CLASSPATH=\/path\/to\/kettle\/lib\/*:$CLASSPATH<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f7f\u7528\u7684\u662fWindows\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728\u73af\u5883\u53d8\u91cf\u4e2d\u6dfb\u52a0\u7c7b\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199Python\u4ee3\u7801\u8c03\u7528Kettle<\/h3>\n<\/p>\n<p><p>\u5728\u914d\u7f6e\u597d\u73af\u5883\u4e4b\u540e\uff0c\u53ef\u4ee5\u7f16\u5199Python\u4ee3\u7801\u6765\u8c03\u7528Kettle\u7684Java\u7c7b\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from py4j.java_gateway import JavaGateway, GatewayParameters, CallbackServerParameters<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    # \u542f\u52a8Java\u7f51\u5173<\/p>\n<p>    gateway = JavaGateway(gateway_parameters=GatewayParameters(port=25333),<\/p>\n<p>                          callback_server_parameters=CallbackServerParameters(port=25334))<\/p>\n<p>    # \u83b7\u53d6Kettle\u73af\u5883<\/p>\n<p>    kettle_environment = gateway.jvm.org.pentaho.di.core.KettleEnvironment<\/p>\n<p>    kettle_environment.init()<\/p>\n<p>    # \u52a0\u8f7dKettle\u4f5c\u4e1a<\/p>\n<p>    job_meta_class = gateway.jvm.org.pentaho.di.job.JobMeta<\/p>\n<p>    job_meta = job_meta_class(&quot;\/path\/to\/your\/job.kjb&quot;)<\/p>\n<p>    # \u521b\u5efa\u5e76\u542f\u52a8Kettle\u4f5c\u4e1a<\/p>\n<p>    job_class = gateway.jvm.org.pentaho.di.job.Job<\/p>\n<p>    job = job_class(job_meta)<\/p>\n<p>    job.start()<\/p>\n<p>    job.waitUntilFinished()<\/p>\n<p>    # \u68c0\u67e5\u4f5c\u4e1a\u662f\u5426\u6210\u529f\u5b8c\u6210<\/p>\n<p>    if job.getErrors() &gt; 0:<\/p>\n<p>        print(&quot;Job failed with errors&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Job completed successfully&quot;)<\/p>\n<p>    # \u5173\u95ed\u7f51\u5173<\/p>\n<p>    gateway.close()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u542f\u52a8\u4e86Py4J\u7f51\u5173\uff0c\u5e76\u521d\u59cb\u5316\u4e86Kettle\u73af\u5883\uff0c\u7136\u540e\u52a0\u8f7d\u4e86Kettle\u4f5c\u4e1a\uff08<code>.kjb<\/code>\u6587\u4ef6\uff09\uff0c\u521b\u5efa\u5e76\u542f\u52a8\u4e86Kettle\u4f5c\u4e1a\uff0c\u5e76\u68c0\u67e5\u4f5c\u4e1a\u662f\u5426\u6210\u529f\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Kettle\u4f5c\u4e1a<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u7ed3\u5408Python\u4e0eKettle\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u8c03\u7528Kettle\u4f5c\u4e1a\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884c\u547d\u4ee4\u884c\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_kettle_job(job_path):<\/p>\n<p>    try:<\/p>\n<p>        # \u6784\u5efa\u547d\u4ee4<\/p>\n<p>        command = f&quot;\/path\/to\/kettle\/kitchen.sh -file={job_path}&quot;<\/p>\n<p>        # \u6267\u884c\u547d\u4ee4<\/p>\n<p>        result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>        # \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>        print(result.stdout.decode())<\/p>\n<p>        # \u68c0\u67e5\u4f5c\u4e1a\u662f\u5426\u6210\u529f\u5b8c\u6210<\/p>\n<p>        if result.returncode == 0:<\/p>\n<p>            print(&quot;Job completed successfully&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Job failed with errors&quot;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Job execution failed: {e.stderr.decode()}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    run_kettle_job(&quot;\/path\/to\/your\/job.kjb&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6784\u5efa\u547d\u4ee4\u884c\u547d\u4ee4\u5e76\u4f7f\u7528<code>subprocess.run<\/code>\u6267\u884c\u8be5\u547d\u4ee4\uff0c\u53ef\u4ee5\u8c03\u7528Kettle\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff08<code>kitchen.sh<\/code>\u6216<code>kitchen.bat<\/code>\uff09\u6765\u8fd0\u884cKettle\u4f5c\u4e1a\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Kettle\u7684REST API<\/h3>\n<\/p>\n<p><p>Kettle\u8fd8\u63d0\u4f9b\u4e86REST API\uff0c\u53ef\u4ee5\u901a\u8fc7HTTP\u8bf7\u6c42\u6765\u89e6\u53d1\u548c\u7ba1\u7406Kettle\u4f5c\u4e1a\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>requests<\/code>\u5e93\u6765\u4e0eKettle\u7684REST API\u8fdb\u884c\u4ea4\u4e92\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<p>def run_kettle_job(job_name, base_url, username, password):<\/p>\n<p>    # \u6784\u5efaURL\u548c\u8ba4\u8bc1\u4fe1\u606f<\/p>\n<p>    url = f&quot;{base_url}\/kettle\/job\/{job_name}\/execute&quot;<\/p>\n<p>    auth = (username, password)<\/p>\n<p>    # \u53d1\u9001HTTP\u8bf7\u6c42<\/p>\n<p>    response = requests.post(url, auth=auth)<\/p>\n<p>    # \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        print(&quot;Job started successfully&quot;)<\/p>\n<p>        job_status_url = f&quot;{base_url}\/kettle\/job\/{job_name}\/status&quot;<\/p>\n<p>        response = requests.get(job_status_url, auth=auth)<\/p>\n<p>        job_status = response.json()<\/p>\n<p>        print(json.dumps(job_status, indent=4))<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to start job&quot;)<\/p>\n<p>        print(response.text)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    run_kettle_job(&quot;your_job_name&quot;, &quot;http:\/\/localhost:8080&quot;, &quot;admin&quot;, &quot;password&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6784\u5efaHTTP\u8bf7\u6c42\u5e76\u4f7f\u7528<code>requests.post<\/code>\u65b9\u6cd5\u53d1\u9001\u8bf7\u6c42\uff0c\u53ef\u4ee5\u89e6\u53d1Kettle\u7684\u4f5c\u4e1a\u3002\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u4f5c\u4e1a\u72b6\u6001\u7684API\u6765\u83b7\u53d6\u4f5c\u4e1a\u6267\u884c\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06Python\u4e0eKettle\u7ed3\u5408\u8d77\u6765\uff0c\u5145\u5206\u5229\u7528Kettle\u5f3a\u5927\u7684ETL\u80fd\u529b\u548cPython\u7075\u6d3b\u7684\u7f16\u7a0b\u80fd\u529b\u6765\u5904\u7406\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u7ed3\u5408Python\u4e0eKettle\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u96c6\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528Kettle\u8f6c\u6362\u548c\u4f5c\u4e1a\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528Kettle\uff08\u4e5f\u79f0\u4e3aPentaho Data Integration\uff09\u7684\u8f6c\u6362\u548c\u4f5c\u4e1a\uff0c\u53ef\u4ee5\u4f7f\u7528Kettle\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u901a\u8fc7Python\u5e93\u4e0eKettle\u7684API\u4ea4\u4e92\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884cKettle\u7684\u547d\u4ee4\u884c\u811a\u672c\uff08\u5982Kitchen\u548cPan\uff09\u6765\u6267\u884c\u4f5c\u4e1a\u548c\u8f6c\u6362\uff0c\u6216\u4f7f\u7528<code>PyPDI<\/code>\u5e93\u6765\u76f4\u63a5\u5728Python\u4e2d\u64cd\u4f5cKettle\u7684\u529f\u80fd\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u7f16\u5199\u547d\u4ee4\u884c\u8c03\u7528\u6216\u4f7f\u7528API\u8fdb\u884c\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u4e0eKettle\u8fdb\u884c\u96c6\u6210\uff1f<\/strong><br \/>\u4e0eKettle\u96c6\u6210\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u4e00\u4e9bPython\u5e93\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>PyPDI<\/code>\u5e93\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\uff0c\u4e13\u95e8\u7528\u4e8e\u4e0eKettle\u8fdb\u884c\u4ea4\u4e92\u3002\u5b83\u5141\u8bb8\u7528\u6237\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u6267\u884cKettle\u8f6c\u6362\u548c\u4f5c\u4e1a\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6570\u636e\u6d41\u548c\u83b7\u53d6\u7ed3\u679c\u3002\u6b64\u5916\uff0c<code>subprocess<\/code>\u6a21\u5757\u4e5f\u53ef\u7528\u4e8e\u8c03\u7528Kettle\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u76f4\u63a5\u8fd0\u884c\u5916\u90e8\u547d\u4ee4\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u4e0eKettle\u96c6\u6210\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u548c\u89e3\u51b3\u65b9\u6848\uff1f<\/strong><br \/>\u5728\u4e0eKettle\u96c6\u6210\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u53ef\u80fd\u5305\u62ec\u8def\u5f84\u914d\u7f6e\u9519\u8bef\u3001\u6743\u9650\u95ee\u9898\u4ee5\u53caKettle\u7248\u672c\u4e0d\u517c\u5bb9\u7b49\u3002\u786e\u4fddKettle\u7684\u5b89\u88c5\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14Python\u811a\u672c\u5177\u5907\u6267\u884c\u76f8\u5e94\u547d\u4ee4\u7684\u6743\u9650\u3002\u5982\u679c\u9047\u5230\u4e0d\u517c\u5bb9\u95ee\u9898\uff0c\u68c0\u67e5Kettle\u548cPython\u5e93\u7684\u7248\u672c\uff0c\u5e76\u8003\u8651\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u4ee5\u89e3\u51b3\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u6b64\u5916\uff0c\u8c03\u8bd5\u65f6\u53ef\u4ee5\u67e5\u770bKettle\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u4e0eKettle\u7ed3\u5408\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528Py4J\u5e93\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Kettle\u4f5c\u4e1a\u3001\u4f7f\u7528Kettle\u7684R [&hellip;]","protected":false},"author":3,"featured_media":1160067,"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\/1160062"}],"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=1160062"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160062\/revisions"}],"predecessor-version":[{"id":1160071,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160062\/revisions\/1160071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160067"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}