带有要在 Talend 中实现的参数的 Java 程序

标签 java talend

我被要求编写一个 Java 程序,它将 TSV 文件作为输入并生成一个不同的 TSV 文件(其中有相当多的更改,这些更改在输入和参数上非常可变)作为输出。

这是一个相当大的程序(我花了 3 天的时间来编码,但我写得不好),它最终可以处理 15k 行的输入,生成 1500K 行的输出。

编写代码时,我不知道之后必须在 Talend 中实现它,因此它是一个普通的 Java 程序,需要 4 个参数:输入文件的名称、输出文件的名称、 int 、 int

我设法将我的 Main 作为一个例程,并包含所需的额外软件包(个人和 openCSV)。

我的问题是:是否可以在不更改的情况下在 Talend 中实现它?我不能直接告诉 Talend,这是输入中的文件,这些是参数?昨天之前我从未听说过 Talend。

如果您感兴趣,这里是主要问题,但我相信我的问题非常笼统。

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 tSystem 组件或 tLibraryLoad 组件(如果它是 Jar)调用它文件。

关于带有要在 Talend 中实现的参数的 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118617/

相关文章:

java - Android API 25 之后,SQLite db 文件在设备/模拟器上的位置是什么?

java - PSQL异常 : This connection has been closed - Spring Boot + AWS RDS + Postgres

java - 在 Talend 5.5 中使用正则表达式的 jsonpath

talend - 如何使用post方法发送多个请求

jboss - 请 Talend Open Studio ESB 用户反馈(相对于 jbossesb/mule/servicemix)

database - Talend如何将一个数据库复制到另一个数据库

localization - 法语格式的 Talend 和数值

java - SpringMVC 处理剩余 Controller 中的错误

java - Saxon 免费版本中的 XQuery

java - 进程被终止后 R.java 中的 Android NoSuchField 错误