Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Get dynamic property name in execute script

Solved Go to solution

Get dynamic property name in execute script

New Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get dynamic property name in execute script

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)}
10 REPLIES 10

Re: Get dynamic property name in execute script

@Félicien Catherin

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!

Re: Get dynamic property name in execute script

@Félicien Catherin

Did the answer help in the resolution of your query? Please close the thread by marking the answer as Accepted!

Re: Get dynamic property name in execute script

New Contributor

Not really.. but Matt's answer is what I needed, thank for your answer anyway !

Re: Get dynamic property name in execute script

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)}

Re: Get dynamic property name in execute script

New Contributor

Thanks Matt ! This is exactly what I needed

Re: Get dynamic property name in execute script

New Contributor

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

Re: Get dynamic property name in execute script

New Contributor

Re: Get dynamic property name in execute script

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.

Re: Get dynamic property name in execute script

New Contributor

Thanks a lot ! , i'll have a look to JsonSlurper and JsonOutput