public static void main(String[] args) { /** * 指定Neo4j存储路径 */ File file = new File(“D:\Documents\Neo4j\default.graphdb2”); //neo4j数据存储地址 GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(file);
//创建事物
Transaction tx=graphDB.beginTx();
try{
/**
* 新增User节点
* 每个节点设置name属性
* 添加Lable以区分节点类型
*/
Node user1 = graphDB.createNode();
user1.setProperty("name","John Johnson");
user1.addLabel(MyLabels.USERS);
Node user2 = graphDB.createNode();
user2.setProperty("name", "Kate Smith");
user2.addLabel(MyLabels.USERS);
Node user3 = graphDB.createNode();
user3.setProperty("name", "Jack Jeffries");
user3.addLabel(MyLabels.USERS);
/**
* 为user1添加Friend关系
* 注:Neo4j的关系是有向的箭头,正常来讲Friend关系应该是双向的,
* 此处为了简单起见,将关系定义成单向的,不会影响后期的查询
*/
user1.createRelationshipTo(user2,MyRelationshipTypes.IS_FRIEND_OF);
user1.createRelationshipTo(user3,MyRelationshipTypes.IS_FRIEND_OF);
/**
* 新增Movie节点
* 每个节点设置name属性
* 添加Lable以区分节点类型
*/
Node movie1 = graphDB.createNode();
movie1.setProperty("name", "Fargo");
movie1.addLabel(MyLabels.MOVIES);
Node movie2 = graphDB.createNode();
movie2.setProperty("name", "Alien");
movie2.addLabel(MyLabels.MOVIES);
Node movie3 = graphDB.createNode();
movie3.setProperty("name", "Heat");
movie3.addLabel(MyLabels.MOVIES);
/**
* 为User节点和Movie节点之间添加HAS_SEEN关系
* HAS_SEEN关系设置stars属性
*/
Relationship relationship1 = user1.createRelationshipTo(movie1, MyRelationshipTypes.HAS_SEEN);
relationship1.setProperty("stars", 5);
Relationship relationship2 = user2.createRelationshipTo(movie3, MyRelationshipTypes.HAS_SEEN);
relationship2.setProperty("stars", 3);
Relationship relationship6 = user2.createRelationshipTo(movie2, MyRelationshipTypes.HAS_SEEN);
relationship6.setProperty("stars", 6);
Relationship relationship3 = user3.createRelationshipTo(movie1, MyRelationshipTypes.HAS_SEEN);
relationship3.setProperty("stars", 4);
Relationship relationship4 = user3.createRelationshipTo(movie2, MyRelationshipTypes.HAS_SEEN);
relationship4.setProperty("stars", 5);
System.out.println("成功");
tx.success();
}catch (Exception e){
}finally {
//关闭数据库
graphDB.shutdown();
}
}
具体代码如上,执行的时候遇到以下错误:
Exception in thread "main" java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.CommunityFacadeFactory, D:\Documents\Neo4j\default.graphdb2
at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.initFacade(GraphDatabaseFacadeFactory.java:144)
at org.neo4j.kernel.impl.factory.CommunityFacadeFactory.initFacade(CommunityFacadeFactory.java:40)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:108)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.newDatabase(GraphDatabaseFactory.java:99)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.lambda$createDatabaseCreator$206(GraphDatabaseFactory.java:88)
at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:183)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.newEmbeddedDatabase(GraphDatabaseFactory.java:65)
at Neo4jDemo.main(Neo4jDemo.java:18)
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component ‘org.neo4j.kernel.NeoStoreDataSource@35865fe6’ was successfully initialized, but failed to start. Please see attached cause exception. at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:443) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:107) at org.neo4j.kernel.impl.transaction.state.DataSourceManager.start(DataSourceManager.java:99) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:433) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:107) at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.initFacade(GraphDatabaseFacadeFactory.java:140) … 7 more Caused by: org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByConfigurationException: Failed to start Neo4j with an older data store version. To enable automatic upgrade, please set configuration parameter “dbms.allow_format_migration=true” at org.neo4j.kernel.impl.storemigration.StoreUpgrader.migrateIfNeeded(StoreUpgrader.java:115) at org.neo4j.kernel.impl.storemigration.DatabaseMigrator.migrate(DatabaseMigrator.java:98) at org.neo4j.kernel.NeoStoreDataSource.upgradeStore(NeoStoreDataSource.java:556) at org.neo4j.kernel.NeoStoreDataSource.start(NeoStoreDataSource.java:429) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:433) … 12 more
求大神解答,跪谢!!!
或者这样初始化GraphDatabaseFactory new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder(storeDir) .setConfig( GraphDatabaseSettings.allow_store_upgrade, “true”) .newGraphDatabase();
@pangguoming 我现在重新安装了一个版本,现在是3.3.0 现在配置里allow_store_upgrade=true 有这个配置 但是没有dbms.allow_format_migration=true这条配置