简体   繁体   English

在Talend中实现我的Java程序:不能拖放

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

Here's the deal : 这是交易:

I was asked to developp a JAVA program that would do some reorganisations of .tsv files (moving cells to do some kind of transposition). 我被要求开发一个JAVA程序,该程序将对.tsv文件进行一些重组(移动单元格进行某种转置)。

So, I tried to do it cleanly and got now 3 different packages: 因此,我尝试干净地进行操作,现在获得了3个不同的软件包:

3种不同的套餐 .

Only tsvExceptions and tsvTranspositer are needed to make the main ( TSVTransposer.java ) work. 只有tsvExceptionstsvTranspositer才能使主( TSVTransposer.java )工作。

Yesterday I learned that I would have to implement it in Talend myself which I had never heard of. 昨天,我得知我将不得不在Talend自己实现它,而我从未听说过。

So by searching, i stepped on this stackOverflow topic . 因此,通过搜索,我踩到了这个stackOverflow主题 So i followed the steps, creating a routine, copy/pasting my main inside it (changing the package to "routines") and added the external needed libraries to it (my two packages exported as jar files and openCSV). 因此,我按照步骤进行操作,创建了一个例程,在其中复制/粘贴了我的主文件(将程序包更改为“例程”),并向其中添加了外部所需的库(我的两个程序包分别导出为jar文件和openCSV)。 Now, when I open the routine, no error is showned but I can't drag & drop it to my created job ! 现在,当我打开例程时,未显示任何错误,但无法将其拖放到我创建的作业中!

什么都没发生。

Nothing happens. 什么都没发生。 It just opens the component infos as shown with "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();
            }

        }

    }
}

I know the exceptions aren't correctly handled yet, but they are in some kind of hurry for it to work in some way. 我知道还没有正确处理异常,但是它们以某种方式使它工作很着急。

Another problem that will occur later is that I have no idea how to parse arguments to the program that are required. 稍后将出现的另一个问题是我不知道如何解析所需程序的参数。

Anyway, thanks for reading this post! 无论如何,感谢您阅读本文!

You cannot add routines per drag and drop to a job. 您不能为每次拖放添加例程。 You will need to access the routines functions through components. 您将需要通过组件访问例程功能。

For example, you would start with a tFileListInput to get all files you need. 例如,您将从tFileListInput开始以获取所需的所有文件。 Then you could add a tFileInputDelimited where you describe all fields of your input. 然后,您可以在其中描述输入的所有字段的地方添加一个tFileInputDelimited After this, with eg a tJavaRow component, you can write some code which would access your routine. 之后,使用tJavaRow组件,您可以编写一些访问例程的代码。

NOTE: Keep in mind that Talend works usually row-wise. 注意: 请记住,Talend通常按行工作。 This means that your routines should handle stuff in a row-wise manner. 这意味着您的例程应按行方式处理内容。 This could also mean that your code has to be refactored accordingly. 这也可能意味着必须对代码进行相应的重构。 A main function won't work, this has at least to become a class which can be instanciated or has static functions. 一个main函数将不起作用,这至少必须成为可以实例化或具有static函数的类。

If you want to handle everything on your own, instead of a tJavaRow component you might use a tJava component which adds more flexibility. 如果你想在自己的处理,而不是tJavaRow组件的一切,你可以使用一个tJava组件,它增加了更多的灵活性。

Still, it won't be as easy as simply adding the routine and everything will work. 尽管如此,这并不像简单地添加例程那样容易,并且一切都会正常进行。

In fact, the whole code can become a job on its own. 实际上,整个代码可以自己完成工作。 Talend generates the whole Java code for you: Talend为您生成整个Java代码:

  • The parameters can become Context variables . 参数可以成为Context variables
  • The check if numbers are numbers could be done several ways, for example with a tPreJob and a tJava 检查数字是否为数字可以通过几种方式完成,例如使用tPreJobtJava
  • Input file could be connected with a tFileInputDelimited with a dot separator 输入文件可以与带有点分隔符的tFileInputDelimited连接
  • Then, every row will be processed with either a tJavaRow with your custom code or with a tMap if its not too complex. 然后,将使用带有自定义代码的tJavaRowtMap(如果不太复杂)处理每一行。
  • Afterwards, you can write the file with a tFileOutputDelimited component 之后,您可以使用tFileOutputDelimited组件写入文件
  • Everything will get connected via right click / main to iterate over the rows 一切都将通过右键单击/ main进行连接以遍历各行

All exception handling is done by Talend. 所有异常处理均由Talend完成。 If you want to react to exceptions, you can use a component like tLogRow . 如果您想对异常做出反应,可以使用类似tLogRow的组件。

Hope this helps a bit to set the direction. 希望这有助于设定方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM