ApacheAirflow-HttpHook与直接使用Python的requests库,以及它们与DAG刷新的关系。
创始人
2024-09-05 09:01:17
0

在 Apache Airflow 中,我们可以选择使用 HttpHook 或直接使用 Python 中的 requests 库来发送 HTTP 请求。使用 HttpHook 可以直接将请求集成到 DAG 中,而不需要单独编写自定义代码。使用 requests 库则需要在 DAG 中编写自定义代码来实现 HTTP 请求。

而在 DAG 刷新方面,使用 HttpHook 会自动从 Airflow metadata database 中获取密钥和密码,以便进行 HTTP 请求。而使用 requests 库则需要手动从 Airflow Vault 中获取这些信息。

下面是使用 HttpHook 和 requests 库发送 HTTP 请求的示例代码:

使用 HttpHook:

from airflow.models import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.providers.http.hooks.http import HttpHook

dag = DAG(
    dag_id='httphook_example',
    schedule_interval=None
)

def send_http_request():
    http_hook = HttpHook(method='GET', http_conn_id='http_api')
    response = http_hook.run(endpoint='/api/data')
    print(response.json())

http_task = PythonOperator(
    task_id='http_task',
    python_callable=send_http_request,
    dag=dag
)

使用 requests 库:

import requests
from airflow.models import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook

dag = DAG(
    dag_id='requests_example',
    schedule_interval=None
)

def send_http_request():
    aws_hook = AwsBaseHook('aws')
    # Get the necessary credentials from AWS Vault
    aws_credentials = aws_hook.get_credentials()
    http_endpoint = 'https://example.com/api/data'
    response = requests.get(http_endpoint, auth=aws_credentials)
    print(response.json())

http_task = PythonOperator(
    task_id='http_task',
    python_callable=send_http_request,
    dag=dag
)

相关内容

热门资讯

透视了解!德扑胜率(wepOk... 透视了解!德扑胜率(wepOke)外挂透明挂辅助器(透视)2024新版教程(发现有挂)-哔哩哔哩 科...
传递经验(微扑克线上)外挂透明... WePoker透视辅助版本稳定性对比与推荐‌:传递经验(微扑克线上)外挂透明挂辅助器(辅助挂)确实是...
透视最新(德州微扑克外挂)外挂... 透视最新(德州微扑克外挂)外挂透明挂辅助器(透视)透视辅助(有挂辅助)-哔哩哔哩1、让任何用户在无需...
揭秘攻略(微扑克发牌)外挂透明... 揭秘攻略(微扑克发牌)外挂透明挂辅助器(辅助挂)透视辅助(果真有挂)-哔哩哔哩关于微扑克发牌机制的,...
一分钟揭秘!德扑ai购买(We... 一分钟揭秘!德扑ai购买(WePoke)外挂透明挂辅助器安装(透视)力荐教程(有挂工具)-哔哩哔哩;...
总算了解(Wepoke是真的)... 总算了解(Wepoke是真的)外挂透明挂辅助器(辅助挂)竟然真的有挂(2021已更新)(哔哩哔哩);...
推荐十款(Wepoke玄学)外... 1、推荐十款(Wepoke玄学)外挂透明挂辅助工具(透视)透视辅助(真的有挂)-哔哩哔哩。2、Wep...
透视脚本!aapoker实测(... 透视脚本!aapoker实测(wepOkE)外挂透明挂辅助软件(辅助挂)教你教程(有挂透视)-哔哩哔...
揭秘几款(fishpoker大... 揭秘几款(fishpoker大菠萝)外挂透明挂辅助工具(透视)软件透明挂(存在有挂)-哔哩哔哩,亲,...
一分钟揭秘!wpk实锤(wep... 1、一分钟揭秘!wpk实锤(wepOkE)外挂透明挂辅助器(透视)必赢教程(有挂细节)-哔哩哔哩;详...