【问题标题】:Insert into Star Schema efficiently using JDBC使用 JDBC 有效地插入星型模式
【发布时间】:2011-11-14 21:12:56
【问题描述】:

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

Server Table

Information Table

Actual Data Table

现在我遇到的问题是 - 我正在尝试使用 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.

【问题讨论】:

    标签: java database-design jdbc star-schema


    【解决方案1】:

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

    JDBC Batch Insert Example

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

    c3p0 Project Page

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

    【讨论】:

    • 我在启动时使用 jdbc 连接池。但我很困惑我应该如何插入数据库。如果架构不是星型架构,那么我已经以一种方式做到了,就像这样,IndexData id = new IndexData(timeStamp, ServerName, updown, indexSize, cacheHits,lastCommit,docs);
    • 这里是一个更好的 URL,它是 Java 中使用 JDBC 的批处理操作示例。假设您已经知道如何从连接池中获取连接:jguru.com/faq/view.jsp?EID=5079
    猜你喜欢
    • 1970-01-01
    • 2020-07-20
    • 2011-04-16
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多