2017-06-15 86 views
1

我想加载到多个线程的Orient DB的大量数据。 我使用OrientDB 2.2.20和Java 1.8.0_131在样本测试客户端下运行。 但是当我使用5个线程和10000个样本运行此客户端时,客户端的CPU使用率超过了100%,并且进程几乎死亡。OrientDB客户端CPU超过100%与ODB2.2.20

其实我想用图形API在它们之间创建大量的顶点和边。 但我在一些文章中看到,对于大量插入使用文档API并在&中使用doc 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

感谢第一种方法的工作。 您是否还有Graph API的示例代码? – pcd2017

+0

您可以在文档中找到它:http://orientdb.com/docs/last/Graph-Database-Tinkerpop.html。它是一样的:创建一个OrientGraph并在完成后关闭它 –