gpt4 book ai didi

java - 将fileinputstream输入中的文件分开并将数据插入到oracle表中

转载 作者:行者123 更新时间:2023-12-02 02:23:11 26 4
gpt4 key购买 nike

目前,我正在从以 'gz' 结尾的文件处读取文件输入,并将此输入文件数据处理到我的 Oracle 表中。

最初我只有路径中以'out.gz'结尾的文件,例如文件名看起来像'xyz.out.gz'。我插入数据的表名称类似于 BB_xyz_IMPORT

但现在我的文件也位于同一路径中,以 'px.gz' 结尾,例如文件名看起来像 'xyz.px.gz' > 我插入数据的表名称类似于 BB_xyz_px_IMPORT

表名称约定始终如下所示,以便可以区分要在“out.gz”文件和“px.gz”之间插入的结束文件名输入数据' 文件。

所以我想修改我的java代码,以便它应该适本地获取这两个文件,区分以'out.gz''px.gz'结尾的文件 code> 并将该文件数据分别插入到正确的oracle表中。

下面是我的java代码。目前,代码无法识别以“out.gz”文件和“px.gz”结尾的文件名,并且这两个文件都将数据插入到同一个表中例如“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;
}
}

最佳答案

你已经写了:

... inputFile.endsWith("gz") ? ... : ...;

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

所以你获取表名的方法:

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

关于java - 将fileinputstream输入中的文件分开并将数据插入到oracle表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com