当前位置: 首页 > 工具软件 > OrientDB > 使用案例 >

JAVA 使用OrientDB(优化)

吕高昂
2023-12-01

OrientGraphUtils 图操作

@Component
public class OreintDBFactory {

    private static final Logger logger = LoggerFactory.getLogger(OreintDBFactory.class);

    private String url;

    private String dataBase;

    private String user;

    private String password;

    private int poolMax;

    private int poolMin;

    //private OObjectDatabaseTxPooled objPool;

    private OrientGraphFactory factory;

    private OrientDBObject orientDB;

    public OreintDBFactory(@Value("${orientdb.url}") String url, @Value("${orientdb.dataBase}") String dataBase, @Value("${orientdb.user}") String user, @Value("${orientdb.password}") String password, @Value("${orientdb.poolMax}") int poolMax, @Value("${orientdb.poolMin}") int poolMin) {
        this.url = url;
        this.dataBase = dataBase;
        this.user = user;
        this.password = password;
        this.poolMax = poolMax;
        this.poolMin = poolMin;
    }

    @PostConstruct
    private void init() {
        //orient = new OrientDB("remote:localhost", OrientDBConfig.defaultConfig());
        //db = orient.open("kbms", "admin", "admin");
        //objPool = new OObjectDatabaseTxPooled(OObjectDatabasePool.global(poolMin, poolMax), url + BaseFieldConfig.TABMARKS + dataBase, user, password);
        //设置文件存储文件大小
        System.setProperty("network.binary.maxLength", "2097151");
        System.setProperty("db.pool.idleTimeout", "60");
        System.setProperty("db.pool.idleCheckDelay", "30");
        factory = new OrientGraphFactory(url + BaseFieldConfig.TABMARKS + dataBase, user, password).setupPool(poolMin, poolMax);

        orientDB = new OrientDBObject(url + BaseFieldConfig.TABMARKS, OrientDBConfig.defaultConfig());


        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            factory.close();
            orientDB.close();
        }));
    }

    //获取图对象
    public OrientGraph getGraph() {
        return factory.getTx();
    }

    //根据classname获取对象
    public ODocument getDocClient(String iClassName) {
        return factory.getDatabase().newInstance(iClassName);
        //return docPool.newInstance(iClassName);
    }

    //public OObjectDatabaseTx getClient(String ipakage) {
    //	OObjectDatabaseTx db = objPool;
    //	db.getEntityManager().registerEntityClasses(ipakage);
    //	return db;
    //}

    /**
     * 新版本获取 ODatabaseSession
     * doc
     *
     * @return
     */
    public ODatabaseSession getDocClient() {
        return factory.getDatabase();
    }

    /**
     * 新版本 object
     */
    public ODatabaseObject getObjectClient(Class clazz) {
        ODatabaseObject dbObj = orientDB.open(dataBase, user, password);
        // REGISTER CLASS ONLY ONCE AFTER DB IS OPENED/CREATED
        dbObj.getEntityManager().registerEntityClass(clazz);
        return dbObj;
    }

    //关闭实例
    public static void close(OrientGraph graph, ODatabaseSession session, ODatabaseObject object) {
        if (null != graph) {
            graph.shutdown();
        }
        if (null != session) session.close();
        if (null != object) {
            object.close();
        }
    }
}



pom文件:

  <!-- 图数据 start -->
        <!--orient Jar -->
        <!--<dependency>-->
            <!--<groupId>com.orientechnologies</groupId>-->
            <!--<artifactId>orientdb-core</artifactId>-->
            <!--<version>3.0.18</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-graphdb</artifactId>
            <version>3.0.18</version>
        </dependency>
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-object</artifactId>
            <version>3.0.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.orientechnologies/orientdb-client -->
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-client</artifactId>
            <version>3.0.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.orientechnologies/orientdb-enterprise -->
        <!--<dependency>-->
            <!--<groupId>com.orientechnologies</groupId>-->
            <!--<artifactId>orientdb-enterprise</artifactId>-->
            <!--<version>2.2.0-beta</version>-->
        <!--</dependency>-->

        <dependency>
            <groupId>com.tinkerpop.blueprints</groupId>
            <artifactId>blueprints-core</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>spring-data-orientdb-object</artifactId>
            <version>0.13</version>
        </dependency>
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>spring-data-orientdb-document</artifactId>
            <version>0.13</version>
        </dependency>
        <!-- 图数据 end -->

注意:有些保存字段,纯属常规字段,自己编写

orientdb.url=remote:localhost
orientdb.dataBase=kbms
orientdb.user=****
orientdb.password=****

 类似资料: