Hitachi Vantara Pentaho Community Forums
Results 1 to 2 of 2

Thread: Passing a parameter from java to Kettle

  1. #1
    Join Date
    Feb 2014
    Posts
    16

    Question SOLVED: Setting a variable from java to Kettle

    Hi!!
    I have been reading a few posts about this issue but I have not been able to suceed. I want to set a variable value in a job executing it from java (I have tried with setVariable and setParameterValue) in order to use it in further transformations. I have the following code for that:

    Code:
    try {            KettleEnvironment.init();
                            RepositoriesMeta repositoriesMeta = new RepositoriesMeta();            
                            repositoriesMeta.readData();
                
                RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repositoryName);
                
                if (repositoryMeta == null){
                    throw new KettleException("Cannot find repository \""+repositoryName+"\". Please make sure it is defined in your "+Const.getKettleUserRepositoriesFile()+" file"); 
                }
                
                PluginRegistry registry = PluginRegistry.getInstance();
                Repository repository = registry.loadClass(
                       RepositoryPluginType.class,
                       repositoryMeta,
                       Repository.class
                  );
                
                repository.init(repositoryMeta);
                repository.connect("admin", "admin");
                
                RepositoryDirectoryInterface tree = repository.loadRepositoryDirectoryTree();
                RepositoryDirectoryInterface dir = tree.findDirectory(directory);
                
                if (dir == null){
                    throw new KettleException("Cannot find directory \""+directory+"\" in repository.");
                }
    
                JobMeta jobMeta = repository.loadJob(jobName, dir, null, null);
        1. ->    //    jobMeta.setParameterValue("initialDate", "xxxxxx");
        2. ->    //    jobMeta.setVariable("initialDate", "xxxxxx");
                
                Job job = new Job(repository, jobMeta);
                job.setLogLevel(LogLevel.BASIC);
                job.start();
                job.waitUntilFinished();
                
                Result result = job.getResult();
    
    
                String outcome = "\nJob "+ directory+"/"+jobName+" executed with result: "+result.getResult()+" and " +result.getNrErrors()+" errors";
                System.out.println(outcome);
                
                return job;
    
    
            } 
    
                    catch (KettleException e) {
                e.printStackTrace();
                return;
            }
    Then, in Kettle I have an example job and transformation, the ones I have attached in the post.
    ababa.ktr ReadDate.kjb

    Anyone can give me a hint about this??

    Thanks in advance
    Alain
    Last edited by alainflecha; 06-04-2014 at 05:36 AM.

  2. #2
    Join Date
    Feb 2014
    Posts
    16

    Default

    SOLVED: After trying many different approaches finally the solution in my case consisted on adding "job.shareVariablesWith(jobMeta);" before setting the variables with: jobMeta.setVariable("variableName", "value");

    Just as a hint for people doing the same-> in kettle it is not necessary to add a "Set Variable" step in the job using these variables. Just the "Get Variables" in the moment you need them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.