Slide 1

Slide 1 text

Bonus: Data Quality in Apache Airflow

Slide 2

Slide 2 text

แนะนําตัวผู้สอน Punsiri Boonyakiat (Beat) Senior Data Engineer at CJ Express

Slide 3

Slide 3 text

ตารางเรียน ● What and Why Data Quality Assurance (DQA) ● Techniques for DQA ● DQA Landscape in Apache Airflow ● Demo ● Short Sli.do Q&A

Slide 4

Slide 4 text

ระหว่างฟังถ้ามีคําถาม พิมพ์ได้ที่นี่เลย ยิ่งถาม ยิ่งไดเรียนรู เขาไปที่เว็บ sli.do พิมพชื่อหอง #r2de3-dqa (สำหรับคนที่ไมชอบออกสื่อ เว็บนี้ถามแบบ Anonymous ไดเลย ไมตอง พิมพชื่อ)

Slide 5

Slide 5 text

Building Data Platform

Slide 6

Slide 6 text

Data Platform Features ● Data Domain ● Data Quality ● Data Discovery ● Data Governance ● Data Observability ● DataOps ● FinOps

Slide 7

Slide 7 text

Data Pipeline without Quality Assurance

Slide 8

Slide 8 text

Why Data Quality ? ● Avoids Business Risks: Poor data can lead to incorrect decisions, financial losses, and damaged business operations. Ensures Data Usability: Data must be accurate, consistent, and reliable to serve its intended purpose effectively. Builds Trust: High-quality data fosters trust among data consumers, leading to better collaboration and usage. Supports Compliance: Maintaining data quality helps meet regulatory requirements and standards. Enhances Efficiency: Clean, well-structured data improves operational efficiency and reduces time spent on data correction.

Slide 9

Slide 9 text

● Size Compliance: Data must meet predefined size and volume standards for efficient processing and analysis. ● Accuracy Assurance: Data accuracy must fall within acceptable error margins to maintain reliability for decision-making. ● Timely Delivery: Data must be delivered and processed within specific timeframes to align with business objectives. ● Scheduled Execution: Pipelines must operate according to established schedules to ensure seamless operations. ● Data Privacy: Data must be free of sensitive or confidential information to comply with regulations and mitigate risks. Example of Requirements

Slide 10

Slide 10 text

Data Quality Challenge

Slide 11

Slide 11 text

Dimensions of Data Quality

Slide 12

Slide 12 text

Completeness Record is Missing: บาง record ข้อมูลชุดนั้นไม่ถูกบันทึกเลย Value is Missing : ข้อมูลบางส่วนในฟิลด์นั้นๆ ไม่ถูกกรอก Data Truncation: ข้อมูลถูกเก็บไม่ครบตามที่ควรจะเป็น (เช่น ตัวอักษรเกินขนาดที่กําหนด)

Slide 13

Slide 13 text

Uniqueness

Slide 14

Slide 14 text

Accuracy ตัวอย่างเช่น ข้อมูลจุดรับส่ง ระยะทาง

Slide 15

Slide 15 text

Validity ● Data Validity based on Business rules ● Data Validity based Sequence of events

Slide 16

Slide 16 text

Consistency ● Record level data consistency across source and target ● Data consistency between domain ● Data consistency across time ● Data consistency in data representation/ system

Slide 17

Slide 17 text

Timeliness - Late for Business Process

Slide 18

Slide 18 text

ทบทวท Airflow นิดนึง

Slide 19

Slide 19 text

Apache Airflow Basic Recap

Slide 20

Slide 20 text

Apache Airflow Basic Recap

Slide 21

Slide 21 text

Techniques for DQA

Slide 22

Slide 22 text

Techniques for DQA - Data Validation

Slide 23

Slide 23 text

Techniques for DQA - Data Reconciliation

Slide 24

Slide 24 text

Techniques for DQA - Data Pipeline Unit Test

Slide 25

Slide 25 text

Data Monitoring + Observability

Slide 26

Slide 26 text

DQA Landscape in Apache Airflow

Slide 27

Slide 27 text

Data Quality Tools ● SQL check operators: A group of operators in the Common SQL provider package that you can use to define data quality checks using Python dictionaries and SQL from within your DAGs. ● Great Expectations: An open source data validation framework where checks are defined in JSON. Airflow offers a provider package including the GreatExpectationsOperator for easy integration. ● Soda: An open source data validation framework that uses YAML to define checks which can be run in Airflow using the BashOperator. Soda also offers the ability to write any custom checks using SQL. ● dbt test: A testing framework for dbt models. The easiest way to run dbt models including dbt test within Airflow is with the Cosmos package.

Slide 28

Slide 28 text

How to do data validation in Airflow? Type 1: Standalone Data-Quality DAG 1. มีการสร้าง DAG แยกเพื่อการตรวจสอบคุณภาพข้อมูล ทํางานทุกวันหลังจาก pipeline หลัก 2. หากตรวจสอบล้มเหลว จะทําการแจ้งเตือนผ่าน และสามารถรันการตรวจสอบใหม่เฉพาะส่วนที่มีปัญหาได้ 3. DAG นี้สามารถเปิด-ปิดได้ง่าย โดยไม่กระทบ DAG เดิม หลัก Type 2: Integrated Checks During Table Creation 1. เพิ่มการตรวจสอบคุณภาพข้อมูลเข้าในขั้นตอนการสร้าง Data Pipeline 2. หากพบข้อผิดพลาดจะหยุดขั้นตอนการทํางานของ pipeline ทันที extract_data_from_source check_data load_data ❌

