Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
10/29 Airflowの基礎を学ぶハンズオンワークショップ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Hank Ehly
October 28, 2022
Technology
310
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
650
Celeryの紹介と本番運用のTips
hankehly
0
1.6k
ChatGPTを活用した 便利ツールの紹介
hankehly
1
1.4k
Efficient Energy Analytics with Airflow, Spark, and MLFlow
hankehly
0
400
Deferrable Operators入門
hankehly
0
760
【初心者/ハンズオン】Dockerコンテナの基礎知識
hankehly
0
600
Compositeパターン: オブジェクトの階層関係をエレガントに表現する方法
hankehly
0
350
システム/データ品質保証のための Airflow 活用法
hankehly
0
680
海外の記事からコードレビューのBest Practiceを集めてみました
hankehly
0
1k
Other Decks in Technology
See All in Technology
[チョークトーク資料]AWS DevOps Agent を使いこなす / AWS Dev Ops Agent Chalk Talk AWS Summit Japan 2026
kinunori
4
810
Text-to-SQLをAgentCoreで実現し、生成されるSQLの精度を定量的に評価する
yakumo
2
120
自作お家AIエージェントスタックチャンFWで困っている所紹介
74th
0
150
組織における AI-DLC 実践
askul
0
170
Lightning近況報告
kozy4324
0
230
Oracle Cloud Infrastructure:2026年6月度サービス・アップデート
oracle4engineer
PRO
1
380
ご挨拶「10周年を迎える共創ラボのこれまでとこれから」
iotcomjpadmin
0
150
Deep Data Security 機能解説
oracle4engineer
PRO
2
230
IaC コードを資産へ:AWS CDK 社内ライブラリと横断展開 / aws-summit-japan-2026
gotok365
10
1.6k
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
610
脱SaaS!FDEを支えるプロビジョニングと分離設計
knih
0
300
初めてのDatabricks勉強会
taka_aki
2
190
Featured
See All Featured
A designer walks into a library…
pauljervisheath
211
24k
Code Review Best Practice
trishagee
74
20k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
350
Paper Plane
katiecoart
PRO
1
52k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
610
Amusing Abliteration
ianozsvald
1
210
4 Signs Your Business is Dying
shpigford
187
22k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
320
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
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