带有args的Java程序,用于在Talend中实现 [英] Java Program with args to implement in Talend

查看:142
本文介绍了带有args的Java程序,用于在Talend中实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求编写一个Java程序,它将TSV文件作为inpunt并生成一个不同的TSV文件(在其中有很多变化,在输入和args上变化很大)作为输出。

I have been asked to write a Java program that would take TSV files as an inpunt and generate a different TSV file (with quite some changes inside it that are very variable on the input and args) as output.

这是一个非常大的程序(花了3天时间编写代码,但我不是很好),它最终处理来自15k行的输入,产生1500K行输出。

It is a pretty big program (took me 3 days to code but I'm not good) and it is finally working on inputs from 15k lines, generating 1500K lines output.

编写代码后,我不知道我之后必须在Talend中实现它,所以它是一个普通的Java程序需要4个args:输入文件的名称,Name输出文件的内容,int,int

Writing the code, I had no idea that I would have to implement it in Talend afterwards, so it is a normal Java program that takes 4 args : Name of the input file, Name of the output file, int , int

我设法将我的Main作为例程与所需的额外包(个人和openCSV)。

I managed to put my Main as a routine with the needed extra packages (personal and openCSV).

我的问题是:是否有可能在Talend中实现它而不更改它?我不能告诉Talend,这里是输入文件,这些是args ?我昨天才听说Talend。

My question is : Is it possible to implement it in Talend without changing it? Can't I just tell Talend, here is the file in input and those are the args? I had never heard of Talend before yesterday.

如果你有兴趣,这是主要的,但我相信我的问题非常通用。

Here is the main if you are interested but i believe my question to be pretty generic.

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

    }

}



<感谢您阅读此内容!

Thanks for reading this far!

推荐答案

如果需要保持原样,您可以将代码打包为任何可运行的代码格式,然后通过Talend tSystem 组件或 tLibraryLoad 组件调用它,如果它是一个Jar文件。

If it's necessary to keep it as it's, you can package your code as any runnable format, then invoke it through Talend tSystem component or tLibraryLoad component if it's a Jar file.

这篇关于带有args的Java程序,用于在Talend中实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