2017-06-15 4 views
1

多くのデータを複数のスレッドでOrient DBにロードしたい。 私はOrientDB 2.2.20とJava 1.8.0_131を使用して、以下のサンプル・テスト・クライアントを実行しています。 しかし、このクライアントを5つのスレッドと10000のサンプルで実行すると、クライアントのCPU使用率は100%以上になり、プロセスはほとんどなくなります。OrientDBクライアントCPUがODB2.2.20で100%以上になる

実際にグラフAPIを使用して、それらの間に多数の頂点とエッジを作成したかったのです。 しかし、大量の挿入にはドキュメントAPIを使用し、ドキュメントAPIを使用してポインタを&に設定するという記事をいくつか読んでいます。したがって、このプログラムを試してみてください。

誰かがコード内で何が間違っていると指摘できますか?

public OrientDBTestClient(){ 

    db = new ODatabaseDocumentTx(url).open(userName, password); 
} 

public static void main(String[] args) throws Exception{ 

    int threadCnt = Integer.parseInt(args[0]); 
    OrientDBTestClient client = new OrientDBTestClient(); 

    try { 

     db.declareIntent(new OIntentMassiveInsert()); 

     Thread[] threads = new Thread[threadCnt]; 
     for (int i = 0; i < threadCnt; i++) { 
      Thread loadStatsThread = new Thread(client.new LoadTask(Integer.parseInt(args[1]))); 
      loadStatsThread.setName("LoadTask" + (i + 1)); 
      loadStatsThread.start(); 
      threads[i] = loadStatsThread; 
     } 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

private class LoadTask implements Runnable{ 

    public int count = 0; 

    public LoadTask(int count){ 
     this.count = count; 
    } 

    public void run(){ 
     long start = System.currentTimeMillis(); 
     try{ 
      db.activateOnCurrentThread(); 
      for(int i = 0; i < count; ++ i){ 
       storeStatsInDB(i +""); 
      } 
     } 
     catch(Exception e){ 
      log.println("Error in LoadTask : " + e.getMessage()); 
      e.printStackTrace(); 
     } 
     finally { 
      db.commit(); 
      System.out.println(Thread.currentThread().getName() + " loaded: " + count + " services in: " + (System.currentTimeMillis() - start) + "ms"); 
     } 
    } 
} 

public void storeStatsInDB(String id) throws Exception{ 

    try{ 
     long start = System.currentTimeMillis(); 

     ODocument doc = db.newInstance(); 
     doc.reset(); 
     doc.setClassName("ServiceStatistics"); 

     doc.field("serviceID", id); 
     doc.field("name", "Service=" + id); 

     doc.save(); 
    } 
    catch(Exception e){ 
     log.println("Exception :" + e.getMessage()); 
     e.printStackTrace(); 
    } 

} 

答えて

3

dbインスタンスはスレッド間で共有できません。

pool = new OPartitionedDatabasePool("remote:localshot/test", "admin", "admin"); 
Runnable acquirer =() -> { 

    ODatabaseDocumentTx db = pool.acquire(); 

    try { 

    List<ODocument> res = db.query(new OSQLSynchQuery<>("SELECT * FROM OUser")); 


    } finally { 

    db.close(); 
    } 

}; 

//spawn 20 threads 
List<CompletableFuture<Void>> futures = IntStream.range(0, 19).boxed().map(i -> CompletableFuture.runAsync(acquirer)) 
    .collect(Collectors.toList()); 

futures.forEach(cf -> cf.join());` 
+0

ありがとうございました。 グラフAPIのサンプルコードもありますか? – pcd2017

+0

このドキュメントはdocs:http://orientdb.com/docs/last/Graph-Database-Tinkerpop.htmlにあります。これは同じです:終了時にOrientGraphを作成して閉じる –

関連する問題