如何从查询中获取列名?

How to get column names from a query?

我有一个包含连接和别名的特定查询,我需要在 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 方法来解决这个问题。

这里是一个 link 应该回答您的问题的 oracle 社区线程

community.oracle.com

我无法发表评论,所以张贴这个作为答案:

SELECT column_name
  FROM all_tab_cols
 WHERE table_name = 'table_name_here'

您可以在 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,您应该会得到预期的结果。