gpt4 book ai didi

java - 使用 JDBC 高效插入星型模式

转载 作者:行者123 更新时间:2023-12-01 19:11:55 24 4
gpt4 key购买 nike

我有星型架构模型,其中服务器表包含有关服务器名称的信息。 信息表包含我想要的特定服务器的信息。 实际数据表包含有关哪个服务器包含哪些信息的信息。

Server Table

enter image description here

Information Table

enter image description here

Actual Data Table

enter image description here

现在我遇到的问题是 - 我正在尝试使用 JDBC 将数据插入到数据表中。但我不确定应该如何将数据添加到星型模式模型中的实际数据表中。我应该连接到数据库并每次为每条信息插入它,还是有任何直接的方法可以通过仅与数据库通信一次来做到这一点。这是我的代码,我在其中获取每个服务器的所有信息。 IndexData 是我将值插入 Oracle 数据库的类。

    public void fetchlog() {
InputStream is = null;
InputStream isUrl = null;
FileOutputStream fos = null;
try {
is = HttpUtil.getFile(monitorUrl);
if(monitorUrl.contains("stats.jsp") || monitorUrl.contains("monitor.jsp")) {
trimUrl = monitorUrl.replaceAll("(?<=/)(stats|monitor).jsp$", "ping");
}
isUrl = HttpUtil.getFile(trimUrl);
BufferedReader in = new BufferedReader (new InputStreamReader (is));
String line;
int i=0,j=0,k=0;
while ((line = in.readLine()) != null) {
if(line.contains("numDocs")) {
docs = in.readLine().trim();
//So should I keep on inserting into Database for each information, like this
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, docs);
} else if(line.contains("indexSize")) {
indexSize = in.readLine().trim();
//For this information-- the same way?
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, indexSize);
} else if(line.contains("cumulative_lookups")) {
cacheHits= in.readLine().trim();
//For this information too-- the same way?
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, cacheHits);
} else if(line.contains("lastCommitTime")) {
lastCommitTime = in.readLine().trim();
//For this information too-- the same way?
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, lastCommitTime );

}

BufferedReader inUrl = new BufferedReader (new InputStreamReader (isUrl));
String lineUrl;
Pattern regex = Pattern.compile("<str name=\"status\">(.*?)</str>");

while ((lineUrl = inUrl.readLine()) != null) {
System.out.println(lineUrl);
if(lineUrl.contains("str name=\"status\"")) {
Matcher regexMatcher = regex.matcher(lineUrl);
if (regexMatcher.find()) {
upDown= regexMatcher.group(1);
//For this information too-- the same way?
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, upDown);

}
System.out.println("Status:- " + status);
}
}
//Or is there some other way we can insert directly into database by communicating with database only one time not multiple times for each information.
//IndexData id = new IndexData(timeStamp, ServerName, InformationName, Value);
fos = new FileOutputStream(buildTargetPath());
IOUtils.copy(is, fos);
} catch (FileNotFoundException e) {
log.error("File Exception in fetching monitor logs :" + e);
} catch (IOException e) {
log.error("Exception in fetching monitor logs :" + e);
}
}

I hope question is clear to everyone. Any suggestions will be appreciated.

最佳答案

我建议您看两件事。首先,使用批量插入在一个 JDBC 事务中执行所有关联的插入。欲了解更多信息:

JDBC Batch Insert Example

我还强烈建议您使用 JDBC 连接池库。我们将 c3p0 与 Postgres 数据库一起使用。您可以在这里找到更多信息:

c3p0 Project Page

基本思想是在启动时创建一个连接池,然后为每组相关插入创建 JDBC 批处理。

关于java - 使用 JDBC 高效插入星型模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8128176/

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