在Talend中实现带有参数的Java程序
我被要求编写一个Java程序,将TSV文件作为输入,并生成一个不同的TSV文件(其中包含一些在输入和参数上非常可变的更改)作为输出 这是一个相当大的程序(花了我3天的时间来编写代码,但我并不擅长),它最终将处理15k行的输入,生成1500K行的输出 在编写代码时,我不知道以后必须在Talend中实现它,因此它是一个普通的Java程序,需要4个参数:输入文件名、输出文件名、int、int 我设法将我的Main作为一个例程,添加了所需的额外包(personal和openCSV) 我的问题是:是否可以在Talend中实现它而不更改它?我能否告诉Talend,这是输入中的文件,这些是参数?在昨天之前我从未听说过泰伦德 如果你有兴趣,这里是主要的问题,但我相信我的问题是相当一般的在Talend中实现带有参数的Java程序,java,talend,Java,Talend,我被要求编写一个Java程序,将TSV文件作为输入,并生成一个不同的TSV文件(其中包含一些在输入和参数上非常可变的更改)作为输出 这是一个相当大的程序(花了我3天的时间来编写代码,但我并不擅长),它最终将处理15k行的输入,生成1500K行的输出 在编写代码时,我不知道以后必须在Talend中实现它,因此它是一个普通的Java程序,需要4个参数:输入文件名、输出文件名、int、int 我设法将我的Main作为一个例程,添加了所需的额外包(personal和openCSV) 我的问题是:是否可以
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();
}
}
}
谢谢你读到这里 否,在不更改代码的情况下,无法使用Talend中的代码 但是,如果定义良好,可以重新创建工作流 编辑
正如54l3d所指出的,可以添加外部库并调用它们,如果只需要做少量的额外工作,这似乎是一个很好的解决方案。如果环境需要,则最好使用“本机”Talend进行此操作,例如,如果有大量作业需要维护。否,在不更改代码的情况下,无法使用Talend中的代码 但是,如果定义良好,可以重新创建工作流 编辑
正如54l3d所指出的,可以添加外部库并调用它们,如果只需要做少量的额外工作,这似乎是一个很好的解决方案。如果环境需要使用“native”Talend,例如,如果有很多作业需要维护,则最好使用它。如果需要保持原样,可以将代码打包为任何可运行的格式,然后通过Talend
tSystem
组件调用它,如果是Jar文件,则通过TalendtLibraryLoad
组件调用它。如果需要保持原样,则可以将代码打包为任何可运行的格式,然后通过TalendtSystem
组件调用它,如果是Jar文件,则通过tLibraryLoad
组件调用它。啊,我没想到。将符合要求,但在我看来也很难维持。此外,使用这种方法,Talend一开始就不需要。在某些情况下,使用现有代码可以缩短上市时间,而Talend只用于监视和记录目的!有趣的方法。因此,在本例中,您将使用存储在某处的其他日志数据,可能是通过管理控制台,并仅使用库中的代码,例如?@Fitz如果您的意思是每次使用不同的参数调用代码,是的,这是可能的,您将需要准备作为全局变量的参数,然后准备调用字符串,仅此而已@Fitz事实上,我们有一个类似的项目,一个Talend作业,它通过tSystem
使用命令java-jar myjar arg1 arg
Ah调用一个现有的jar,我没有想到这一点。将符合要求,但在我看来也很难维持。此外,使用这种方法,Talend一开始就不需要。在某些情况下,使用现有代码可以缩短上市时间,而Talend只用于监视和记录目的!有趣的方法。因此,在本例中,您将使用存储在某处的其他日志数据,可能是通过管理控制台,并仅使用库中的代码,例如?@Fitz如果您的意思是每次使用不同的参数调用代码,是的,这是可能的,您将需要准备作为全局变量的参数,然后准备调用字符串,仅此而已@Fitz事实上,我们有一个类似的项目,一个Talend作业,它通过tSystem
使用命令java-jar myjar arg1 arg