简体   繁体   English

如何在Talend Open Studio作业中将SQL查询作为上下文变量传递

[英]How to pass SQL query as a context variable in Talend Open Studio job

I am creating a job to pull data from a database to CSV file using talend open studio. 我正在创建一个使用talend open studio将数据从数据库拉到CSV文件的作业。 There are 100 of tables, the data types and no of columns differ in the tables, I want to pull the data from database tables with a single job and customizable SQL query. 有100个表,表中的数据类型和列数均不同,我想通过一个作业和可自定义的SQL查询从数据库表中提取数据。 I know how to create and use context variables. 我知道如何创建和使用上下文变量。

If I understood you correctly you should be using tMap's reload at each row -option and defining table names in Excel sheet or in tFixedFlowInput. 如果我理解正确,则应该在每行-option处使用tMap的重新加载,并在Excel工作表或tFixedFlowInput中定义表名称。

tMap settings tMap设置

Whole job and results 整个工作和结果

SQL Script: SQL脚本:

"SELECT TOP(1) Name,  Code from mdm." + (String)globalMap.get("row4.table")

I used Microsoft SQL Server for example but same script works as well with MySQL. 我以Microsoft SQL Server为例,但是相同的脚本也可以与MySQL一起使用。

You can simply use a context-variable which you set via the --context_param argument in a tWhicheverDatabaseInput. 您可以简单地使用上下文变量,该变量是通过tWhicheverDatabaseInput中的--context_param参数设置的。 Eg define a context variable "my_sql" which you can set in the commandline as 例如,定义上下文变量“ my_sql”,您可以在命令行中将其设置为

my_job.sh --context_param my_sql="select a,b,c from a_test_table"

and then use context.my_sql as the SQL in you database input component. 然后使用context.my_sql作为数据库输入组件中的SQL。

However, as garpitmzn already mentioned, you will need dynamic schemas to actually work with this unknown structure in Talend. 但是,正如garpitmzn所述,您将需要动态模式才能在Talend中实际使用此未知结构。 This feature only exists in the enterprise version. 此功能仅在企业版中存在。

If the enterprise version is available to you, simply declare a single column of type "Dynamic", which you can pass through the rest of your flow. 如果您可以使用企业版,则只需声明一个类型为“ Dynamic”的列,即可在其余流程中进行传递。

  1. Declare a local context say query as type string. 将本地上下文声明为字符串类型的查询。

  2. Prepare a context file with variable query: query=select name from employee 使用变量查询准备上下文文件: query=select name from employee

  3. Excecute the query: toraclecomponent use context.query 执行查询: toraclecomponent use context.query

Query is throwing some error when you have where conditions with string type. 当您具有字符串类型的where条件时,查询将引发一些错误。 Need to investigate more on that. 需要对此进行更多调查。 Otherwise it works. 否则它将起作用。

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

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