oracle Talend:已定义字符串/参数的查询数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20371792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Talend: Query Database with Strings/Parameters already defined
提问by user2144555
How can I perform a Query to my Database (using tOracleInput), like a Select, and use Strings that are already defined as parameters in other components, for example in a 'tFlowToIterate' ?
如何对我的数据库执行查询(使用tOracleInput),如Select,并使用已在其他组件中定义为参数的字符串,例如在 'tFlowToIterate' 中?
For example: "SELECT * from TABLE_X where FIELD_X= ?;"
例如:“ SELECT * from TABLE_X where FIELD_X= ?;”
My '?'is the variable that comes from my tFlowToIterate component (foo). I already tried with (String)globalMap.get("foo"), and other similar forms...
我的'?是来自我的 tFlowToIterate 组件 (foo) 的变量。我已经尝试过(String)globalMap.get("foo")和其他类似的形式......
Thanks
谢谢
[Talend Open Studio for Data Integration v5.3.1; DB: Oracle]
[Talend Open Studio for Data Integration v5.3.1;数据库:甲骨文]
回答by Gabriele B
You answered by yourself. tOracleInput
component accepts the query as parameter. This is a very boring java String, no more, no less. This means that if you want to use a globalMap element inside a query, you just need to do a java String concatenation. Something like that:
你自己回答的。tOracleInput
组件接受查询作为参数。这是一个很无聊的java String,不多也不少。这意味着如果你想在查询中使用 globalMap 元素,你只需要做一个 java String 连接。类似的东西:
"SELECT * from TABLE_X where FIELD_X='" + (String)globalMap.get("foo") + "'"
but this won'twork (look carefully at the quotes):
但是这不会工作(仔细看引号):
"SELECT * from TABLE_X where FIELD_X='(String)globalMap.get("foo")'"
Keep in mind that if you write a query using string concatenation and external vars, the query editor will probably going to mess all the quotes, generating a broken query.
请记住,如果您使用字符串连接和外部变量编写查询,则查询编辑器可能会弄乱所有引号,从而生成损坏的查询。
As a general advice, I never suggest to use the "*" operator inside a database input component like tOracleInput
. Talend has a fixed-scheme structure that is generated at compile time. This means that if one day you'll add a column to TABLE_X, your ETL will going to fail.
作为一般建议,我从不建议在数据库输入组件(如tOracleInput
. Talend 具有在编译时生成的固定方案结构。这意味着,如果有一天您将一列添加到 TABLE_X,您的 ETL 将失败。
A more robust solution is the following:
更强大的解决方案如下:
- Write down your query with the * operator
- Click "Guess Schema" to retrieve the table schema and put in your component metadata
- Now click "Guess Query" to explicitely rewrite your SELECT
- Fix the query (ie. WHERE conditions,...) if needed
- 用 * 运算符写下您的查询
- 单击“Guess Schema”以检索表模式并放入您的组件元数据
- 现在单击“Guess Query”以明确重写您的 SELECT
- 如果需要,修复查询(即 WHERE 条件,...)
回答by ydaetskcoR
You just need to concatenate it with your variable.
您只需要将它与您的变量连接起来。
So in your case it would look like:
所以在你的情况下,它看起来像:
"SELECT *
FROM TABLE_X
WHERE FIELD_X = '" + (String)globalMap.get("foo") + "'"