在 Talend 中实现我的 Java 程序:无法拖放

Implementing my Java Program in Talend : can't Drag & Drop

这是交易:

我被要求开发一个 JAVA 程序来对 .tsv 文件进行一些重组(移动单元格以进行某种换位)。

所以,我尝试干净地完成它,现在得到了 3 个不同的包:

.

只需要 tsvExceptionstsvTranspositer 即可使主要 (TSVTransposer.java) 工作。

昨天我了解到我必须自己在 Talend 中实现它,这是我从未听说过的。

所以通过搜索,我踩到了这个。所以我按照这些步骤创建了一个例程,copy/pasting 我在其中的主要内容(将包更改为 "routines")并向其中添加了外部所需的库(我的两个包导出为 jar 文件和 openCSV)。现在,当我打开例程时,没有显示任何错误,但是 我无法将其拖放到我创建的作业中!

没有任何反应。它只是打开组件信息,如 "Properties not available."

所示
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();
            }

        }

    }
}

我知道异常还没有得到正确处理,但他们有点急于让它以某种方式工作。

稍后会出现的另一个问题是我不知道如何解析所需程序的参数。

无论如何,感谢阅读本文post!

您不能通过拖放作业来添加例程。您将需要通过组件访问例程功能。

例如,您将从 tFileListInput 开始,以获取您需要的所有文件。然后你可以添加一个 tFileInputDelimited 来描述你输入的所有字段。在此之后,例如tJavaRow 组件,您可以编写一些代码来访问您的例程。

注意:请记住,Talend 通常按行工作。这意味着您的例程应该按行处理内容。这也可能意味着您的代码必须相应地重构。 main 函数将不起作用,这至少必须成为可以实例化或具有 static 函数的 class。

如果您想自己处理所有事情,而不是 tJavaRow 组件,您可以使用 tJava 组件增加了更多的灵活性。

不过,它不会像简单地添加例程那样容易,一切都会正常进行。

事实上,整个代码本身就可以成为一项工作。 Talend 为您生成整个 Java 代码:

  • 参数可以变成Context variables.
  • 可以通过多种方式检查数字是否为数字,例如使用 tPreJobtJava
  • 输入文件可以用带点分隔符的 tFileInputDelimited 连接
  • 然后,每一行都将使用带有自定义代码的 tJavaRowtMap 进行处理如果不是太复杂的话。
  • 之后,您可以使用 tFileOutputDelimited 组件写入文件
  • 一切都将通过右键单击/主要连接以遍历行

所有异常处理均由 Talend 完成。如果你想对异常做出反应,你可以使用像 tLogRow.

这样的组件

希望这对确定方向有所帮助。