使用JDBC有效地插入Star Schema

时间:2011-11-14 21:12:56

标签: java database-design jdbc star-schema

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

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.

1 个答案:

答案 0 :(得分:4)

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

JDBC Batch Insert Example

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

c3p0 Project Page

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