{"id":1039207,"date":"2024-12-31T12:30:29","date_gmt":"2024-12-31T04:30:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039207.html"},"modified":"2024-12-31T12:30:31","modified_gmt":"2024-12-31T04:30:31","slug":"python%e5%a6%82%e4%bd%95%e8%b7%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e8%a1%a8%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039207.html","title":{"rendered":"python\u5982\u4f55\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/feab8d4f-6d8c-4a9e-a4dd-1fe2c6d230b8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\" \/><\/p>\n<p><p> <strong>Python\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65<\/strong>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982<code>sqlalchemy<\/code>\u6216<code>pyodbc<\/code>\uff09\u3001\u4f7f\u7528ETL\uff08\u62bd\u53d6\u3001\u8f6c\u6362\u3001\u52a0\u8f7d\uff09\u5de5\u5177\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u540c\u6b65\u811a\u672c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u540c\u6b65<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2aPython SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff08ORM\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5e93\u62bd\u8c61\u548c\u8fde\u63a5\u529f\u80fd\u3002\u4f7f\u7528SQLAlchemy\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8de8\u6570\u636e\u5e93\u7684\u8868\u540c\u6b65\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5SQLAlchemy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86SQLAlchemy\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 sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6e90\u6570\u636e\u5e93\u548c\u76ee\u6807\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u521b\u5efa\u6e90\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>source_engine = create_engine(&#39;mysql+pymysql:\/\/user:password@host\/dbname&#39;)<\/p>\n<p>SourceSession = sessionmaker(bind=source_engine)<\/p>\n<p>source_session = SourceSession()<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u6807\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>target_engine = create_engine(&#39;postgresql+psycopg2:\/\/user:password@host\/dbname&#39;)<\/p>\n<p>TargetSession = sessionmaker(bind=target_engine)<\/p>\n<p>target_session = TargetSession()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9a\u4e49\u6620\u5c04\u7c7b<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u8868\u7684ORM\u6620\u5c04\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy import Column, Integer, String<\/p>\n<p>Base = declarative_base()<\/p>\n<p>class SourceTable(Base):<\/p>\n<p>    __tablename__ = &#39;source_table&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    value = Column(String)<\/p>\n<p>class TargetTable(Base):<\/p>\n<p>    __tablename__ = &#39;target_table&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    value = Column(String)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u6570\u636e\u540c\u6b65<\/h4>\n<\/p>\n<p><p>\u4ece\u6e90\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\u5e76\u63d2\u5165\u5230\u76ee\u6807\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sync_data():<\/p>\n<p>    # \u4ece\u6e90\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e<\/p>\n<p>    source_data = source_session.query(SourceTable).all()<\/p>\n<p>    # \u63d2\u5165\u5230\u76ee\u6807\u6570\u636e\u5e93<\/p>\n<p>    for record in source_data:<\/p>\n<p>        target_record = TargetTable(id=record.id, name=record.name, value=record.value)<\/p>\n<p>        target_session.add(target_record)<\/p>\n<p>    target_session.commit()<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    sync_data()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ETL\u5de5\u5177\u8fdb\u884c\u540c\u6b65<\/h3>\n<\/p>\n<p><p>ETL\u5de5\u5177\uff08\u5982Apache Airflow\u3001Talend\u7b49\uff09\u53ef\u4ee5\u81ea\u52a8\u5316\u6570\u636e\u540c\u6b65\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Apache Airflow\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Airflow<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Airflow\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install apache-airflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49DAG\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aDAG\u4efb\u52a1\u8fdb\u884c\u6570\u636e\u540c\u6b65\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from airflow import DAG<\/p>\n<p>from airflow.operators.python_operator import PythonOperator<\/p>\n<p>from datetime import datetime<\/p>\n<p>import sqlalchemy<\/p>\n<p>default_args = {<\/p>\n<p>    &#39;owner&#39;: &#39;airflow&#39;,<\/p>\n<p>    &#39;start_date&#39;: datetime(2023, 1, 1),<\/p>\n<p>    &#39;retries&#39;: 1,<\/p>\n<p>}<\/p>\n<p>dag = DAG(&#39;db_sync&#39;, default_args=default_args, schedule_interval=&#39;@daily&#39;)<\/p>\n<p>def sync_data():<\/p>\n<p>    source_engine = sqlalchemy.create_engine(&#39;mysql+pymysql:\/\/user:password@host\/dbname&#39;)<\/p>\n<p>    target_engine = sqlalchemy.create_engine(&#39;postgresql+psycopg2:\/\/user:password@host\/dbname&#39;)<\/p>\n<p>    with source_engine.connect() as source_conn, target_engine.connect() as target_conn:<\/p>\n<p>        source_data = source_conn.execute(&quot;SELECT * FROM source_table&quot;).fetchall()<\/p>\n<p>        for row in source_data:<\/p>\n<p>            target_conn.execute(&quot;INSERT INTO target_table (id, name, value) VALUES (%s, %s, %s)&quot;,<\/p>\n<p>                                (row.id, row.name, row.value))<\/p>\n<p>sync_task = PythonOperator(<\/p>\n<p>    task_id=&#39;sync_task&#39;,<\/p>\n<p>    python_callable=sync_data,<\/p>\n<p>    dag=dag,<\/p>\n<p>)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    dag.cli()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u540c\u6b65\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528ORM\u6216ETL\u5de5\u5177\uff0c\u53ef\u4ee5\u76f4\u63a5\u7f16\u5199\u81ea\u5b9a\u4e49\u811a\u672c\u8fdb\u884c\u6570\u636e\u540c\u6b65\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u6570\u636e\u5e93\u9a71\u52a8<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u9700\u8981\u7684\u6570\u636e\u5e93\u9a71\u52a8\uff0c\u5982<code>pymysql<\/code>\u548c<code>psycopg2<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymysql psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u81ea\u5b9a\u4e49\u540c\u6b65\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u81ea\u5b9a\u4e49\u811a\u672c\u8fdb\u884c\u6570\u636e\u540c\u6b65\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>import psycopg2<\/p>\n<p>def sync_data():<\/p>\n<p>    # \u6e90\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>    source_conn = pymysql.connect(host=&#39;host&#39;, user=&#39;user&#39;, password=&#39;password&#39;, db=&#39;dbname&#39;)<\/p>\n<p>    source_cursor = source_conn.cursor()<\/p>\n<p>    # \u76ee\u6807\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>    target_conn = psycopg2.connect(host=&#39;host&#39;, user=&#39;user&#39;, password=&#39;password&#39;, dbname=&#39;dbname&#39;)<\/p>\n<p>    target_cursor = target_conn.cursor()<\/p>\n<p>    # \u4ece\u6e90\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e<\/p>\n<p>    source_cursor.execute(&quot;SELECT id, name, value FROM source_table&quot;)<\/p>\n<p>    source_data = source_cursor.fetchall()<\/p>\n<p>    # \u63d2\u5165\u5230\u76ee\u6807\u6570\u636e\u5e93<\/p>\n<p>    for row in source_data:<\/p>\n<p>        target_cursor.execute(&quot;INSERT INTO target_table (id, name, value) VALUES (%s, %s, %s)&quot;, row)<\/p>\n<p>    target_conn.commit()<\/p>\n<p>    source_cursor.close()<\/p>\n<p>    source_conn.close()<\/p>\n<p>    target_cursor.close()<\/p>\n<p>    target_conn.close()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    sync_data()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6570\u636e\u51b2\u7a81\u548c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u540c\u6b65\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u636e\u51b2\u7a81\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u9700\u8981\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u548c\u51b2\u7a81\u89e3\u51b3\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u540c\u6b65\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    sync_data()<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;Error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u51b2\u7a81\u89e3\u51b3<\/h4>\n<\/p>\n<p><p>\u5728\u63d2\u5165\u6570\u636e\u65f6\u5904\u7406\u51b2\u7a81\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in source_data:<\/p>\n<p>    try:<\/p>\n<p>        target_cursor.execute(&quot;INSERT INTO target_table (id, name, value) VALUES (%s, %s, %s)&quot;, row)<\/p>\n<p>    except psycopg2.IntegrityError:<\/p>\n<p>        target_conn.rollback()<\/p>\n<p>        target_cursor.execute(&quot;UPDATE target_table SET name = %s, value = %s WHERE id = %s&quot;, (row[1], row[2], row[0]))<\/p>\n<p>    else:<\/p>\n<p>        target_conn.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u8fdb\u884c\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528SQLAlchemy\u3001ETL\u5de5\u5177\uff08\u5982Apache Airflow\uff09\u548c\u81ea\u5b9a\u4e49\u540c\u6b65\u811a\u672c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0Python\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u662f\u6307\u5728\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u4e4b\u95f4\u4fdd\u6301\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b9e\u65f6\u66f4\u65b0\u7684\u4e00\u79cd\u6280\u672f\u3002\u901a\u8fc7\u6570\u636e\u540c\u6b65\uff0c\u53ef\u4ee5\u786e\u4fdd\u591a\u4e2a\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u76f8\u4e92\u66f4\u65b0\uff0c\u907f\u514d\u56e0\u6570\u636e\u6ede\u540e\u800c\u4ea7\u751f\u7684\u95ee\u9898\u3002\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\u591a\u79df\u6237\u5e94\u7528\u3001\u6570\u636e\u5907\u4efd\u548c\u707e\u96be\u6062\u590d\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u8de8\u6570\u636e\u5e93\u7684\u540c\u6b65\uff1f<\/strong><br \/>\u5b9e\u73b0\u8de8\u6570\u636e\u5e93\u540c\u6b65\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982SQLAlchemy\u3001Pandas\u6216\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u9a71\u52a8\uff09\uff0c\u901a\u8fc7\u7f16\u5199\u811a\u672c\u6765\u5b9a\u671f\u8bfb\u53d6\u6e90\u6570\u636e\u5e93\u7684\u6570\u636e\uff0c\u7136\u540e\u5c06\u5176\u63d2\u5165\u6216\u66f4\u65b0\u5230\u76ee\u6807\u6570\u636e\u5e93\u4e2d\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6d88\u606f\u961f\u5217\uff08\u5982RabbitMQ\u6216Kafka\uff09\u4e5f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u540c\u6b65\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u65f6\u5904\u7406\u6570\u636e\u53d8\u5316\u3002<\/p>\n<p><strong>\u8de8\u6570\u636e\u5e93\u540c\u6b65\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6027\u80fd\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u9700\u5173\u6ce8\u6570\u636e\u4f20\u8f93\u7684\u5ef6\u8fdf\u3001\u7f51\u7edc\u5e26\u5bbd\u3001\u6570\u636e\u5e93\u7684\u8bfb\u5199\u6027\u80fd\u7b49\u3002\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\uff0c\u53ef\u4ee5\u8003\u8651\u589e\u91cf\u540c\u6b65\uff0c\u53ea\u540c\u6b65\u6709\u53d8\u5316\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f\u5168\u91cf\u540c\u6b65\u3002\u6b64\u5916\uff0c\u5408\u7406\u7684\u7d22\u5f15\u548c\u6279\u91cf\u64cd\u4f5c\u4e5f\u80fd\u663e\u8457\u63d0\u5347\u540c\u6b65\u6548\u7387\u3002\u5b9a\u671f\u76d1\u63a7\u540c\u6b65\u8fc7\u7a0b\u4e2d\u7684\u6027\u80fd\u6307\u6807\u4e5f\u662f\u786e\u4fdd\u7cfb\u7edf\u7a33\u5b9a\u7684\u91cd\u8981\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8de8\u6570\u636e\u5e93\u8868\u540c\u6b65\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982sqlalchemy\u6216pyo [&hellip;]","protected":false},"author":3,"featured_media":1039213,"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\/1039207"}],"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=1039207"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039207\/revisions"}],"predecessor-version":[{"id":1039216,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039207\/revisions\/1039216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039213"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}