Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
10/29 Airflowの基礎を学ぶハンズオンワークショップ
Search
Hank Ehly
October 28, 2022
Technology
340
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
10/29 Airflowの基礎を学ぶハンズオンワークショップ
Hank Ehly
October 28, 2022
More Decks by Hank Ehly
See All by Hank Ehly
Fivetranでデータ移動を自動化する
hankehly
0
680
Celeryの紹介と本番運用のTips
hankehly
0
1.7k
ChatGPTを活用した 便利ツールの紹介
hankehly
1
1.4k
Efficient Energy Analytics with Airflow, Spark, and MLFlow
hankehly
0
430
Deferrable Operators入門
hankehly
0
810
【初心者/ハンズオン】Dockerコンテナの基礎知識
hankehly
0
620
Compositeパターン: オブジェクトの階層関係をエレガントに表現する方法
hankehly
0
370
システム/データ品質保証のための Airflow 活用法
hankehly
0
700
海外の記事からコードレビューのBest Practiceを集めてみました
hankehly
0
1.1k
Other Decks in Technology
See All in Technology
Lambda MicroVMsが分からなすぎたので使い所を1から考えてみた
tsukuboshi
2
430
Deep Data Security 機能解説
oracle4engineer
PRO
2
730
SREでアラート疲れを 解決しよう!
kairim0
1
210
自作バイナリを動かす「入口」8選
puhitaku
1
1.1k
AIがコードを書く時代に、エンジニアは何で自分の価値を証明するのか / In an era where AI writes code how do engineers prove their value
carta_engineering
0
530
3つのアプローチで目指す チームを強くするAI駆動開発
thkt
0
140
LLM機能を自作して分かるSnowflake Cortex AIの強み
nayuts
0
330
AI時代のAPI開発を加速する品質ガードレール / API Quality Guardrails in the AI Era
yokawasa
0
160
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
550
宣言型部分更新は何を解決しどこへ向かうのか
shuta13
0
140
AIに攻撃される前に、AIに攻撃させる
tsuchikazu
0
160
20260930_Gemma4_Hands-on
tsho
0
240
Featured
See All Featured
How to make the Groovebox
asonas
2
2.5k
ラッコキーワード サービス紹介資料
rakko
1
5.2M
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
So, you think you're a good person
axbom
PRO
2
2.2k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
400
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
260
Ethics towards AI in product and experience design
skipperchong
2
420
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Paper Plane
katiecoart
PRO
4
54k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
270
Transcript
Airflowの基礎を学ぶハンズオ ンワークショップ Tokyo Apache Airflow Meetup 2022/10/29 1
• Hank Ehly(名:ハンク 姓:イーリー) • ENECHANGE株式会社 • https://qiita.com/hankehly • https://github.com/hankehly
• https://connpass.com/user/hankehly 自己紹介 2
スポンサーのご紹介 https://www.astronomer.io/ 3 https://enechange.co.jp/recruit/require ment/django-engineer/ Airflowを活用し、電力時系列データの分析を 中心としたアプリケーション企画 /開発
アジェンダ • 課題 #1 - Example DAGを実行する ◦ Airflowの起動方法 /
DAG実行 / ログ確認 • 課題 #2 - 簡易的なDAGを作る ◦ DAG/Taskの定義方法 / XComやDAGパラメータの使い方 • 課題 #3 - 高度なDAGを作る ◦ SQLite DBにWeb APIから取得したデータを挿入する 4
課題 #1 - Example DAGを実行する • Airflowを起動する • Example DAGを有効にして実行する
• タスクログを確認する 5
Airflow を Python 仮想環境 (venv) にインストールする場合 6 # venv モジュールのインストール
(Debian/Ubuntu の場合) $ sudo apt-get install -y python3-pip python3-venv # venv を作成し、中に入る $ python3 -m venv (任意のパス) # venv の作成 $ source (上で指定したパス)/bin/activate # venv に入る $ pip install --upgrade pip setuptools wheel # pipやその関連ツール自体をアップグレードしておく ... Successfully installed pip-22.3 setuptools-65.5.0 wheel-0.37.1 # 以下, venv に入った状態で Airflow のインストールを行う # venv から出る $ deactivate # 不要になった venv を削除 $ rm -rf (上で指定したパス)
課題 #1 - Example DAGを実行する Airflowを起動する (Standalone) Quick Start -
Airflow Documentation # Airflowのプロジェクトディレクトリを作る mkdir ~/airflow-test && cd airflow-test export AIRFLOW_HOME=~/airflow-test # Pythonパッケージをインストールする AIRFLOW_VERSION=2.4.2 PYTHON_VERSION="$(python3 --version | cut -d " " -f 2 | cut -d "." -f 1-2)" CONSTRAINT_URL="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERS ION}/constraints-${PYTHON_VERSION}.txt" pip install "apache-airflow==${AIRFLOW_VERSION}" "apache-airflow-providers-sqlite" --constraint "${CONSTRAINT_URL}" # standaloneコマンドは、DB初期化 / ユーザー作成 / 環境を起動してくれます。 # コンソールに出力されるユーザー名/PWをコピーして、ブラウザで localhost:8080 を開いてログインする airflow standalone 7 前提: • Windowsの方はWSL2 • Python 3.7以上 • Airflowバージョン 2.4以上 ※一部の Linux ディストリビューション( CentOS 7・8、Ubuntu 18.04 など)は、標準で付属する Python のバージョンが 3.6 の場合があります。 Python 3.6 向けの Airflow 2.3 以降の constraints が提供されていない ため、2.3 未満の constraints(2.2.5 が最新)を使う必要があります。
課題 #1 - Example DAGを実行する Airflowを起動する (Docker) Running Airflow in
Docker - Airflow Documentation # Airflowのプロジェクトディレクトリを作り、公式 docker-compose.yml をダウンロードす る mkdir ~/airflow-test && cd airflow-test curl -LfO 'https://airflow.apache.org/docs/apache-airflow/2.4.2/docker-compose.yaml' # 環境を立ち上げて、ブラウザで localhost:8080 を開いて、ユーザー名 :airflow / PW:airflow でログインする docker compose up -d 8 前提: • Windowの方 WSL2 を使用する • Python 3.7以上 • Airflowバージョン 2.4以上
課題 #1 - Example DAGを実行する DAGを有/無効化する 過去の実行履歴 実行スケジュール 手動実行する 9
課題 #1 - Example DAGを実行する タスクログ画面 タスクインスタンスのコンテキストメニュー ホーム画面 Graph画面 10
課題 #2 - 簡易的なDAGを作る • DAGを定義する • Pythonタスクを作る • Bashタスクを作る
• XComでタスク間にデータを渡す • DAGパラメータを出力する 11
課題 #2 - 簡易的なDAGを作る / DAGを定義する import datetime from airflow
import DAG with DAG( dag_id="exercise_02", description="A simple tutorial DAG", schedule="0 * * * *", # schedule_interval="0 * * * *", # Airflow 2.4未満の場合 start_date=datetime.datetime(2021, 1, 1), tags=["example"], ) as dag: pass 12
課題 #2 - 簡易的なDAGを作る / Pythonタスクを作る import datetime from airflow
import DAG with DAG( dag_id="exercise_02", description="A simple tutorial DAG", schedule="0 * * * *", # schedule_interval="0 * * * *", # Airflow 2.4未満の場合 start_date=datetime.datetime(2021, 1, 1), tags=["example"], ) as dag: @dag.task def compute_random_number(): import random num = random.randint(1, 10) return num random_number = compute_random_number() 13
課題 #2 - 簡易的なDAGを作る / Bashタスクを作る import datetime from airflow
import DAG from airflow.operators.bash import BashOperator with DAG( dag_id="exercise_02", description="A simple tutorial DAG", schedule="0 * * * *", # schedule_interval="0 * * * *", # Airflow 2.4未満の場合 start_date=datetime.datetime(2021, 1, 1), tags=["example"], ) as dag: @dag.task def compute_random_number(): import random num = random.randint(1, 10) return num random_number = compute_random_number() bash_task_1 = BashOperator( task_id="bash_task_1", bash_command="echo 'hello world'", ) random_number >> bash_task_1 14
課題 #2 - 簡易的なDAGを作る / XComでタスク間にデータを渡す import datetime from airflow
import DAG from airflow.decorators import task from airflow.operators.bash import BashOperator with DAG( dag_id="exercise_02", description="A simple tutorial DAG", schedule="0 * * * *", # schedule_interval="0 * * * *", # Airflow 2.4未満の場合 start_date=datetime.datetime(2021, 1, 1), tags=["example"], ) as dag: @dag.task def compute_random_number(): import random num = random.randint(1, 10) return num random_number = compute_random_number() bash_task_1 = BashOperator( task_id="bash_task_1", bash_command="echo 'random number: {{ ti.xcom_pull(\"compute_random_number\") }}'", ) random_number >> bash_task_1 15 Templates reference
課題 #2 - 簡易的なDAGを作る / DAGパラメータを出力する import datetime from airflow
import DAG from airflow.operators.bash import BashOperator with DAG( dag_id="exercise_02", description="A simple tutorial DAG", schedule="0 * * * *", # schedule_interval="0 * * * *", # Airflow 2.4未満の場合 start_date=datetime.datetime(2021, 1, 1), tags=["example"], ) as dag: @dag.task def compute_random_number(): import random num = random.randint(1, 10) return num random_number = compute_random_number() bash_task_1 = BashOperator( task_id="bash_task_1", bash_command="echo 'random number: {{ ti.xcom_pull(\"compute_random_number\") }}'", ) bash_task_2 = BashOperator( task_id="bash_task_2", bash_command="echo 'message: {{ dag_run.conf[\"message\"] }}'", ) random_number >> bash_task_1 >> bash_task_2 16 Templates reference
課題 #3 - 高度なDAGを作る 事前準備 • SQLiteをインストールする • SQLite DBを作る
Airflow開発 • SQLite テーブルを作る • Web APIからデータを取得する ◦ https://jsonplaceholder.typicode.com/users • SQLiteテーブルにデータを挿入する 17 # ubuntu $ sudo apt-get install -y sqlite3 # mac $ brew install sqlite