我每天从CSV格式获取数据。
示例数据如下所示:
Emp_ID emp_leave_id EMP_LEAVE_reason Emp_LEAVE_Status Emp_lev_apprv_cnt
E121 E121- 21 Head ache, fever, stomach-ache Approved 16
E139 E139_ 5 Attending a marraige of my cousin Approved 03
在这里,您可以看到emp_leave_id
和EMP_LEAVE_reason
列数据被移动/分散到下一列中。
因此,通过使用tFileInputDelimit和各种读取模式的问题,我无法将数据正确加载到目标数据库中。主要是我无法使用 Talend 中的该组件正确读取数据。
有没有办法可以正确解析此 CSV 以我想要的格式获取数据?
这可能是一个 TSV 文件。不确定 Talend,但 uniVocity 可以为您解析这些文件:
TsvDataStoreConfiguration tsv = new TsvDataStoreConfiguration("my_TSV_datastore");
tsv.setLimitOfRowsLoadedInMemory(10000);
tsv.addEntities("/some/dir/with/your_files", "ISO-8859-1"); //all files in the given directory path will accessible entities.
JdbcDataStoreConfiguration database = new JdbcDataStoreConfiguration("my_Database", myDataSource);
database.setLimitOfRowsLoadedInMemory(10000);
Univocity.registerEngine(new EngineConfiguration("My_ETL_Engine", tsv, database));
DataIntegrationEngine engine = Univocity.getEngine("My_ETL_Engine");
DataStoreMapping dataStoreMapping = engine.map("my_TSV_datastore", "my_Database");
EntityMapping entityMapping = dataStoreMapping.map("your_TSV_filename", "some_database_table");
entityMapping.identity().associate("Emp_ID", "emp_leave_id").toGeneratedId("pk_leave"); //assumes your database does not keep the original ids.
entityMapping.value().copy("EMP_LEAVE_reason", "Emp_LEAVE_Status").to("reason", "status"); //just copies whatever you need
engine.executeCycle(); //executes the mapping.
不要使用 CSV 解析器来解析 TSV 输入。它不会正确处理转义序列(例如值内的\t,您将获得转义序列而不是制表符),并且如果您的值中有引号,它肯定会中断(CSV解析器将尝试查找结束引号字符并将继续读取字符,直到找到另一个引号)
披露:我是这个库的作者。它是开源和免费的(Apache V2.0许可证)。