Slide 29

Slide 29 text

Data Quality Check Type Table-Level: จะเป็นการทดสอบระดับโครงสร้างของ table เช่น Schema, Data Type และ Records count ของทั้ง table Column-Level : การตรวจสอบระดับคอลัมน์ เช่น ถ้าเป็น primary หรือ secondary key column ต้องไม่มีค่า NULL และมีความ unique, สําหรับ column ที่เป็น numerical หรือตัวเลข ต้องอยู่ใน range ที่กําหนด, สําหรับค่าที่เป็น string หรือข้อมความสามารถกําหนด pattern เพื่อทดสอบได้ Row-Level: การตรวจสอบทั่วไป เช่น การเปรียบเทียบค่าหลายคอลัมน์ในแต่ละแถว และการตรวจ สอบว่าไม่มีค่าที่ขัดแย้งกันปรากฏในแถวเดียวกัน Over several tables: ตรวจสอบความสอดคล้องของข้อมูลระหว่างหลายตาราง

Slide 30

Slide 30 text

SQL Common Provider

Slide 31

Slide 31 text

SQL Check Operator

Slide 32

Slide 32 text

Great Expectation (GX)

Slide 33

Slide 33 text

https://greatexpectations.io/ Great Expectation (GX)

Slide 34

Slide 34 text

Expectation Gallery https://greatexpectations.io/expectations/

Slide 35

Slide 35 text

DEMO: Let’s go to the code Repo: https://github.com/punsiriboo/data-quality- with-apache-airflow 1. SLA for Data Freshness Monitoring 2. Data Validation with SQL Check Operator 3. Data Validation with Great Expectation 4. Notification to Slack 5. Simple Data Reconciliation 6. Apache Airflow Unit Test

Slide 36

Slide 36 text

Demo Architecture Explanation SQLite load transform validate BigQuery

Slide 37

Slide 37 text

DEMO: Data Freshness with SLA - Service Level Agreement

Slide 38

Slide 38 text

SLA - Service Level Agreement ● Measure task time from DAG start, not task start. ● Trigger alert if task exceeds SLA. ● SLAs apply only to scheduled DAG runs. ● 2 SLA levels: DAG and Task.

Slide 39

Slide 39 text

SLA - Service Level Agreement (Task Level)

Slide 40

Slide 40 text

SSLA - Service Level Agreement SLA for Data Freshness monitoring

Slide 41

Slide 41 text

DEMO: Data Validation with SQL Check Operator

Slide 42

Slide 42 text

Common SQL Provider

Slide 43

Slide 43 text

Common SQL Provider 1. SQLCheckOperator: ตรวจสอบคุณภาพข้อมูลด้วย Sql 2. SQLTableCheckOperator: ตรวจสอบคุณภาพข้อมูลระดับ table 3. SQLColumnCheckOperator: ตรวจสอบคุณภาข้อมูลระกับ column 4. SQLValueCheckOperator: ตรวจสอบความถูกต้องของข้อมูลด้วยการ check value ตามที่คาดหวัง 5. SQLThresholdCheckOperator: ตรวจสอบความถูกต้องของข้อมูลด้วยการ check value ตามที่คาด หวังแบบกําหนดช่วงขอบเขต min max 6. SQLIntervalCheckOperator: ตรวจสอบข้อมูลเทียบกับช่วงเวลาย้อนหลัง

Slide 44

Slide 44 text

SQLCheckOperator

Slide 45

Slide 45 text

SQLTableCheckOperator

Slide 46

Slide 46 text

SQLColumnCheckOperator

Slide 47

Slide 47 text

DEMO: Data Validation with Great Expectation

Slide 48

Slide 48 text

Great Expectation Provider

Slide 49

Slide 49 text

Great Expectation Components

Slide 50

Slide 50 text

Great Expectation Configuration

Slide 51

Slide 51 text

Great Expectation Operator https://registry.astronomer.io/providers/airflow-provider-great-expectations/versions/l atest/modules/GreatExpectationsOperator

Slide 52

Slide 52 text

Expectations Suite

Slide 53

Slide 53 text

Great Expectation Data Doc

Slide 54

Slide 54 text

DEMO: Notification to Slack

Slide 55

Slide 55 text

Slack Notification

Slide 56

Slide 56 text

DEMO: Simple Data Reconciliation

Slide 57

Slide 57 text

Data Reconciliation

Slide 58

Slide 58 text

DEMO: Apache Airflow Unit Testing

Slide 59

Slide 59 text

Data Life Cycle

Slide 60

Slide 60 text

Techniques for DQA - Data Pipeline Unit Test

Slide 61

Slide 61 text

Python UnitTest with Pytest

Slide 62

Slide 62 text

Example: Simple Test

Slide 63

Slide 63 text

Example: Test DAG integrity

Slide 64

Slide 64 text

Summary

Slide 65

Slide 65 text

สรุปความแตกต่าง!

Slide 66

Slide 66 text

References ● https://www.astronomer.io/docs/learn/data-quality ● https://www.astronomer.io/blog/orchestration-or-how-to-become-a-data-driven-company ● https://www.astronomer.io/blog/how-to-keep-data-quality-in-check-with-airflow/ ● https://www.astronomer.io/docs/learn/airflow-sql-data-quality#example-sqlcolumncheckoperator ● https://www.astronomer.io/docs/learn/testing-airflow?tab=traditional#debug-interactively-with-dagtest

Slide 67

Slide 67 text

ฝากบทความ ไปตามอ่านกันได้นะ 🤩 https://medium.com/@punsiriboonyakiat