Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
talendpj
Contributor
Contributor

Can one iterate over all the keys of a context within a job?

If one wanted to list all the defined context keys, can this be accomplished in Java code?

I attempted using context.keySet; however, only one key was listed.

Labels (3)
1 Solution

Accepted Solutions
rhall1
Contributor III
Contributor III

Yes, you can do this. You can even return all of the values in their String form (if you are not sure of the actual type and don't want to write code to identify the type). The code below demonstrates how to do this. Just add this to a tJava component and you will see.

 

java.util.Iterator<String> it = context.stringPropertyNames().iterator();

while(it.hasNext()){

String key = it.next();

System.out.println(key);

System.out.println(context.getProperty(key));

}

View solution in original post

4 Replies
rhall1
Contributor III
Contributor III

Yes, you can do this. You can even return all of the values in their String form (if you are not sure of the actual type and don't want to write code to identify the type). The code below demonstrates how to do this. Just add this to a tJava component and you will see.

 

java.util.Iterator<String> it = context.stringPropertyNames().iterator();

while(it.hasNext()){

String key = it.next();

System.out.println(key);

System.out.println(context.getProperty(key));

}

talendpj
Contributor
Contributor
Author

How, after setting context value of variable, would it be seen using "get" method?"

e.g.

context.myVar is set to null in job.

context.myVar is set to 42 in tPreJob code.

String key does find myVar and context.getProperty("myVar") returns null. However, context.myVar returns 42. 

 

How to print all the runtime values of context variables without manually coding for every context variable name?

rhall1
Contributor III
Contributor III

This sounds like a timing issue. If you context variables start their life as null, then this code will only work after they have been set. I presume that they are being set using your tPreJob. As an example, I just put this job together to prove this....

 

0695b00000fLCo9AAG.pngI set the values using the tFixedFlowInput and the tContextLoad and then ran the code in the tJava after that had run.

Anonymous
Not applicable

Hello,

 

Without knowing why you want to do this it's hard to help. But for hacking contexts one can use java reflection.

 

Here's a routine I created for this:

 

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.HashMap;

 

public class modifyContext {

 

 

  public static void test(Object context) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

   Field[] allFields = context.getClass().getDeclaredFields();

  

   for(Field field : allFields) {

   field.setAccessible(true);

   String fieldName = field.getName();

   if( field.getType().equals(String.class) ) {

   String value = (String)field.get(context);

   System.err.println("Hello I am: " + fieldName + " and my value was " + value + " but lets add HACKED to it!");

   value = value + " Hacked!";

   field.set(context, value);

  

   }

   }

   Method[] methods = context.getClass().getDeclaredMethods();

   for(Method method : methods ) {

   if(method.getName().equals("synchronizeContext")) {

     System.out.println("We have method: " + method.getName() + " lets call it");

   method.invoke(context);

   }

   }

  }

   

  public static void replaceThings(Object context) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

   Field[] allFields = context.getClass().getDeclaredFields();

   HashMap<String,String> fieldValues = new HashMap<>();

   for(Field field : allFields) {

   field.setAccessible(true);

   String fieldName = field.getName();

   if( field.getType().equals(String.class) ) {

   fieldValues.put(fieldName, (String)field.get(context));

   }

   }

   System.err.println(fieldValues.toString());

   for(Field field : allFields) {

   String fieldName = field.getName();

   if( field.getType().equals(String.class) ) {

   if(field.get(context) == null) continue;

   String value = (String)field.get(context);

   if(value.length() > 3 && value.length() != value.replaceAll("\\$[A-Z_]+\\$", "").length()){

   System.err.println("Looking for patterns for " + fieldName);

   for(String name : fieldValues.keySet() ) {

   if(value.length() != value.replace("$"+name+"$", fieldValues.get(name)).length()) {

   System.err.println("Found pattern ("+ name +") in " + fieldName + " replacing with " + fieldValues.get(name));

   value = value.replace("$"+name+"$", fieldValues.get(name));

   }

   }

   field.set(context, value);

   }

   }

   }

   Method[] methods = context.getClass().getDeclaredMethods();

   for(Method method : methods ) {

   if(method.getName().equals("synchronizeContext")) {

     System.out.println("We have method: " + method.getName() + " lets call it");

   method.invoke(context);

   }

   }

   //return context;

  }

}