简体   繁体   English

Pentaho在同一个jar中启动一个Java程序

[英]Pentaho start a java program inside the same jar

We have an organization reports portal for which we use Pentaho. 我们有一个使用Pentaho的组织报告门户。 Now the catch is, we need to define one single java Jar in which Pentaho + java files + sql, all are there. 现在要抓住的是,我们需要定义一个单个的Java Jar,其中包含Pentaho + Java文件+ sql。

I'm able to execute SQL via Pentaho but I need to execute a class in the same jar via pentaho. 我可以通过Pentaho执行SQL,但是我需要通过pentaho在同一jar中执行一个类。 This can be done via user Defined Java Class. 这可以通过用户定义的Java类来完成。 But how to do it? 但是怎么做呢? How to directly import java class in the KTR --> UserDefinedJavaClass. 如何在KTR-> UserDefinedJavaClass中直接导入Java类。

Folder Structure in jar: jar中的文件夹结构:

MainJar MainJar
|-testJob.kjb | -testJob.kjb
|-testTransformer.ktr | -testTransformer.ktr
|-com.mainTest.HelloWorld.class | -com.mainTest.HelloWorld.class

How to call HelloWorld.class from test.ktr? 如何从test.ktr调用HelloWorld.class?

I haven't been able to get it working with User Defined Java Class step but using the Javascript step instead. 我无法使其与“用户定义的Java类”步骤一起使用,而是改为使用Javascript步骤。 You need to copy the JAR to pentaho /lib or /libext folder first. 您需要先将JAR复制到pentaho / lib或/ libext文件夹。 Then in the modified javascript step I just called the class. 然后,在经过修改的javascript步骤中,我刚刚调用了该类。 Here is my step XML 这是我的步骤XML

<entry>
  <name>JavaScript</name>
  <description />
  <type>EVAL</type>
  <script>
   var filename=parent_job.getVariable("filename","");
   var filenameClean=filename+"_clean";
   package.CsvCleaner(filename,filenameClean);
  </script>
  <parallel>N</parallel>
  <draw>Y</draw>
  <nr>0</nr>
  <xloc>800</xloc>
  <yloc>1008</yloc>
</entry>

The important part is package.CsvCleaner(filename,filenameClean); 重要的部分是package.CsvCleaner(filename,filenameClean); actually invokes the constructor of the class with the two parameters and I have added my code in the constructor. 实际上使用两个参数调用类的构造函数,并且我在构造函数中添加了代码。 You can also call methods and stuff. 您还可以调用方法和内容。

I guess the User Defined Java is called in a similar way -add the jar to /lib import the class and use it. 我猜以类似的方式调用用户定义的Java-将jar添加到/ lib导入类并使用它。

I have one advise though. 我有一个建议。 In my opinion it is better to leave the JAR with the actual ETL code (and not making it part of pentaho by adding it to /lib or /libext) and execute it using a shell command - something like java -jar MyProg.jar Otherwise you will have strange problems: 在我看来,最好让JAR保留实际的ETL代码(并且不要通过将其添加到/ lib或/ libext使其成为pentaho的一部分),并使用shell命令执行它-类似于java -jar MyProg.jar否则您将遇到奇怪的问题:

  • having an older version of the Jar in the pentaho installation folder 在pentaho安装文件夹中有一个较旧版本的Jar
  • harder installation because that jar is not where the kjb and kjt files are 较难安装,因为该jar不在kjb和kjt文件所在的位置
  • if pentaho is installed with a different user than the one deploying the ETL he might not have rights to access /lib and /libext folders 如果pentaho的安装用户不同于部署ETL的用户,则他可能无权访问/ lib和/ libext文件夹
  • If you have more complicated java code and logic it is better to keep it in the jar instead of in a transformation (having java code in two places is a maintenance hell). 如果您有更复杂的Java代码和逻辑,最好将其保存在jar中,而不是放在转换中(将Java代码放在两个地方是一个维护地狱)。 So adding the jar to pentaho gives you no benefit over just calling java -jar MyJar.jar 因此,将jar添加到pentaho不会给您带来任何好处,仅需调用java -jar MyJar.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM