这是交易:
我被要求开发一个JAVA程序,它可以对.tsv文件进行一些重组(移动单元格进行某种转换)。
所以,我试着干净利落地完成了3个不同的套餐:
只需tsvExceptions
和tsvTranspositer
即可使主(TSVTransposer.java
)正常工作。
昨天我了解到我必须在Talend中实现它,这是我从未听说过的。
所以通过搜索,我踩到了stackOverflow topic。所以我按照步骤,创建一个例程,复制/粘贴我的主要内容(将包更改为“例程”)并添加外部所需的库(我的两个包导出为jar文件和openCSV)。现在,当我打开例程时,没有显示任何错误,但我无法拖动&把它放到我创造的工作中!
什么都没发生。它只是打开组件信息,如“属性不可用”所示。
package routines;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import tsvExceptions.ArgsExceptions;
import tsvExceptions.EmptyArgsException;
import tsvExceptions.OutOfBordersArgsException;
import tsvTranspositer.CommonLine;
import tsvTranspositer.HeadOfValuesHandler;
import tsvTranspositer.InputFile;
import tsvTranspositer.OutputFile;
public class tsvRoutine {
public static void main(String[] args) throws ArgsExceptions {
// Boolean set to true while everything is good
Boolean everythingOk = true;
String inputFile = null; // Name of the entry file to be transposed.
String outputFile = null; // Name of the output file.
int serieNb = 1 ; // Number of columns before the actual values in the input file. Can be columns describing the product as well as empty columns before the values.
int linesToCopy = 0; // Number of lines composing the header of the file (those lines will be copy/pasted in the output)
/*
* Handling the arguments first.
*/
try {
switch (args.length) {
case 0:
throw new EmptyArgsException();
case 1:
inputFile = args[0];
String[] parts = inputFile.split("\\.");
// If no outPutFile name is given, will add "Transposed" to the inputFile Name
outputFile = parts[0] + "Transposed." + parts[1];
break;
case 2:
inputFile = args[0];
outputFile = args[1];
break;
case 3:
inputFile = args[0];
outputFile = args[1];
serieNb = Integer.parseInt(args[2]);
break;
case 4:
inputFile = args[0];
outputFile = args[1];
serieNb = Integer.parseInt(args[2]);
linesToCopy = Integer.parseInt(args[3]);
break;
default:
inputFile = args[0];
outputFile = args[1];
serieNb = Integer.parseInt(args[2]);
linesToCopy = Integer.parseInt(args[3]);
throw new OutOfBordersArgsException();
}
}
catch (ArgsExceptions a) {
a.notOk(everythingOk);
}
catch (NumberFormatException n) {
System.out.println("Arguments 3 & 4 should be numbers."
+ " Number 3 is the Number of columns before the actual values in the input file. \n"
+ "(Can be columns describing the product as well as empty columns before the values. (1 by default)) \n"
+ "Number 4 is the number of lines to copy/pasta. (0 by default) \n"
+ "Please try again.");
everythingOk = false;
}
// Creating an InputFile and an OutputFile
InputFile ex1 = new InputFile(inputFile, linesToCopy);
OutputFile ex2 = new OutputFile(outputFile);
if (everythingOk) {
try ( FileReader fr = new FileReader(inputFile);
CSVReader reader = new CSVReader(fr, '\t', '\'', 0);
FileWriter fw = new FileWriter(outputFile);
CSVWriter writer = new CSVWriter(fw, '\t', CSVWriter.NO_QUOTE_CHARACTER))
{
ex1.setReader(reader);
ex2.setWriter(writer);
// Reading the header of the file
ex1.readHead();
// Writing the header of the file (copy/pasta)
ex2.write(ex1.getHeadFile());
// Handling the line containing the columns names
HeadOfValuesHandler handler = new HeadOfValuesHandler(ex1.readLine(), serieNb);
ex2.writeLine(handler.createOutputHOV());
// Each lien will be read and written (in multiple lines) one after the other.
String[] row;
CommonLine cl1;
// If the period is monthly
if (handler.isMonthly()) {
while (!ex1.isAllDone()) {
row = ex1.readLine();
if (!ex1.isAllDone()) {
cl1 = new CommonLine(row, handler.getYears(), handler.getMonths(), serieNb);
ex2.write(cl1.exportOutputLines());
}
}
}
// If the period is yearly
else {
while (!ex1.isAllDone()) {
row = ex1.readLine();
if (!ex1.isAllDone()) {
cl1 = new CommonLine(row, handler.getYears(), serieNb);
ex2.write(cl1.exportOutputLines());
}
}
}
}
catch (FileNotFoundException f) {
System.out.println(inputFile + " can't be found. Cancelling...");
}
catch (IOException e) {
System.out.println("Unknown exception raised.");
e.printStackTrace();
}
}
}
}
我知道异常还没有得到正确处理,但是它们有点急于以某种方式工作。
稍后会出现的另一个问题是我不知道如何解析程序所需的参数。
无论如何,感谢阅读这篇文章!
答案 0 :(得分:1)
每次拖放操作都无法添加例程。您需要通过组件访问例程函数。
例如,您可以从 tFileListInput 开始,以获取所需的所有文件。然后,您可以添加 tFileInputDelimited ,您可以在其中描述输入的所有字段。在此之后,例如一个 tJavaRow 组件,您可以编写一些可以访问例程的代码。
注意:请记住,Talend通常按行工作。这意味着您的例程应该以行方式处理内容。这也可能意味着您的代码必须相应地重构。 main
函数不起作用,至少可以成为可以实例化或具有static
函数的类。
如果您想自己处理所有内容,而不是 tJavaRow 组件,则可以使用 tJava 组件,这样可以增加灵活性。
尽管如此,它只是简单地添加例程并且一切都会有效。
事实上,整个代码本身就可以成为一项工作。 Talend为您生成完整的Java代码:
Context variables
。所有异常处理均由Talend完成。如果您想对异常做出反应,可以使用 tLogRow 等组件。
希望这有助于设定方向。