简体   繁体   English

使用 Python 创建 Nifi PutSQL 处理器

[英]Create Nifi PutSQL Processor with Python

I want to transfer my Nifi-ETL pipeline (mainly PUTSQL-processors) from my development instance to my production instance of Apache Nifi , optimally with Python for re-usability.我想将我的 Nifi-ETL 管道(主要是 PUTSQL 处理器)从我的开发实例转移到Apache Nifi的生产实例,最好使用Python以实现可重用性。

I thought I would give it a shot to just try and copy-paste them.我想我会试一试,尝试复制粘贴它们。

  1. GET processor from DEV with GET-request on /nifi-api/processors/{id}使用 /nifi-api/processors/{id} 上的 GET 请求从 DEV 获取处理器
  2. PUT processor to PRD Nifi-instance with PUT-request on /nifi-api/processors/{id}在 /nifi-api/processors/{id} 上使用 PUT-request 将处理器 PUT 到 PRD Nifi-instance

Code:代码:

# GET processor and parse to JSON
response = requests.get(nifi_url_dev + 'processors/' + proc_id
                            , headers=header)
processor = json.loads(response.content)


# PUT processor 
processor['revision']['version'] = 0 # reset version
payload = json.dumps(processor).encode('utf8')
response = requests.put(nifi_url_prd + 'processors/' + proc_id
                        , data=payload
                        , headers=header)

This failed on the PUT with a 409 HTTP Conflict Error .这在 PUT 上失败,出现409 HTTP Conflict Error I am guessing this is because I am trying to put a ressource on an URI that expects a resource to exist already at that place.我猜这是因为我试图将一个资源放在一个期望资源已经存在于那个地方的 URI 上。

The documentation lists "Create a processor, Set properties, Schedule" next to the processor APIs, but when looking into it, there is no dedicated API for creation - I decided to go with PUT because it says "Updates a processor" which is the closest thing I can see in there to creating a new one from scratch. 文档在处理器 API 旁边列出了“创建处理器,设置属性,计划”,但是在查看它时,没有专用的 API 用于创建 - 我决定使用 PUT 进行 go,因为它说“更新处理器”,这是我在那里看到的最接近从头开始创建一个新的东西。

Do you have any ideas on how to create processors with Python?您对如何使用 Python 创建处理器有任何想法吗? Either by copying existing ones or creating entirely new ones?通过复制现有的或创建全新的?

So the problem was that the API documentation is a bit misleading... The correct API to create a new processor is process-groups/{process_group_id}/processors .所以问题是 API 文档有点误导......创建新处理器的正确 API 是process-groups/{process_group_id}/processors It is also listed in the docs under "Process Groups" and not under "Processors" despite the description.尽管有描述,它也列在“进程组”下的文档中,而不是“处理器”下。

The following worked for me - it was necessary to adapt the json a bit though: mainly to delete any IDs of the Dev-environment.以下对我有用 - 有必要稍微调整 json:主要是删除开发环境的任何 ID。

# GET processor and parse to JSON
response = requests.get(nifi_url_dev + 'processors/' + proc_id
                            , headers=header)
processor = json.loads(response.content)


# PUT processor 
del processor["id"]  # Processor ID cannot be specified.
del processor["uri"]  # Processor ID cannot be specified.
del processor["component"]["id"]  # Processor ID cannot be specified.

del processor["component"]["parentGroupId"]  # Parent process group id should not be specified.
# If specified, the parent process group id must be the same as specified in the POST-URI.

processor['revision']['version'] = 0 # reset version
payload = json.dumps(processor).encode('utf8')
response = requests.post(nifi_url_prd + 'process-groups/' + process_group + '/processors'
                        , data=payload
                        , headers=header)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM