Java代码修改需求:区分.gz后缀文件导入对应Oracle表
Fixing Table Mapping for .out.gz vs .px.gz Files in Your Oracle Import Workflow
Got it, let's tackle this problem. The root issue here is your tablenameFromFilename method—it only grabs the part of the filename before the first dot, so both xyz.out.gz and xyz.px.gz end up pointing to the same BB_XYZ_IMPORT table. We need to update this method to detect the specific file suffixes and generate the correct table names accordingly.
Here's the revised tablenameFromFilename method, with explanations of the changes:
private String tablenameFromFilename(String path) { String filename = Paths.get(path).getFileName().toString(); String baseName; String tableSuffix = "_IMPORT"; // Check for .px.gz first (it's a longer suffix, so we don't accidentally match .gz first) if (filename.endsWith(".px.gz")) { // Grab everything before the .px.gz suffix baseName = filename.substring(0, filename.lastIndexOf(".px.gz")); tableSuffix = "_PX_IMPORT"; } else if (filename.endsWith(".out.gz")) { // Grab everything before the .out.gz suffix baseName = filename.substring(0, filename.lastIndexOf(".out.gz")); } else { // Fallback to original behavior for unexpected file types baseName = filename.substring(0, filename.indexOf('.')); log.warn("Unexpected file suffix for {} - using default table naming", filename); } return "BB_" + baseName.toUpperCase() + tableSuffix; }
What Changed:
- Targeted Suffix Checks: We check for
.px.gzfirst because it's a longer suffix—this prevents us from mistakenly treating it as just a.gzfile. Then we handle.out.gzspecifically. - Dynamic Suffix for Tables: For
.px.gzfiles, we swap the default_IMPORTsuffix with_PX_IMPORTto match your required table naming rule. - Fallback with Warning: If the code encounters a file with an unexpected suffix, it falls back to the original naming logic and logs a warning—this keeps existing functionality working for other file types while alerting you to odd cases.
Full Updated Class (only modified sections shown):
public class BIImportWorkflow extends AbstractBloombergWorkflow { private final static Logger log = LoggerFactory.getLogger(BIImportWorkflow.class); @Override public boolean run(CommandLine commandLine) throws Exception { // Your existing run method code stays exactly the same here 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; } // Updated table name generation method private String tablenameFromFilename(String path) { String filename = Paths.get(path).getFileName().toString(); String baseName; String tableSuffix = "_IMPORT"; if (filename.endsWith(".px.gz")) { baseName = filename.substring(0, filename.lastIndexOf(".px.gz")); tableSuffix = "_PX_IMPORT"; } else if (filename.endsWith(".out.gz")) { baseName = filename.substring(0, filename.lastIndexOf(".out.gz")); } else { baseName = filename.substring(0, filename.indexOf('.')); log.warn("Unexpected file suffix for {} - using default table naming", filename); } return "BB_" + baseName.toUpperCase() + tableSuffix; } // Your other methods (addOptions, getName, updateDatabase) remain unchanged @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; } }
Why This Works:
- For
xyz.out.gz, it pullsxyzas the base name and appends_IMPORT, giving youBB_XYZ_IMPORT. - For
xyz.px.gz, it pullsxyzand appends_PX_IMPORT, resulting inBB_XYZ_PX_IMPORT. - Even if your filenames have extra dots (like
abc.def.out.gz), this code will correctly extractabc.defas the base name instead of stopping at the first dot.
内容的提问来源于stack exchange,提问作者Marcus




