助力工业物联网,工业大数据之服务域:AirFlow的架构组件【三十二】_airflow大数据组件

80 阅读3分钟
	- 开发一个Python程序,程序文件中需要包含以下几个部分
	- 注意:该文件的运行不支持utf8编码,**不能写中文**
	- **step1:导包**
	
	 
	```
	# 必选:导入airflow的DAG工作流
	from airflow import DAG
	# 必选:导入具体的TaskOperator类型
	from airflow.operators.bash import BashOperator
	# 可选:导入定时工具的包
	from airflow.utils.dates import days_ago
	
	```
	 ![image-20211015103936196](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/6517a059ae2141eeadb6130be6ad4960~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771854293&x-signature=R4aR1vVWlBDANwN0QQFl%2FDYGV9k%3D)
	- **step2:定义DAG及配置**
	
	 
	```
	# 当前工作流的基础配置
	default_args = {
	    # 当前工作流的所有者
	    'owner': 'airflow',
	    # 当前工作流的邮件接受者邮箱
	    'email': ['airflow@example.com'],
	    # 工作流失败是否发送邮件告警
	    'email\_on\_failure': True,
	    # 工作流重试是否发送邮件告警
	    'email\_on\_retry': True,
	    # 重试次数
	    'retries': 2,
	    # 重试间隔时间
	    'retry\_delay': timedelta(minutes=1),
	}
	
	# 定义当前工作流的DAG对象
	dagName = DAG(
	    # 当前工作流的名称,唯一id
	    'airflow\_name',
	    # 使用的参数配置
	    default_args=default_args,
	    # 当前工作流的描述
	    description='first airflow task DAG',
	    # 当前工作流的调度周期:定时调度【可选】
	    schedule_interval=timedelta(days=1),
	    # 工作流开始调度的时间
	    start_date=days_ago(1),
	    # 当前工作流属于哪个组
	    tags=['itcast\_bash'],
	)
	
	```
	
		* 构建一个DAG工作流的实例和配置
	- **step3:定义Tasks**
	
	
		* Task类型:http://airflow.apache.org/docs/apache-airflow/stable/concepts/operators.html
		* 常用
		
		
			+ [`BashOperator`](https://gitee.com/vip204888) - executes a bash command
				- 执行Linux命令
			+ [`PythonOperator`](https://gitee.com/vip204888) - calls an arbitrary Python function
				- 执行Python代码
			+ [`EmailOperator`](https://gitee.com/vip204888) - sends an email
				- 发送邮件的
		* 其他
		
		
			+ [`MySqlOperator`](https://gitee.com/vip204888)
			+ [`PostgresOperator`](https://gitee.com/vip204888)
			+ [`MsSqlOperator`](https://gitee.com/vip204888)
			+ [`OracleOperator`](https://gitee.com/vip204888)
			+ [`JdbcOperator`](https://gitee.com/vip204888)
			+ [`DockerOperator`](https://gitee.com/vip204888)
			+ [`HiveOperator`](https://gitee.com/vip204888)
			+ [`PrestoToMySqlOperator`](https://gitee.com/vip204888)
			+ ……
		* BashOperator:定义一个Shell命令的Task
		
		 
		```
		# 导入BashOperator
		from airflow.operators.bash import BashOperator
		# 定义一个Task的对象
		t1 = BashOperator(
			# 指定唯一的Task的名称
		    task_id='first\_bashoperator\_task',
			# 指定具体要执行的Linux命令
		    bash_command='echo "hello airflow"',
			# 指定属于哪个DAG对象
		    dag=dagName
		)
		
		```
		* PythonOperator:定义一个Python代码的Task
		
		 
		```
		# 导入PythonOperator
		from airflow.operators.python import PythonOperator
		
		# 定义需要执行的代码逻辑
		def sayHello():
		    print("this is a programe")
		
		#定义一个Task对象
		t2 = PythonOperator(
		    # 指定唯一的Task的名称
		    task_id='first\_pyoperator\_task',
		    # 指定调用哪个Python函数
		    python_callable=sayHello,
		    # 指定属于哪个DAG对象
		    dag=dagName
		)
		
		```​


	- **step4:运行Task并指定依赖关系**
	
	
		* 定义Task
		
		 
		```
		Task1:runme_0
		Task2:runme_1
		Task3:runme_2
		Task4:run_after_loop
		Task5:also_run_this
		Task6:this_will_skip
		Task7:run_this_last
		
		```
		* 需求
		
		
			+ Task1、Task2、Task3并行运行,结束以后运行Task4
			+ Task4、Task5、Task6并行运行,结束以后运行Task7![image-20211005121040679](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/68b2b57a9f8e4fb9be00d7c2568297fd~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771854293&x-signature=cZ5fYaw4wg0rWMNuiu1z0z49lyE%3D)
		* 代码
		
		 
		```
		task1 >> task4
		task2 >> task4
		task3 >> task4
		task4 >> task7
		task5 >> task7
		task6 >> task7
		
		```
		* 如果只有一个Task,只要直接写上Task对象名称即可
		
		 
		```
		task1
		
		```
+ **提交Python调度程序**


	- 哪种提交都需要等待一段时间
	- 自动提交:需要等待自动检测
	
	
		* 将开发好的程序放入AirFlow的DAG Directory目录中
		* 默认路径为:/root/airflow/dags
	- 手动提交:手动运行文件让airflow监听加载
	
	 
	```
	python xxxx.py
	
	```
	- 调度状态
	
	
		* No status (scheduler created empty task instance):调度任务已创建,还未产生任务实例
		* Scheduled (scheduler determined task instance needs to run):调度任务已生成任务实例,待运行
		* Queued (scheduler sent task to executor to run on the queue):调度任务开始在executor执行前,在队列中
		* Running (worker picked up a task and is now running it):任务在worker节点上执行中
		* Success (task completed):任务执行成功完成
  • 小结

    • 掌握AirFlow的开发规则

img img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取