Dave,

For ScriptedRecordSetWriter (and all the scripted Controller
Services), you provide the properties yourself, rather than (like
ExecuteScript) defining dynamic properties and referring to them from
the script. I have an example [1] of using Record controller services
from InvokeScriptedProcessor, but you can use the same approach to
define the DistributedCacheMapClient controller service property, add
it to the supported properties for the RecordSetWriterFactory
implementation in your script (see the getSupportedPropertyDescriptors
method in [1]) and pass either the client or the context to your
RecordSetWriter, then you can use it in your write() method.

This would make a good blog post, if I find the time I may write it
up, but of course if you try it and it works, please feel free to
write it up or share with us!

Regards,
Matt

[1] http://funnifi.blogspot.com/2019/04/record-processing-with.html

On Fri, Aug 21, 2020 at 12:30 PM davide <[email protected]> wrote:
>
> All,
>
> I am new to the scripted record environment but have done Groovy scripts
> with ExecuteScript and other customizations with the interface... So not a
> power user but maybe mid-tier.
>
> I am trying to create a ScriptedRecordSetWriter that accesses a controller
> service (map cache).  We need to do a lookup for each record and doing it in
> broken out flow files won't scale.
>
> I can do this in an ExecuteScript with something like this:
>
> -----------------
> import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient
> import org.apache.nifi.distributed.cache.client.Serializer
> import org.apache.nifi.distributed.cache.client.Deserializer
> import java.nio.charset.StandardCharsets
>
> def ff = session.get()
>
> if(!ff) return
>
> def StringSerializer = {value, out ->
> out.write(value.getBytes(StandardCharsets.UTF_8))} as Serializer<String>
> def StringDeserializer = { bytes -> new String(bytes) } as
> Deserializer<String>
>
> def myDistClient =
> AggMapClientService.*asControllerService*(DistributedMapCacheClient)
> mn = ff.getAttribute('record.mn')
> def result = myDistClient.get(mn, StringSerializer, StringDeserializer)
>
> log.info("Result = $result")
> session.transfer(ff, REL_SUCCESS)
> ------------------
>
> But this only works because I am passing the UUID of the controller service
> as a property.  This shows up in the script as a type PropertyValue and
> "asControllerService" becomes possible.
>
> What I can't find is any example of how to access properties in the
> ScriptedRecordSetWriter.  And it may be that I can't access controller
> services in the script for this service.
>
> Any pointers/examples would be greatly appreciated.  If someone has tried to
> access a controller service from a ScriptedRecordSetWriter and determined it
> is not possible, I would appreciate that knowledge as well!
>
> Thanks in advance
>
> Dave
>
>
>
>
>
> --
> Sent from: http://apache-nifi-users-list.2361937.n4.nabble.com/

Reply via email to