这是交易:
我被要求开发一个 JAVA 程序,该程序将对 .tsv 文件进行一些重组(移动单元格以进行某种转置)。
所以,我尝试干净利落地完成它,现在得到了 3 个不同的包:
.
只需 tsvExceptions
和 tsvTranspositer
即可使主程序 (TSVTransposer.java
) 工作。
昨天我得知我必须自己在 Talend 中实现它,这是我从未听说过的。
所以通过搜索,我踩到了这个stackOverflow topic 。因此,我按照步骤操作,创建一个例程,将我的 main 复制/粘贴到其中(将包更改为“例程”),并向其中添加所需的外部库(我的两个包导出为 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();
}
}
}
}
我知道异常还没有得到正确处理,但他们急于让它以某种方式发挥作用。
稍后会出现的另一个问题是我不知道如何解析程序所需的参数。
无论如何,感谢您阅读这篇文章!
最佳答案
您无法通过拖放到作业来添加例程。您将需要通过组件访问例程函数。
例如,您可以从 tFileListInput 开始获取所需的所有文件。然后,您可以添加一个 tFileInputDelimited 来描述输入的所有字段。之后,例如一个 tJavaRow 组件,您可以编写一些代码来访问您的例程。
注意:请记住,Talend 通常按行工作。这意味着您的例程应按行方式处理内容。这也可能意味着您的代码必须进行相应的重构。 main
函数不起作用,它至少必须成为一个可以实例化或具有 static
函数的类。
如果您想自己处理所有事情,则可以使用 tJava 组件来代替 tJavaRow 组件,这会增加更多的灵 active 。
不过,这并不像简单地添加例程那么一切都会起作用。
事实上,整个代码本身就可以成为一项工作。 Talend 为您生成完整的 Java 代码:
- 参数可以成为
上下文变量
。 - 可以通过多种方式检查数字是否为数字,例如使用 tPreJob 和 tJava
- 输入文件可以与带有点分隔符的 tFileInputDelimited 连接
- 然后,每一行都将使用带有您的自定义代码的 tJavaRow 进行处理,如果不太复杂,则使用 tMap 进行处理。
- 之后,您可以使用 tFileOutputDelimited 组件写入文件
- 一切都将通过右键单击/main来连接以迭代行
所有异常处理均由 Talend 完成。如果你想对异常使用react,你可以使用像tLogRow这样的组件。
希望这有助于确定方向。
关于java - 在 Talend 中实现我的 Java 程序 : can't Drag & Drop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38103422/