Created 03-30-2018 08:46 AM
Hi,
I am trying to make a Javascript code for the executeScript processor and I wonder if it's possible to get the name of a custom property added from the executeSript Properties tab.
I know that if I add a property "myProperty", I can access it from the script by using myProperty.getValue() (or with .evaluateAttributeExpressions() when needed).
But is it possible to find the key "myProperty" from inside the script ?
I would like to do something like
for(i = 0 ; i < customProperties.length ; i++){ propertyName = customProperties[i].getName() value = propertyName.getValue() //Do something with propertyName and its value }
Thanks in advance !
Félicien
Created 04-02-2018 01:49 PM
You can access all the properties from the "context" variable, then filter on only the dynamic properties. You get a Map of PropertyDescriptors as keys to their values, so you can iterate over the keys to look for your property key(s). Here is a quick Groovy snippet that will log each dynamic property name:
context.properties.findAll {k,v -> k.dynamic}.each {k,v -> log.info(k.name)}
Created 03-31-2018 05:31 AM
If I understand your question properly, you want to check all the attributes of a flow file and then take some action on that attribute. For this, you can use getAttributes() function in your script. This will return you a map with attribute name as key and attribute value as value. For example
flowFile = session.get() attrMap = flowFile.getAttributes()
You can iterate on the map to check if a certain property exists or not or whatever actions you may want to take.
Hope that helps!
Created 04-01-2018 03:27 PM
Did the answer help in the resolution of your query? Please close the thread by marking the answer as Accepted!
Created 04-03-2018 09:37 AM
Not really.. but Matt's answer is what I needed, thank for your answer anyway !
Created 04-02-2018 01:49 PM
You can access all the properties from the "context" variable, then filter on only the dynamic properties. You get a Map of PropertyDescriptors as keys to their values, so you can iterate over the keys to look for your property key(s). Here is a quick Groovy snippet that will log each dynamic property name:
context.properties.findAll {k,v -> k.dynamic}.each {k,v -> log.info(k.name)}
Created 04-03-2018 09:35 AM
Thanks Matt ! This is exactly what I needed
Created 04-03-2018 01:03 PM
This is working perfectly with this groovy script. However, I wanted to do that in Javascript (since I want to manipulate JSON objects), is there an easy way to filter the dynamics properties ?
For now, I can retrieve the list of properties (in context.properties), but I don't see any difference between the "hard coded properties" ("script engine","script file", "modules directory") and the properties I added myself.
Thanks in advance if you can help
Created 04-05-2018 08:30 AM
Created 04-05-2018 01:16 PM
I used Groovy's findAll method for filtering/iterating in my example, you'd just have to use the Javascript idiom, maybe something like:
for(var pdMap in context.getProperties()) {
if(pd.getKey().isDynamic()) {
// This is a user-defined "dynamic" property, the ExecuteScript properties won't show up here
}
}
Having said that, if you are only using Javascript in order to parse JSON, you can keep Groovy and use JsonSlurper and JsonOutput and other handy classes for manipulating JSON objects.
Created 04-05-2018 01:21 PM
Thanks a lot ! , i'll have a look to JsonSlurper and JsonOutput