Support Questions

Find answers, ask questions, and share your expertise
Announcements
Check out our newest addition to the community, the Cloudera Data Analytics (CDA) group hub.

Get dynamic property name in execute script

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

Super Guru

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

View solution in original post

10 REPLIES 10

@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!

@Félicien Catherin

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

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

Super Guru

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

Thanks Matt ! This is exactly what I needed

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

Super Guru

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.

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

If anyone needs it (Matt answers helped me a lot but is not exactly correct) :

for(var prop in context.properties) {
if(prop.isDynamic()) { var name = prop.getName(); var value = context.getProperty(name).evaluateAttributeExpressions(flowFile).getValue();
//or var value = context.getProperty(name).getValue(); if AttributeExpression are not needed log.info('name : ' + name + " value : " + value); }
}
Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.