About ResourceDefinition
Class
nifiapi.properties.ResourceDefinition
The ResourceDefinition
class is used to define and manage external resources that processors interact with, such as files, databases, or APIs. It specifies the characteristics and configuration parameters required to access these resources, ensuring they are properly set up and validated before use.
The ResourceDefinition
can be supplied as the value of an optional resource_definition
parameter in a PropertyDescriptor class.
Parameters
Name | Type | Description |
---|---|---|
allow_multiple | Boolean; Default False | The value can contain a comma separated list of entries |
allow_file | Boolean; Default True | The value of a property must be a valid file path |
allow_directory | Boolean; Default False | The value of a property must be a valid directory path |
allow_url | Boolean; Default False | The value of a property must be a valid URL |
allow_text | Boolean; Default False | The value of a property must be a valid UTF-8 encoded string |
e.g.:
from nifiapi.properties import (
PropertyDescriptor,
ResourceDefinition
)
PROPERTY = PropertyDescriptor(
name="Property A",
description='''
Property with Resource Definition.
''',
required=True,
resource_definition=ResourceDefinition(allow_file=True)
)
Note that allow_file
parameter is set to True
by default, and should be explicitly disabled if not needed.
ResourceDefinition(allow_file=False, allow_url=True)
allow_file
The input value of a property must represent a path to an existing and accessible file. If a relative path is given, it will be evaluated in relation to the NiFi root directory.
Example of a failed file resource validation:
allow_directory
The input value of a property must represent a path to an existing and accessible directory. If a relative path is given, it will be evaluated in relation to the NiFi root directory.
allow_text
The input value of a property must be a UTF-8 encoded string.
Allowing Multiple Resource Types
It is possible to define a resource that expects a combination of definitions, as ResourceDefinition
parameters are not mutually exclusive.
Example of a property accepting a list of directories or files:
from nifiapi.properties import (
PropertyDescriptor,
ResourceDefinition
)
PROPERTY = PropertyDescriptor(
name="Property A",
description='''
Property with Resource Definition.
''',
required=True,
resource_definition=ResourceDefinition(
allow_multiple=True,
allow_file=True,
allow_directory=True
)
)
Accessing Resource Reference
Defined resources can be accessed using an instance of the ProcessContext
class, which is supplied to the onSchedule
, onStopped
, or transform methods.
e.g.:
from nifiapi.flowfiletransform import (
FlowFileTransform,
FlowFileTransformResult,
)
from nifiapi.properties import (
ProcessContext,
PropertyDescriptor,
ResourceDefinition
)
class Processor(FlowFileTransform):
(...)
RESOURCE = PropertyDescriptor(
name="File Resource",
description='''
This property accepts a file resource reference.
''',
required=True,
resource_definition=ResourceDefinition(allow_file=True)
)
MULTI_RESOURCE = PropertyDescriptor(
name="Multiple File Resources",
description='''
This property accepts a comma-separated list of file
resource references.
''',
required=True,
resource_definition=ResourceDefinition(
allow_file=True,
allow_multiple=True,
)
)
(...)
def transform(
self, context: ProcessContext, flow_file
) -> FlowFileTransformResult:
'''
Parameters:
context (ProcessContext)
descriptor (PropertyDescriptor)
Returns:
List(Dict)
'''
# Accessing a singluar resource
resource = context.getProperty(self.RESOURCE)\
.asResource()
# Accessing a collection of resources
collection = context.getProperty(self.MULTI_RESOURCE)\
.asResources()
return FlowFileTransformResult("success")