3

私のプロジェクトはdblpから xml ファイルを解析し、ORM データベースに保存するのに約 1Gb を使用するため、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, ... 出版物の

ステップ 2: データベース内の複製された作成者、発行者、... を確認します。ステップ 3: パブリケーションをデータベースに保存 End: データベースが終了するまで次の要素に移動します。

ファイルdblpを約10MB実行すると、正常に実行されます(11分)-約21000 publicaitonがデータベースに入力されます。しかし、完全な dblp ファイル (1GB) を実行すると、データベースで約 75000 pub (約 1 日) を見たとき、Netbean スローが応答しません。データベースに入力します(ただし、dblp には約 170 万のパブがあります)。

長い質問で申し訳ありませんが、どなたか助けていただければ幸いです。問題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 (またはコードを実行するその他のツール) がクラッシュする理由は単純です。大規模なデータセットでは、休止状態のステートフル セッションが多くのメモリを消費します。

解決策:「ステートレス」休止状態セッションを使用します。大規模な休止状態 ETL ジョブには、ステートレス セッション (オブジェクトの状態を追跡しない) を使用することになっています。

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

「StatelessSessions」が ETL スタイルの hibernate ジョブにとって重要な理由:通常の JVM は 1g 未満の RAM で実行されるため、hibernate を介して 10 ギガのデータをロードする場合、セッションでオブジェクトをキャッシュしないように注意する必要があります。

その他の最適化:

1) ハイバネート プログラムを非常に高性能なサーバー、またはデータベース サーバーに対してローカルなサーバーで実行します。

2) インポーターの実行中は、データベース サーバーで外部キー チェックとインデックス作成を無効にします。

3) 他の休止状態の Java/GC の最適化を調べます。たとえば、ガベージ コレクションの負担を軽減したり、JVM メモリを増やしたりします。 Java アプリケーション サーバーのパフォーマンス

この問題を解決するには、セッション タイプを変更し、挿入、コミット、およびフラッシュの方法を少し編集する必要があります。

異なるセッション タイプを使用する理由:

休止状態が遅くなったり、応答を停止したりする場合は、JVM をプッシュしすぎている可能性があります。これは、メモリの制限です (つまり、JVM メモリで管理されている多くのオブジェクトを作成したことを意味します)。Hibernate の優れた Web アプリのパフォーマンスは、データベースで照会または更新する Bean の状態とライフサイクルを管理する機能によるものです。通常、隠れたコストを認識せずに、標準の Web アプリでこの利点を享受しています。したがって、大規模な ETL スタイルのデータベース プログラミングを行う場合、キャッシュも状態も不要で、高速で書き込むだけです。これは、ステートレス セッションを使用することを意味します。これは、より洗練された Web 最適化された休止状態のステートフル セッション管理を試みません。

于 2011-12-27T04:41:42.127 に答える
0

大量のデータをロードする場合は、休止状態をスキップすることを検討し、プリペアドステートメントでバッチ挿入を使用してJDBCを使用する必要があります。また、データベースに挿入された後、saxパーサーがアイテムを保持していないことを確認してください。メモリがすぐに不足します。

于 2011-12-27T04:47:44.833 に答える