3

我的项目从dblp解析 xml 文件,大约 1Gb 保存在 ORM 数据库中,所以我使用 SAXP 读取信息,如(paper'author,paper'infomation ......)并使用 Hibeanate 将数据保存到数据库中:

第 1 步:使用 SAXP 读取如下元素:

<article key="journals/cs/BhaskarAS11" mdate="2011-11-09"><author>Data Ram Bhaskar</author><author>Kasim Karam Abdalla</author><author>Raj Senani</author><title>Electronically-Controlled Current-mode second order Sinusoidal Oscillators Using MO-OTAs and Grounded Capacitors.</title><pages>65-73</pages><year>2011</year><volume>2</volume><journal>Circuits and Systems</journal><number>2</number><ee>http://dx.doi.org/10.4236/cs.2011.22011</ee><url>db/journals/cs/cs2.html#BhaskarAS11</url></article>

获取: Author'name , publicaiton, publisher, ... of publication

第 2 步:检查数据库中的重复作者、出版商、...。第 3 步:在数据库中保存发布结束:转到下一个元素,直到结束数据库。

当我运行大约 10MB 的文件 dblp 时,它运行正常(11 分钟) - 它在数据库中输入了大约 21000 个 publicaiton。但是当运行完整的 dblp 文件(1GB)时,当在数据库中看到它大约 75000 pub(大约 1 天)时,Netbean 抛出没有响应 - 我无法从 netBean 得到任何错误或消息 - 它无法输入任何 pub在数据库中(但在 dblp 中有大约 1,700 万个酒吧)。

很抱歉这个问题很长,但我非常感谢你们能给我的任何帮助!问题 SAXP ?或休眠....?

这里有一些代码:

休眠配置:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cspublicationcrawler</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.transaction.auto_close_session">false</property>
    <property name="hibernate.transaction.flush_before_completion">true</property>

    <property name="hibernate.connection.pool_size">50</property>
    <property name ="hibernate.jdbc.batch_size">500</property> 
    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Magazine.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Reviewer.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Conference.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Author.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Paper.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/RankAuthorKeyword.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Publisher.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Comment.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Domain.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Journal.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/PaperType.hbm.xml"/>
  </session-factory>

休眠实用程序

public HibernateUtil() throws Exception {
    sessionFactoryConfigPath = "";
    sessionFactory = new Configuration().configure().buildSessionFactory();
}

public HibernateUtil(String sessionFactoryConfigPath) {
    this.sessionFactoryConfigPath = sessionFactoryConfigPath;
    sessionFactory = new Configuration().configure(sessionFactoryConfigPath).buildSessionFactory();
}

/**
 * Begin a transaction
 */
protected void beginTransaction() {
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();
}

/**
 * Commit transaction and close session
 */
protected void commitAndClose() {
    if (session != null) {
        for(int i=0;i<10000;i++) {
            if ( i % 500 == 0 ) { //50, same as the JDBC batch size
                                 //flush a batch of inserts and release memory:
                    session.flush();
                    session.clear();
            }               
        }
        session.getTransaction().commit();
        if (session.isOpen()) {
            session.close();
        }
    }
}

