Orientdb:在内存中导入数据库并将其用作图形

时间:2016-02-14 12:01:17

标签: java orientdb

这是我的Java DB类,我在其中打开数据库并在内存图数据库中导入数据库导出文件,我在其中定义测试用例的所有数据库模式信息。

操作进展顺利,但如何以图形实例的形式访问导入的数据库而不是数据库的文档实例?

我尝试了很多东西,但我失败了......

错误:

Person类存在于我的架构中,所以其他东西出错了。

Caused by:
> com.orientechnologies.orient.core.exception.OCommandExecutionException:
> Class 'PERSON' was not found in current database

代码:

import com.orientechnologies.orient.core.db.tool.ODatabaseExportException;
import com.orientechnologies.orient.core.db.tool.ODatabaseImport;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import lombok.Getter;

import java.io.IOException;

public class Db {

    @Getter private static OrientGraphFactory factory;
    @Getter private static OrientGraphNoTx graph;

    static public void main(String[] args){
        open("memory","database");
        importDB("/schemas/diary-11202016.gz");
        try {
            seed();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        closeDB();
    }

    public static void open(String dbType, String dbUrl) {

        String dbInfo = dbType + ":" + dbUrl;
        System.out.println(dbInfo);

        factory = new OrientGraphFactory(dbInfo, "root", "root").setupPool(1, 10);
        graph = factory.getNoTx();
    }

    public static void importDB(String path) {
        try {
            ODatabaseImport importDb = new ODatabaseImport(graph.getRawGraph(), Db.class.getResourceAsStream(path), (iText) -> {
                System.out.print(iText);
            });
            importDb.setMerge(true);
            importDb.importDatabase();
            importDb.close();
            System.out.println("\nImporting database: OK");
        } catch (ODatabaseExportException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void seed() throws InterruptedException {
        System.out.println("Starting to seed...");
        for (Vertex v : (Iterable<Vertex>) graph.command( new OCommandSQL("select from Person")).execute()) {
            System.out.println("- Bought: " + v.getProperty("name"));
        }
        System.out.println("Finish to seed...");
    }

    public static void closeDB() {
        factory.close();
    }
}

1 个答案:

答案 0 :(得分:0)

替换以下代码

ODatabaseImport importDb = new ODatabaseImport(graph.getRawGraph(), Db.class.getResourceAsStream(path), (iText) -> {
                System.out.print(iText);
            });
importDb.setMerge(true);

ODatabaseImport importDb = new ODatabaseImport(graph.getRawGraph(), path, (iText) -> {
                System.out.print(iText);
            });
// importDb.setMerge(true);