You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/01/06 11:04:25 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #13281: Add How-To guide for PostgresOperator

kaxil commented on a change in pull request #13281:
URL: https://github.com/apache/airflow/pull/13281#discussion_r552509538



##########
File path: docs/apache-airflow-providers-postgres/operators/postgres_operator_howto_guide.rst
##########
@@ -0,0 +1,243 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+ ..   http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+How-to Guide for PostgresOperator
+=================================
+
+Introduction
+------------
+
+Apache Airflow has a robust trove of operators that can be used to implement the various tasks that make up your
+workflow. Airflow is essentially a graph (Directed Acyclic Graph) made up of tasks (nodes) and dependencies (edges).
+
+A task defined or implemented by a operator is a unit of work in your data pipeline.
+
+The purpose of Postgres Operator is to define tasks involving interactions with the PostgreSQL database.
+ In ``Airflow-2.0``, the ``PostgresOperator`` class resides at ``airflow.providers.postgres.operator.postgres``.
+
+Under the hood, the ``PostgresOperator`` class delegates its heavy lifting to the ``PostgresHook`` class.
+
+Common Database Operations with PostgresOperator
+------------------------------------------------
+
+To use the postgres operator to carry out SQL request, two parameters are required: ``sql`` and ``postgres_conn_id``.
+These two parameters are eventually fed to the postgres hook object that interacts directly with the postgres database.
+
+Creating a Postgres database table
+----------------------------------
+
+The code snippets below are based on Airflow-2.0
+
+.. code-block:: python
+
+    import datetime
+    from airflow import DAG
+    from airflow.providers.postgres.operators.postgres import PostgresOperator
+
+
+    default_args = {
+                    "start_date": datetime.datetime(2020, 2, 2),
+                    "owner": "airflow"
+                    }
+
+
+    with DAG(dag_id="postgres_operator_dag", schedule_interval="@once", default_args=default_args, catchup=False) as dag:
+        create_pet_table = PostgresOperator(
+                                            "create_pet_table",
+                                            postgres_conn_id = "postgres_default",
+                                            sql = """
+                                            CREATE TABLE IF NOT EXISTS pet (
+                                                  pet_id SERIAL PRIMARY KEY,
+                                                  name VARCHAR NOT NULL,
+                                                  pet_type VARCHAR NOT NULL,
+                                                  birth_date DATE NOT NULL,
+                                                  OWNER VARCHAR NOT NULL);
+                                                  """
+                                            )

Review comment:
       ```suggestion
       with DAG(dag_id="postgres_operator_dag", schedule_interval="@once", default_args=default_args, catchup=False) as dag:
       create_pet_table = PostgresOperator(
           task_id="create_pet_table",
           postgres_conn_id = "postgres_default",
           sql = """
           CREATE TABLE IF NOT EXISTS pet (
                 pet_id SERIAL PRIMARY KEY,
                 name VARCHAR NOT NULL,
                 pet_type VARCHAR NOT NULL,
                 birth_date DATE NOT NULL,
                 OWNER VARCHAR NOT NULL);
                 """
       )
           )
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org