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 2022/03/14 10:10:09 UTC

[GitHub] [airflow] ephraimbuddy commented on a change in pull request #22236: Addressed some issues in the tutorial mentioned in discussion #22233

ephraimbuddy commented on a change in pull request #22236:
URL: https://github.com/apache/airflow/pull/22236#discussion_r825773905



##########
File path: docs/apache-airflow/tutorial.rst
##########
@@ -378,62 +378,83 @@ Lets look at another example; we need to get some data from a file which is host
 Initial setup
 ''''''''''''''''''''
 We need to have Docker and Postgres installed.
-We will be using this `docker file <https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html#docker-compose-yaml>`_
+We will be using the `quick-start docker-compose installation <https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html>`_ for the following steps. 
 Follow the instructions properly to set up Airflow.
 
-You can use the postgres_default connection:
+We will also need to create a `connection <https://airflow.apache.org/docs/apache-airflow/stable/concepts/connections.html>`_ to the postgres db. To create one via the web UI, from the "Admin" menu, select "Connections", then click the Plus sign to "Add a new record" to the list of connections.
 
-- Conn id: postgres_default
-- Conn Type: postgres
+Fill in the fields as shown below. Note the Connection Id value, which we'll pass as a parameter for the ``postgres_conn_id`` kwarg.
+
+- Connection Id: tutorial_pg_conn
+- Connection Type: postgres
 - Host: postgres
 - Schema: airflow
 - Login: airflow
 - Password: airflow
+- Port: 5432
 
+Test your connection and if the test is successful, save your connection.
 
-After that, you can test your connection and if you followed all the steps correctly, it should show a success notification. Proceed with saving the connection. For
-
-
-Open up a postgres shell:
-
-.. code-block:: bash
-
-  ./airflow.sh airflow db shell
+Table Creation Tasks
+~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-Create the Employees table with:
+We can use the `PostgresOperator <https://airflow.apache.org/docs/apache-airflow-providers-postgres/stable/operators/postgres_operator_howto_guide.html#creating-a-postgres-database-table>`_ to define tasks that create tables in our postgres db.
 
-.. code-block:: sql
+We'll create one table to serve as the destination for the retrieved data and another to facilitate 
 
-  CREATE TABLE EMPLOYEES
-  (
-      "Serial Number" NUMERIC PRIMARY KEY,
-      "Company Name" TEXT,
-      "Employee Markme" TEXT,
-      "Description" TEXT,
-      "Leave" INTEGER
-  );
+.. code-block:: python
 
-Afterwards, create the Employees_temp table:
+  from airflow.providers.postgres.operators.postgres import PostgresOperator
+   
+  create_employees_table = PostgresOperator(
+      task_id="create_employees_table",
+      postgres_conn_id="tutorial_pg_conn",
+      sql="""
+          CREATE TABLE IF NOT EXISTS employees (
+              "Serial Number" NUMERIC PRIMARY KEY,
+              "Company Name" TEXT,
+              "Employee Markme" TEXT,
+              "Description" TEXT,
+              "Leave" INTEGER
+          );""",
+  )
+  
+  create_employees_temp_table = PostgresOperator(
+      task_id="create_employees_temp_table",
+      postgres_conn_id="tutorial_pg_conn",
+      sql="""
+          DROP TABLE IF EXISTS employees_temp;
+          CREATE TABLE employees_temp (
+              "Serial Number" NUMERIC PRIMARY KEY,
+              "Company Name" TEXT,
+              "Employee Markme" TEXT,
+              "Description" TEXT,
+              "Leave" INTEGER
+          );""",
+  )
 
-.. code-block:: sql
+Optional Note: 
+""""""""""""""
+If you want to abstract these sql statements out of your DAG, you can move the statements sql files somewhere within the ``dags/`` directory and pass the sql file_path (relative to ``dags/``) to the ``sql`` kwarg. For ``employees`` for example, create a ``sql`` directory in ``dags/``, put ``employees`` DDL in ``dags/sql/employees_schema.sql``, and modify the PostgresOperator() to 
 
-  CREATE TABLE EMPLOYEES_TEMP
-  (
-      "Serial Number" NUMERIC PRIMARY KEY,
-      "Company Name" TEXT,
-      "Employee Markme" TEXT,
-      "Description" TEXT,
-      "Leave" INTEGER
-  );
+.. code-block:: python
 
-We are now ready write the DAG.
+  create_employees_table = PostgresOperator(
+      task_id="create_employees_table",
+      postgres_conn_id="tutorial_pg_conn",
+      sql="sql/employees_schema.sql"
+  )
 
+and repeat for the ``employees_temp`` table.
 
+Data Retrival Task

Review comment:
       ```suggestion
   Data Retrieval Task
   ```




-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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