Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

check for the existence of a context parameter like the global map? tJava

Is it possible to check the existence of a context parameter, like I'd check a key in the global map?

I'm trying to avoid having to create context parameters for every variable I might possibly check for. 

 

Labels (1)
9 Replies
nfz11
Creator III
Creator III

Yes, you can do this like:

 

if ( context.containsKey("myParameter")  ) {
  // myParameter is a context variable
} else {
  // myParameter is not a context variable
}

You can reference the context object in the fields of most components or in a tJava or tJavaRow if you need custom logic.

Anonymous
Not applicable
Author

How would I access the value without getting an unresolved compile error?

nfz11
Creator III
Creator III


@cyberpunkspike wrote:

How would I access the value without getting an unresolved compile error?


Like I showed you above:

if ( context.containsKey("myParameter")  ) {
  System.out.println((String)context.myParameter);
} else {
  // myParameter is not a context variable
}
Anonymous
Not applicable
Author

context.myparameter would be unresolved.

You're checking for the existence of a contextkey at runtime, but retrieving from the corresponding field a value at compilation time.

 

 

Anonymous
Not applicable
Author

This is a slightly different way of doing this. FYI you can look at the generated code to work most of this stuff out if you have enough Java experience.

 

PropertiesWithType pwt = (PropertiesWithType)context;

if ( pwt.getProperty("myParameter")!=null  ) {
 	
 	if(pwt.getContextType("myParameter").compareToIgnoreCase("ID_STRING")==0){
 		System.out.println(pwt.getProperty("myParameter"));
 	}else if(pwt.getContextType("myParameter").compareToIgnoreCase("ID_INTEGER")==0){
 		System.out.println(pwt.getProperty("myParameter"));
 	}
} else {
  // myParameter is not a context variable
}

There is a class constructed inside the Talend job called PropertiesWithType. This has all of the information you will need, even the type information so that you know how to deal with it. The code I have written above is just a basic example, you should be able to reconstruct this to do what you want.

nfz11
Creator III
Creator III


@cyberpunkspike wrote:

context.myparameter would be unresolved.

You're checking for the existence of a contextkey at runtime, but retrieving from the corresponding field a value at compilation time.

 

 


Yes, that is the way to do it.  If the context parameter does not exist at run time, my code does not try to access it so there will be no error.

 

If you post more specifics on your use case maybe we could help you more.

Anonymous
Not applicable
Author

@nfz11 your code would lead to compilation errors I'm afraid. If you try to use a context variable (in the normal way) that does not exist in a job, it will not compile.

 

Try this code in a job without the context variable "bob".....

 

if(1==0){
    System.out.println(context.bob);
}

 The code inside the if condition will never run as 1 will never equal 0. However the code inside is still compiled.

nfz11
Creator III
Creator III


@rhall wrote:

@nfz11 your code would lead to compilation errors I'm afraid. If you try to use a context variable (in the normal way) that does not exist in a job, it will not compile.

 

Try this code in a job without the context variable "bob".....

 

if(1==0){
    System.out.println(context.bob);
}

 The code inside the if condition will never run as 1 will never equal 0. However the code inside is still compiled.


Oh OK thanks, I did not know that.  I thought it worked as it does in Java.

Anonymous
Not applicable
Author

It does work as it does in Java. You can try the same code (but replacing "context.bob" with another variable that has not been declared in your code) in a small Java class.