如何使用Java API for OrientDB 3.0创建索引?

时间:2020-04-26 12:22:41

标签: java orientdb graph-databases

我正在使用oriendb multi-model java api。我使用OVertexOEdge类存储文档。它们从OElement类继承。看来OElement类似乎没有公开createIndex()方法。我知道,如果我们使用OClass创建类并保存文档,这是可能的。

如果我使用OVertexOEdge类,如何使用多模型API创建索引。

我缺少链接[OVertex,OEdge]--inherits-from-->[OElement]--(?)-->[OClass]

1 个答案:

答案 0 :(得分:0)

如果您使用的是JAVA多模型API,我发现的最干净的方法是:

// create the connection pool for orientdb
OrientDB orient = new OrientDB(orientUrl, OrientDBConfig.defaultConfig());
OrientDBConfigBuilder poolCfg = OrientDBConfig.builder();
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MIN, 2);
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MAX, 5);
ODatabasePool pool = new ODatabasePool(orientUrl, databaseName, orientUser, orientPass, poolCfg.build());

// acquire the orient pool connection
try (ODatabaseSession db = pool.acquire()) {
        // check and create vertex/edge class
        if (db.getClass("className") == null) {
            // create the class if it does not exist in the DB
            OClass orientClass =
                    db.createVertexClass("className");
            // OR db.createEdgeClass("className");
            orientClass.createProperty("id", OType.STRING);
            orientClass.createIndex("id", OClass.INDEX_TYPE.UNIQUE, "id");
        }
       // now create the OVertex/OEdge/OElement
       OVertex vertex = db.newVertex("className");
       // add properties to your document
       vertex.setProperty("id", id);
       ...
// release the connection back to the pool
} finally {
        orient.close();
    }

我尚未在文档中找到此内容,因此它可能对某人有所帮助。