-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIFI-13745 Added StateManager Support to Python Processor API (#9419)
Signed-off-by: David Handermann <exceptionfactory@apache.org>
- Loading branch information
Showing
8 changed files
with
419 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...sion-bundle/nifi-python-test-extensions/src/main/resources/extensions/TestStateManager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from nifiapi.componentstate import Scope, StateManager, StateException | ||
from nifiapi.flowfilesource import FlowFileSource, FlowFileSourceResult | ||
from nifiapi.properties import PropertyDescriptor, StandardValidators, PropertyDependency | ||
|
||
class TestStateManager(FlowFileSource): | ||
class Java: | ||
implements = ['org.apache.nifi.python.processor.FlowFileSource'] | ||
|
||
class ProcessorDetails: | ||
version = '0.0.1-SNAPSHOT' | ||
description = '''A Python source processor that uses StateManager.''' | ||
tags = ['text', 'test', 'python', 'source'] | ||
|
||
METHOD_TO_TEST = PropertyDescriptor( | ||
name='StateManager Method To Test', | ||
description='''The name of StateManager's method that should be tested.''', | ||
required=True | ||
) | ||
|
||
property_descriptors = [METHOD_TO_TEST] | ||
|
||
def __init__(self, **kwargs): | ||
pass | ||
|
||
def getPropertyDescriptors(self): | ||
return self.property_descriptors | ||
|
||
def create(self, context): | ||
method_to_test = context.getProperty(self.METHOD_TO_TEST).getValue() | ||
flowfile_attributes = None | ||
state_manager = context.getStateManager() | ||
|
||
match method_to_test.lower(): | ||
case 'setstate': | ||
new_state = {'state_key_1': 'state_value_1'} | ||
state_manager.setState(new_state, Scope.CLUSTER) | ||
case 'getstate': | ||
flowfile_attributes = state_manager.getState(Scope.CLUSTER).toMap() | ||
case 'replace': | ||
old_state = state_manager.getState(Scope.CLUSTER) | ||
new_state = {'state_key_2': 'state_value_2'} | ||
state_manager.replace(old_state, new_state, Scope.CLUSTER) | ||
case 'clear': | ||
state_manager.clear(Scope.CLUSTER) | ||
case _: | ||
pass | ||
|
||
return FlowFileSourceResult(relationship='success', attributes=flowfile_attributes, contents='Output FlowFile Contents') |
40 changes: 40 additions & 0 deletions
40
...le/nifi-python-test-extensions/src/main/resources/extensions/TestStateManagerException.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from nifiapi.componentstate import Scope, StateManager, StateException | ||
from nifiapi.flowfilesource import FlowFileSource, FlowFileSourceResult | ||
|
||
class TestStateManagerException(FlowFileSource): | ||
class Java: | ||
implements = ['org.apache.nifi.python.processor.FlowFileSource'] | ||
|
||
class ProcessorDetails: | ||
version = '0.0.1-SNAPSHOT' | ||
description = '''A Python source processor that uses StateManager.''' | ||
tags = ['text', 'test', 'python', 'source'] | ||
|
||
def __init__(self, **kwargs): | ||
pass | ||
|
||
def create(self, context): | ||
state_manager = context.getStateManager() | ||
flowfile_attributes = None | ||
try: | ||
new_state = {'state_key_1': 'state_value_1'} | ||
state_manager.setState(new_state, Scope.CLUSTER) | ||
except StateException as state_exception: | ||
flowfile_attributes = {'exception_msg': str(state_exception)} | ||
|
||
return FlowFileSourceResult(relationship='success', attributes=flowfile_attributes, contents='Output FlowFile Contents') |
Oops, something went wrong.