gpt4 book ai didi

sql - 如何从查询中获取列名?

转载 作者:行者123 更新时间:2023-12-04 18:20:33 29 4
gpt4 key购买 nike

我有一个包含连接和别名的特定查询,我需要在 Talend 中检索 REST 请求的列名。

我正在使用 Talend Open Studio for Data Integration 6.2,我有一个 Oracle 11g 数据库和一个只读帐户。我可以使用 Talend 执行脚本,例如查询:

select 
u.name as "user",
f.name as "food",
e.rate
from
Users as u
join Eval as e on u.user_id = e.user_id
join Food as f on e.food_id = f.food_id
where
1 = 1

应该给出以下结果:

+------+--------+------+
| user | food | rate |
+------+--------+------+
| Baba | Donuts | 16.0 |
| Baba | Cheese | 20.0 |
| Keke | Pasta | 12.5 |
| Keke | Cheese | 15.0 |
+------+--------+------+

我尝试使用脚本或 Talend 获取列(以正确的顺序)如下:

+--------+
| Column |
+--------+
| user |
| food |
| rate |
+--------+

有没有办法查询 Oracle 数据库以获取列或使用 talend 检索它们?

更新

感谢 Marmite Bomber,已为 Oracle 方法确定了重复的 here。现在我们需要 Talend 方法来解决这个问题。

最佳答案

您可以在 tJavaRow 上尝试这个,在您的 DBInput 组件之后:

for (java.lang.reflect.Field field: row1.getClass().getDeclaredFields()) {
context.columnName = field.getName();
System.out.println("Field name is " + context.columnName );
}

在这里发现了 talend 帮助中心:https://community.talend.com/t5/Design-and-Development/resolved-how-to-get-the-column-names-in-a-data-flow/td-p/99172

您可以扩展它,并将列列表放在您的输出流中:

//add this inside the loop, and 'columnNames' as an output row in tJavaRow schema

output_row.columnNames+=context.columnName+";";

在 tJavaRow 之后使用 tNormalize,您应该会得到预期的结果。

关于sql - 如何从查询中获取列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949238/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com