SAXP

  public void endElement(String uri, String localName, String qName) throws SAXException {
    try {
        if(!recordTag.equals(WWW)&&!recordTag.equals(PROCEEDINGS)){
            super.endElement(uri, localName, qName);
            if(this.str != null){
                this.value = this.str.toString();
            }
            if (qName.equals(AUTHOR) || qName.equals(EDITOR)) {
                 try {                        
                    String temp = value.replaceAll("'","");
                    author = this.authorBO.checkExitAuthor(temp);
                    if (author ==null)
                    {
                        author = new Author();
                        author.setAuthorName(value);
                        authorBO.addNew(author);
                    }
                     authors.add(author);
                     author=null;
                return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(qName.equals(TITLE)){
                this.paper.setTitle(value);
                return;
            }

            if(qName.equals(BOOKTITLE)){
                if(recordTag.equals(INPROCEEDINGS)){
                    String temp = value.replaceAll("'","");
                    conference = conferenceBO.checkExitConference(temp);
                    if(conference == null)
                    {
                        conference = new Conference();
                        conference.setConferenceName(value);
                        conferenceBO.addNew(conference);
                        this.paper.setConference(conference);
                        conference=null;
                        return;
                    }
                }else {
                    this.paper.setTitle(value);
                    return;
                }
            }

            if(qName.equals(PAGES)){
                this.paper.setPages(value);
                return;
            }

            if(qName.equals(YEAR)){
                this.paper.setYear(Integer.parseInt(value));
                return;
            }

            if(qName.equals(ADDRESS)){
                this.paper.setAdress(value);
                return;
            }
            if(qName.equals(JOURNAL)){
                try {
                    String temp = value.replaceAll("'","");
                    journal = this.journalBO.checkExitJournal(temp);
                    if (journal ==null)
                    {
                        journal = new Journal();
                        journal.setJournalName(value);
                        journalBO.addNew(journal);
                    }

                    this.paper.setJournal(journal);
                    journal =null;                        
                    return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(qName.equals(VOLUME)){
                this.paper.setVolume(value);
                return;
            }

            if(qName.equals(NUMBER)){
                this.paper.setNumber(value);
                return;
            }

            if(qName.equals(MONTH)){
                this.paper.setMonth(value);
                return;
            }

            if(qName.equals(URL)){
                this.paper.setUrl(value);
                return;
            }

            if(qName.equals(EE)){
                this.paper.setEe(value);
                return;
            }

            if(qName.equals(CDROM)){
                this.paper.setCdrom(value);
                return;
            }

            if(qName.equals(CITE)){
                this.paper.setCite(value);
                return;
            }

            if(qName.equals(PUBLISHER)){
                 try {
                    String temp = value.replaceAll("'","");
                    publisher =publisherBO.checkExitPublisher(temp);
                    if (publisher ==null)
                    {
                        publisher = new Publisher();
                        publisher.setNamePublisher(value);
                        publisherBO.addNew(publisher);
                    }

                    this.paper.setPublisher(publisher);
                    publisher=null;                        
                    return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(qName.equals(CROSSREF)){
                this.paper.setCrossref(value);
                return;
            }

            if(qName.equals(ISBN)){
                this.paper.setIsbn(value);
                return;
            }

            if(qName.equals(SERIES)){
                this.paper.setSeries(value);
                return;
            }

            if(qName.equals(SCHOOL)){
                this.paper.setSchool(value);
                return;
            }

            if(qName.equals(CHAPTER)){
                this.paper.setChapter(value);
                return;
            }

            if (qName.equals(recordTag)) {
                this.paper.setAuthors(authors);
                this.paperBO.addNew(paper);
                if(this.authors != null){
                    this.authors = null;
                }
                if(this.paper != null){
                    this.paper = null;
                }
                if(this.str != null){
                    this.str = null;
                }
                return;
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void endDocument() throws SAXException {
    super.endDocument();
}

@Override
public void startDocument() throws SAXException {
    super.startDocument();
    try {
        str = new StringBuffer();
        this.authorBO = AuthorBO.getAuthorBO();
        this.conferenceBO = ConferenceBO.getConferenceBO();
        this.journalBO = JournalBO.getJournalBO();
        this.publisherBO = PublisherBO.getPublisherBO();
        this.paperTypeBO = PaperTypeBO.getPaperTypeBO();
        this.paperBO = PaperBO.getPaperBO();
    } catch (Exception ex) {
        Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    super.startElement(uri, localName, qName, attributes);    
    this.str = new StringBuffer();
    if ((attributes.getLength()>0) && (attributes.getValue("key")!=null)) {
        recordTag = qName;
        this.paper = new Paper();            
        this.authors = new HashSet <Author>();       
        this.paper.setDblpKey(attributes.getValue("key"));            
        if(!recordTag.equals(WWW)&&!recordTag.equals(PROCEEDINGS))
        {
            papertype = this.paperTypeBO.checkExitPaperType(qName);
            if (papertype ==null)
            {
                try {
                    papertype = new PaperType();
                    papertype.setNameType(qName);
                    paperTypeBO.addNew(papertype);
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.paper.setPaperType(papertype);
            papertype=null;
        }
        return;
    }
} 
4

2 回答 2

2

netbeans(或任何其他运行代码的工具)崩溃的原因很简单:在大型数据集上,hibernate 的有状态会话会占用大量内存。

解决方案:使用“无状态”休眠会话 - 对于大型休眠 ETL 作业,您应该使用无状态会话(不跟踪对象的状态)。

//edit the session creation method in HibernateUtil as follows.
    StatelessSession session = sessionFactory.openStatelessSession();

为什么“StatelessSessions”对于 ETL 风格的休眠作业很重要:由于典型的 JVM 使用 < 1g ram 运行,因此当通过休眠加载 10 gigs 数据时,您必须小心不要在会话中缓存对象。

其他优化:

1) 在性能极高的服务器或数据库服务器本地的服务器上运行您的休眠程序。

2) 当导入器运行时,禁用数据库服务器中的任何外键检查和索引。

3) 研究其他休眠 java/GC 优化,例如,减少垃圾收集职责或增加 JVM 内存。 Java 应用服务器性能

为了解决此问题,您需要更改会话类型 - 并对插入、提交和刷新的方式进行一些小的编辑。

为什么我们使用不同的会话类型:

当休眠速度变慢或停止响应时,这可能意味着您也在推动 JVM 内存方面的限制(也就是说,您创建了很多在 JVM 内存中管理的对象)。Hibernate 出色的 web 应用程序性能是由于它能够管理它在数据库中查询或更新的 bean 的状态和生命周期——通常,我们在标准 web 应用程序中获得了这一好处,而没有意识到隐藏的成本。因此,当我们进行大型 ETL 风格的数据库编程时,我们不需要缓存,不需要状态——只需要高速写入。这意味着使用无状态会话,它不会尝试更高级的、针对 Web 优化的休眠状态会话管理。

于 2011-12-27T04:41:42.127 回答
0

If you're going to load that much data, you should consider skipping hibernate, and just used JDBC with batched inserts on a prepared statement. Also make certain that your sax parser is not holding on to items after they've been inserted into the database, you will run out of memory quickly.

于 2011-12-27T04:47:44.833 回答