如何在Apache NiFi中重新排序CSV列 [英] How to reorder CSV columns in Apache NiFi
问题描述
在Apache Nifi中的csv中对列进行重新排序.
Reorder column in a csv in apache nifi.
输入-我有多个文件,它们具有相同的列,但顺序不同.
Input - I have multiple files which have same columns but are in different order.
输出-刮擦一些列并以相同顺序存储.
Output - Scrape some columns and store in same order.
推荐答案
在我的情况下,因为我确定这些列将包含在所有CSV文件中,所以我只需要对其重新排序.所以我用QueryRecord
重新排序我的csv文件.
In my case, because I'm sure those columns will be included in all CSV files, I just need to reorder them. So I use QueryRecord
to reorder my csv files.
例如,这是我的csv文件:
For example, here're my csv files:
\\file1
name, age, location, gender
Jack, 40, TW, M
Lisa, 30, CA, F
\\file2
name, location, gender, age
Mary, JP, F, 25
Kate, DE, F, 23
我想将列重新排序为location,name,gender,age
,我在QueryRecord
中设置了名为reorder_data
的新属性,其值类似于:
I'd like to reorder columns to location,name,gender,age
, I set a new property in QueryRecord
named reorder_data
, with the value like:
SELECT location,name,gender,age FROM FLOWFILE
然后,流文件中的数据将变为:
Then data in the flowfile will become:
\\file1 - reordered
location, name, gender, age
TW, Jack, M, 40
CA, Lisa, F, 30
\\file2 - reordered
location, name, gender, age
JP, Mary, F, 25
DE, Kate, F, 23
因此,我可以从QueryRecord
输出重新排序的数据以及原始数据,这非常方便.
Thus, I can get reordered data output from QueryRecord
as well as original data, it's very convenient.
顺便说一句,您也可以使用组变量或属性来设置列顺序以更好地维护:
BTW, You can also use group variable or attribute to set column order for better maintenance:
//Group variable or attribute
column_order location,name,gender,age
//Property in QueryRecord
reorder_data SELECT ${column_order} FROM FLOWFILE
这篇关于如何在Apache NiFi中重新排序CSV列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!