dolphinscheduler_批量创建任务

方式一:API 调用

 第三方系统集成就需要通过调用 API 来管理项目、流程  
 12345/dolphinscheduler/doc.html?language=zh_CN&lang=cn
    01.创建 token
	02.使用 Token
	   Postman  ApiPost
	   进行API调试的Postman和编写接口文档的Swagger以及进行压力测试的Jmeter等。

示例

   d8975b0e802dab417415ad24641c2751
   http://10.77.4.183:12345/dolphinscheduler
   01. JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能
   02. HttpClient  okhttp
  0. value, method;
  value:     指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
  method:  指定请求的method类型, GET、POST、PUT、DELETE等;
 
 1、 params,headers;
  params: 指定request中必须包含某些参数值是,才让该方法处理。
  headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求
        
  2、 consumes,produces;
  consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
  produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
   POST请求方式的编码有3种,具体的编码方式如下:
      A:application/x-www-form-urlencoded ==最常见的post提交数据的方式,以form表单形式提交数据
      B:application/json    ==以json格式提交数据
      C:multipart/form-data  ==一般使用来上传文件(较少用)
	在通过requests.post()进行POST请求时,传入报文的参数有两个,一个是data,一个是json。
	 常见的form表单可以直接使用data参数进行报文提交,而data的对象则是python中的字典类型。  
	  01.通过给 data 参数传递一个 dict,我们的数据字典在发出请求时会被自动编码为表单形式
	  02.发送编码为 JSON 形式的数据
	   data=json.dumps(payload)  json=payload    files=files

示例代码

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# ---------------------------

import requests
import json

# base info
headers = {'token':'d641c2751'}
host = "http://22.22.22.22:12345"

## interface
base_url = "/dolphinscheduler/projects/list"
need_url = host+base_url
r = requests.get(need_url,headers=headers, timeout=3)
print(r.text)

comb_url = host+ "/dolphinscheduler/projects"
print(comb_url)
payload = {'pageNo': 1, 'pageSize':1}
r = requests.get(comb_url,  headers=headers, params=payload,timeout=3)
print(r.text)


create_url = host+ "/dolphinscheduler/projects"
payload = {'description': "Data deal", 'projectName':"Dataflow"}
payload = json.dumps(payload)
print(payload)
r = requests.post(create_url,   headers=headers,params= payload)
print(r.url)
print(r.text)

方式二:01.WorkflowAsCode

 PythonGatewayServer, 这是一个 Workflow-as-code 的服务端,与 apiServer 等服务的启动方式相同  
  Workflow-as-code 让用户可以通过 Python API 创建工作流,动态、批量地创建和更新工作流
  
##起服务 org.apache.dolphinscheduler.server.PythonGatewayServer
#安装 Install
 $ pip install apache-dolphinscheduler
 
 # Verify installation is successful, it will show the version of apache-dolphinscheduler, here we use 0.1.0 as example
 $ pydolphinscheduler version
 0.1.0

02.Python API 动态、批量地创建和更新工作流

# 定义工作流属性,包括名称、调度周期、开始时间、使用租户等信息
# from pydolphinscheduler.core.engine import ProgramType
from pydolphinscheduler.core.process_definition import ProcessDefinition
from pydolphinscheduler.tasks.shell import Shell
# from pydolphinscheduler.tasks.condition import FAILURE, SUCCESS, And, Condition
# from pydolphinscheduler.tasks.datax import CustomDataX, DataX
# from pydolphinscheduler.tasks.flink import DeployMode, Flink, ProgramType
# from pydolphinscheduler.tasks.spark import DeployMode, ProgramType, Spark
# from pydolphinscheduler.tasks.map_reduce import 
# from pydolphinscheduler.tasks.switch import Branch, Default, Switch, SwitchCondition

with ProcessDefinition(
    name="tutorial",
    schedule="0 0 0 * * ? *",
    start_time="2022-03-01",
    tenant="caic",
) as pd:
    # 定义4个任务,4个都是 shell 任务,shell 任务的必填参数为任务名、命令信息,这里都是 echo 的 shell 命令
    task_parent = Shell(name="task_parent", command="echo hello pydolphinscheduler")
    task_child_one = Shell(name="task_child_one", command="echo 'child one'")
    task_child_two = Shell(name="task_child_two", command="echo 'child two'")
    task_union = Shell(name="task_union", command="echo union")

    # 定义任务间依赖关系
    # 这里将 task_child_one,task_child_two 先声明成一个任务组,通过 python 的 list 声明
    task_group = [task_child_one, task_child_two]
    # 使用 set_downstream 方法将任务组 task_group 声明成 task_parent 的下游,如果想要声明上游则使用 set_upstream
    task_parent.set_downstream(task_group)

    # 使用位操作符 << 将任务 task_union 声明成 task_group 的下游,同时支持通过位操作符 >> 声明
    task_union << task_group   
posted @ 2022-03-30 14:42  辰令  阅读(1288)  评论(0编辑  收藏  举报