简体   繁体   English

如何将查询的实际TEXT从Pentaho Spoon导出到Excel文件?

[英]How do I export the actual TEXT of a query from Pentaho Spoon into an Excel file?

I use Pentaho Spoon for Data Integration (aka, PDI). 我使用Pentaho Spoon进行数据集成(又名PDI)。 My internal client wants reports written to excel files, which is fine, but he also wants to see the queries I use in separate tabs in the workbook. 我的内部客户希望将报告写入excel文件,这很好,但是他还希望查看我在工作簿的单独选项卡中使用的查询。 Is there a way to use Spoon to export the actual text of the query itself - not the results - to a .xlsx? 有没有一种方法可以使用Spoon将查询本身的实际文本 (而不是结果)导出到.xlsx?

In other words, if one query is 换句话说,如果一个查询是

SELECT Muppets, Peanuts, Sprites FROM eighties_shows.childrens_cartoons 从80ies_shows.childrens_cartoons中选择木偶,花生,雪碧

Then I want to have an excel file that shows 然后我想有一个显示

SELECT Muppets, Peanuts, Sprites FROM eighties_shows.childrens_cartoons 从80ies_shows.childrens_cartoons中选择木偶,花生,雪碧

You can use PDI Table Input to get the data from a variable. 您可以使用PDI表输入从变量获取数据。 Try to define a variable which will hold all the column names (and another variable containing the table name) and try to pass this variable to the Table Input Step. 尝试定义一个将保留所有列名的变量(以及另一个包含表名的变量),然后尝试将此变量传递给“表输入步骤”。 For eg: In the Table Input Step define: 例如:在表格输入步骤中定义:

Select ${COLUMN VARIABLE} FROM ${TABLE NAME VARIABLE}

This will execute your query. 这将执行您的查询。 Now for Generating the Query in your excel, use a "Modified Java Script" step to recreate the same structure. 现在,要在Excel中生成查询,请使用“修改的Java脚本”步骤来重新创建相同的结构。 The JS Code snip is given below: JS代码片段如下所示:

LOOP i=1 till getInputRowMeta().size()

   var fields =getInputRowMeta().getValueMeta(i) /* This will give you the list of Input Columns along with the datatype e.g. Muppets String(100) */

END LOOP

Remove the datatype from your fields variable and create a final variable having the concatenation of 从您的字段变量中删除数据类型,并创建一个最终变量,其串联为

var final_query="SELECT" + cleaned fields variable from the above JS + "FROM" + table name;

Hence you generate a query which you can easily output in your excel output. 因此,您生成一个查询,可以轻松地在excel输出中输出该查询。

This is slightly lengthy process. 这是一个漫长的过程。 But i am not able to get any other solution other than generating query using the method above. 但是,除了使用上述方法生成查询之外,我无法获得任何其他解决方案。 Hope this solution is good for you !! 希望这个解决方案对您有好处! :) :)

Most modern databases support dynamic sql. 大多数现代数据库都支持动态sql。 If you are using SQL Server look into sp_executesql. 如果使用的是SQL Server,请查看sp_executesql。 All this means is that your SQL code is stored as a string and executed that way. 所有这些意味着您的SQL代码将存储为字符串并以这种方式执行。

Now to using dynamic sql in pentaho.... 现在要在pentaho中使用动态SQL。

Set up a job that creates the excel file for output and that also gets the sql either passed in on the job call or from the filesystem. 设置一个作业,该作业创建excel文件以进行输出,并且还获取通过作业调用或从文件系统传递的sql。 Now pass the sql to a transformation within the job that will be responsible for filling the excel file. 现在将sql传递到作业中的转换中,该转换将负责填充excel文件。 Write the sql to whatever sheet you wish. 将sql写入所需的任何工作表。

Hope this helps 希望这可以帮助

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

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