通过 REST 发布 NIFI 模板? [英] Post a NIFI template via REST?
问题描述
我有多个 nifi 服务器,我希望能够通过脚本中的 REST 接口将模板发布到
I have multiple nifi servers that I would like to be able to POST templates to via the REST interface from a script
/controller/templates"端点似乎是支持向我的 Nifi 安装发布任意模板的正确 REST 端点.snippetId"字段让我感到困惑,我如何确定内容将包含模板的片段的 id"?有没有人举个例子说明我如何在不使用 UI 的情况下将模板test.xml"上传到我的服务器?
The "/controller/templates" endpoint appears to be the proper REST endpoint to support POSTing an arbitrary template to my Nifi installation. The "snippetId" field is what is confusing me, how do I determine "The id of the snippet whose contents will comprise the template"? Does anyone have an example of how I can upload a template "test.xml" to my server without having to use the UI?
推荐答案
提供的文档有些混乱,我制定的解决方案来自 https://github.com/aperepel/nifi-api-deploy
The provided documentation is somewhat confusing, and the solution I worked out was derived from the nifi api deploy groovy script at https://github.com/aperepel/nifi-api-deploy
最终,要直接 POST 模板,您可以在 Python 请求中使用以下内容
Ultimately, to POST a template directly, you can use the following in Python requests
requests.post("%s/nifi-api/controller/templates"%(url,), files={"template":open(filename, 'rb')})
其中 filename 是模板的文件名,url 是 nifi 实例的路径.我还没有直接在 curl 中弄清楚,但这应该会让有类似问题的人开始!
Where filename is the filename of your template and url is the path to your nifi instance. I haven't figured it out in curl directly but this should hopefully get folks with a similar question started!
请注意,您也不能上传与现有模板同名的模板.在尝试重新上传之前,请确保删除现有模板.使用 untangle 库解析模板的 XML,下面的脚本工作正常:
Note that you also can't upload a template with the same name as an existing template. Make sure to delete your existing template before attempting to re-upload. Using the untangle library to parse the XML of the template, the following script works just fine:
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])
这篇关于通过 REST 发布 NIFI 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!