Search code examples
javafileinputstream

separate the file in fileinputstream input and insert the data into oracle tables


Currently i am reading the file input from where the file ending with 'gz' and processing this input file data into my oracle tables.

Initially i was having only the files in the path which is ending with 'out.gz' for example file name looks like 'xyz.out.gz'. And the table name where i am inserting the data looks like BB_xyz_IMPORT.

But now i am also having the files in the same path which is ending with 'px.gz' for example file name looks like 'xyz.px.gz' and the table name where i am inserting the data looks like BB_xyz_px_IMPORT.

The table name conventions always looks like this so that it can differentiate the ending file name input data to insert between 'out.gz' file and 'px.gz' file.

So i want to modify my java code such that it should take the both the files appropriately, differantiate the file ending with 'out.gz' and 'px.gz' and insert this file data into correct oracle tables respectively.

Below is my java code. Currently the code is not recognizing the file name ending between 'out.gz' file and 'px.gz' and both the file is inserting the data in the same table for example which is 'BB_xyz_IMPORT'.

    public class BIImportWorkflow extends AbstractBloombergWorkflow {
    private final static Logger log = LoggerFactory.getLogger(BIImportWorkflow.class);

    @Override
    public boolean run(CommandLine commandLine) throws Exception {
        AnnotationConfigApplicationContext applicationContext = initializeApplicationContext();

        String inputFile = commandLine.getOptionValue("in");

        if (inputFile == null) {
            log.error("The input file has not been specified");
            return false;
        }

        log.info("Importiere Daten aus der Datei " + inputFile);

        try (InputStream is = new FileInputStream(inputFile);
                Reader underlyingReader = inputFile.endsWith("gz")
                        ? new InputStreamReader(new GZIPInputStream(is), DEFAULT_CHARSET)
                        : new InputStreamReader(is, DEFAULT_CHARSET);
                BufferedReader reader = new BufferedReader(underlyingReader)) {

            BlImporter BlImporter = applicationContext.getBean(BlImporter.class);
            BlImporter.processFile(reader, tablenameFromFilename(inputFile));
        }
        log.info("Import abgeschlossen");

        return true;
    }

    private String tablenameFromFilename(String path) {
        String filename = Paths.get(path).getFileName().toString();
        return "BB_" + filename.substring(0, filename.indexOf('.')).toUpperCase() + "_IMPORT";
    }

    @Override
    public void addOptions(Options options) {
        options.addOption(null, "in", true, "(bbgimport) specifies the input file");
    }

    @Override
    public String getName() {
        return "bbgimport";
    }

    @Override
    public boolean updateDatabase() {
        AnnotationConfigApplicationContext applicationContext = initializeApplicationContext();
        BlDBMigrator BlDBMigrator = applicationContext.getBean(BlDBMigrator.class);
        BlDBMigrator.updateDB();
        return true;
    }
}

Solution

  • You already wrote it :

    ... inputFile.endsWith("gz") ? ... : ...;
    
    boolean isIn = filePath.endsWith("px.gz");
    boolean isOut = filePath.endsWith("out.gz");
    

    So your method to get your table name :

    private String tablenameFromFilename(String path) {
      String filename = Paths.get(path).getFileName().toString();
      if (filename.endsWith("out.gz") {
         return "BB_"+ filename.substring(0,filename.indexOf('.')).toUpperCase() + "_IMPORT";
      } else if (filename.endsWith("px.gz")) {
        return "BB_" + filename.substring(0, filename.indexOf('.')).toUpperCase() + "_PX_IMPORT";
      } else {
        throw new RuntimeException("Extension unsupported");
      }
    }