通过REST发布NIFI模板?

Jos*_*son 6 apache-nifi

我有多个nifi服务器,我希望能够通过脚本从REST接口POST模板

"/ controller/templates"端点似乎是正确的REST端点,支持将任意模板POST到我的Nifi安装. 在此输入图像描述 "snippetId"字段让我很困惑,如何确定"内容将包含模板的片段的ID"?有没有人有一个例子,说明如何在不使用UI的情况下将模板"test.xml"上传到我的服务器?

Ste*_*ane 5

API 在 1.0 中已移至:

POST /process-groups/{id}/templates/upload

例如,使用 Python 的 requests 库:

res = requests.post( "{hostname}/nifi-api/process-groups/{destination_process_group}/templates/upload".format( **args ), 
    files={"template": open( file_path, 'rb')} )
Run Code Online (Sandbox Code Playgroud)


Jos*_*son 4

提供的文档有些令人困惑,我制定的解决方案源自https://github.com/aperepel/nifi-api-deploy上的 nifi api 部署 groovy 脚本

最终,要直接 POST 模板,您可以在 Python 请求中使用以下内容

requests.post("%s/nifi-api/controller/templates"%(url,), files={"template":open(filename, 'rb')})
Run Code Online (Sandbox Code Playgroud)

其中 filename 是模板的文件名,url 是 nifi 实例的路径。我还没有直接在卷曲中弄清楚这一点,但这应该能让有类似问题的人开始!

编辑:请注意,您也无法上传与现有模板同名的模板。请确保在尝试重新上传之前删除现有模板。使用 untangle 库解析模板的 XML,以下脚本可以正常工作:

import untangle, sys, requests

def deploy_template(filename, url):
    p = untangle.parse(filename)
    new_template_name=p.template.name.cdata
    r=requests.get("%s/nifi-api/controller/templates"%(url,), headers={"Accept":"application/json"})

    for each in r.json()["templates"]:
        if each["name"]==new_template_name:
            requests.delete(each["uri"])
    requests.post("%s/nifi-api/controller/templates"%(url,), files={"template":open(filename, 'rb')})

if __name__=="__main__":
    deploy_template(sys.argv[1], sys.argv[2])
Run Code Online (Sandbox Code Playgroud)