nifi-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Harding <mikeyhard...@gmail.com>
Subject Re: javascript executescript processor
Date Mon, 07 Mar 2016 19:12:52 GMT
Thanks Matt! worked a treat.

Mike

On 7 March 2016 at 17:42, Matt Burgess <mattyb149@gmail.com> wrote:

> Looks like there are a few more differences between Sun Rhino (Java 7) and
> Nashorn (Java 8) than I thought.  For your importClass statements above,
> those shouldn't be in quotes. Also I couldn't get the one-method interface
> override to work even though the doc says it should, so I used an anonymous
> class with 'process' mapped to the function.
>
> I will update the post or add a new one soon; in the meantime, here's a
> Java 7 version of the script I got working:
>
> importClass(org.apache.commons.io.IOUtils)
> importClass(java.nio.charset.StandardCharsets)
> importClass(org.apache.nifi.processor.io.StreamCallback)
>
> var flowFile = session.get();
> if (flowFile != null) {
>
> flowFile = session.write(flowFile,
>         new StreamCallback() {process : function(inputStream,
> outputStream) {
>             var text = IOUtils.toString(inputStream,
> StandardCharsets.UTF_8)
>    var obj = JSON.parse(text)
>             var newObj = {
> "Range": 5,
> "Rating": obj.rating.primary.value,
> "SecondaryRatings": {}
>    }
>    for(var key in obj.rating){
>             var attrName = key;
>             var attrValue = obj.rating[key];
>   if(attrName != "primary") {
>   newObj.SecondaryRatings[attrName] = {"id": attrName, "Range": 5,
> "Value": attrValue.value};
> }
>             }
>   outputStream.write(new java.lang.String(JSON.stringify(newObj, null,
> '\t')).getBytes(StandardCharsets.UTF_8))
>         }})
> flowFile = session.putAttribute(flowFile, "filename",
> flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
> session.transfer(flowFile, REL_SUCCESS)
> }
>
> Regards,
> Matt
>
> On Mon, Mar 7, 2016 at 12:05 PM, Mike Harding <mikeyharding@gmail.com>
> wrote:
>
>> Just wondering as importClass fails if I do the following.
>>
>> importClass("org.apache.nifi.processor.io.StreamCallback");
>> importClass("org.apache.commons.io.IOUtils");
>> importClass("java.nio.charset.StandardCharsets");
>>
>>
>> Apologies for all the questions - I've never tried to include Java
>> classes in a script before.
>>
>>
>>
>> On 7 March 2016 at 17:03, Mike Harding <mikeyharding@gmail.com> wrote:
>>
>>> aaa ok cool. Given that org.apache.nifi.processor.io.StreamCallback is
>>> an interface do I need to include the underlying classes?
>>>
>>> On 7 March 2016 at 16:29, Matt Burgess <mattyb149@gmail.com> wrote:
>>>
>>>> Looks like on Rhino you need a different syntax to import stuff:
>>>> http://docs.oracle.com/javase/7/docs/technotes/guides/scripting/programmer_guide/#jsengine
>>>>
>>>> On Mon, Mar 7, 2016 at 11:26 AM, Matt Burgess <mattyb149@gmail.com>
>>>> wrote:
>>>>
>>>>> So that's weird since you're running NiFi on Java already and trying
>>>>> to use JavaScript. What version of Java are you using? I wonder if the
>>>>> Java.type() thing is new in Java 8's Nashorn.
>>>>>
>>>>> Sent from my iPhone
>>>>>
>>>>> On Mar 7, 2016, at 11:22 AM, Mike Harding <mikeyharding@gmail.com>
>>>>> wrote:
>>>>>
>>>>> Hi Matt,
>>>>>
>>>>> Thanks for doing this - I've just tried to run the template and I get
>>>>> the reference error: "Java" is not defined. I have JAVA_HOME set on my
>>>>> ubuntu machine - just wondering if theres a new config setting I'm missing
>>>>> perhaps?
>>>>>
>>>>> Mike
>>>>>
>>>>> On 2 March 2016 at 21:40, Matt Burgess <mattyb149@gmail.com> wrote:
>>>>>
>>>>>> Ask and ye shall receive ;) I realize most of my examples are in
>>>>>> Groovy so it was a good idea to do some non-trivial stuff in another
>>>>>> language, thanks for the suggestion!
>>>>>>
>>>>>> I recreated the JSON-to-JSON template but with Javascript as the
>>>>>> language:
>>>>>> http://funnifi.blogspot.com/2016/03/executescript-json-to-json-revisited.html
>>>>>>
>>>>>> Regards,
>>>>>> Matt
>>>>>>
>>>>>> On Wed, Mar 2, 2016 at 10:52 AM, Mike Harding <mikeyharding@gmail.com
>>>>>> > wrote:
>>>>>>
>>>>>>> Hi Matt,
>>>>>>>
>>>>>>> Do you know if there is documentation that describes the
>>>>>>> ExecuteScript JavaScript API at the moment ? Just as a practical
example
>>>>>>> how would I translate the Groovy code sample you walk through
in this post
>>>>>>> >
>>>>>>> http://funnifi.blogspot.co.uk/2016/02/executescript-json-to-json-conversion.html
>>>>>>>
>>>>>>> Thanks,
>>>>>>> M
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 1 March 2016 at 18:32, Mike Harding <mikeyharding@gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Hi Matt,
>>>>>>>>
>>>>>>>> That's exactly what I'm looking for - much appreciated !
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Mike
>>>>>>>>
>>>>>>>> On Tue, 1 Mar 2016 at 18:13, Matt Burgess <mattyb149@gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Mike,
>>>>>>>>>
>>>>>>>>> I have a blog containing a few posts on how to use ExecuteScript
>>>>>>>>> and InvokeScriptedProcessor: http://funnifi.blogspot.com
>>>>>>>>>
>>>>>>>>> One contains an example using Javascript to get data
from
>>>>>>>>> Hazelcast and update flowfile attributes:
>>>>>>>>> http://funnifi.blogspot.com/2016/02/executescript-using-modules.html
>>>>>>>>>
>>>>>>>>> If you'd like to share what you'd like to do with ExecuteScript,
>>>>>>>>> I'd be happy to help you get going!
>>>>>>>>>
>>>>>>>>> Regards,
>>>>>>>>> Matt
>>>>>>>>>
>>>>>>>>> On Tue, Mar 1, 2016 at 11:53 AM, Mike Harding <
>>>>>>>>> mikeyharding@gmail.com> wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> I'd like to utilise the ExecuteScript processor but
I understand
>>>>>>>>>> that its experimental. Can anyone point me in the
direction of an example
>>>>>>>>>> or tutorial preferably using Javascript on how to
get started with it?
>>>>>>>>>>
>>>>>>>>>> Thanks,
>>>>>>>>>> Mike
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>

Mime
View raw message