2014-10-09 59 views
2

我正在嘗試創建一個Oak的JCR資源庫來存儲「Apache Oak over MongoDB」的內容。如何使用MongoMK在JackRabbit Oak中創建資源庫實例

(我絕對不知道) 這是什麼iv'e一直在做。

MongoClient connection = new MongoClient("127.0.0.1", 27017); 
        DB db = connection.getDB("test"); 
        MongoMK.Builder m = new MongoMK.Builder(); 
        MongoMK kernel = m.setMongoDB(db).open(); 
        Repository repo = new Jcr().createRepository(); 
        session = repo.login(); // Error javax.jcr.NoSuchWorkspaceException 

正試圖將「Repository」鏈接到「MongoMK」 - 這看起來像一場噩夢。

我曾嘗試做

Repository repo = new Jcr(kernel).createRepository(); //Error 

我發現類似的東西@ [How to create repository instance in JackRabbit Oak using MicroKernel,這並沒有幫助。

我的問題是,有無論如何連接MongMK - 存儲庫?

P.S - 嘗試使用「NodeStore」。

回答

2

是的,這沒有很好的記錄。以下應該工作:

import javax.jcr.Node; 
import javax.jcr.Repository; 
import javax.jcr.Session; 
import org.apache.jackrabbit.oak.Oak; 
import org.apache.jackrabbit.oak.plugins.document.DocumentMK; 
import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; 
import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider; 
import com.mongodb.DB; 
import com.mongodb.MongoClient; 

public class Test { 
    public static void main(String... args) throws Exception { 
     DB db = new MongoClient("127.0.0.1", 27017).getDB("test2"); 
     DocumentNodeStore ns = new DocumentMK.Builder(). 
       setMongoDB(db).getNodeStore(); 
     Repository repo = new Jcr(new Oak(ns)) 
       .with(new OpenSecurityProvider()) 
       .createRepository(); 
     Session session = repo.login(); 
     Node root = session.getRootNode(); 
     if (root.hasNode("hello")) { 
      Node hello = root.getNode("hello"); 
      long count = hello.getProperty("count").getLong(); 
      hello.setProperty("count", count + 1); 
      System.out.println("found the hello node, count = " + count); 
     } else { 
      System.out.println("creating the hello node"); 
      root.addNode("hello").setProperty("count", 1); 
     } 
     session.save(); 
     session.logout(); 
     ns.dispose(); 
    } 
} 

這也是documented

相關問題