当前位置:   article > 正文

使用java操作Neo4j数据库_neo4j 创建数据库 for java

neo4j 创建数据库 for java

 

 

1.引言

这里的Java操作Neo数据库使用的是嵌入式模式,Neo4j可分为嵌入式模式和服务端模式,两者的区别可以查看方案(https://blog.csdn.net/regan_hoo/article/details/78771888

也可以查看《Neo4j实战 》这本书的第10章,上面有详细讲解(需要电子版的可以找我)。

我们构造一个案件的场景。比如一个案件中有多个嫌疑人,有的嫌疑人参与了多个案件。

2 程序代码

pom

  1. <dependency>
  2. <groupId>org.neo4j</groupId>
  3. <artifactId>neo4j</artifactId>
  4. <version>3.3.5</version>
  5. </dependency>

Java代码

  1. package neo4j;
  2. import org.neo4j.graphdb.*;
  3. import org.neo4j.graphdb.factory.GraphDatabaseFactory;
  4. import org.neo4j.graphdb.index.Index;
  5. import org.neo4j.graphdb.traversal.Evaluators;
  6. import org.neo4j.graphdb.traversal.TraversalDescription;
  7. import java.io.File;
  8. import java.util.Iterator;
  9. /**
  10. * Created by cgz on 2018-04-17 15:29
  11. * 描述:
  12. */
  13. public class CaseInfoAs {
  14. // private static final String DB_PATH = "testgraph.db";
  15. private static final String DB_PATH = "E:\\soft\\neo4j-community-3.3.5\\data\\databases\\graph.db";
  16. private GraphDatabaseService graphDB;
  17. private enum RelTypes implements RelationshipType {
  18. CRIME, LINK
  19. }
  20. public void init() {
  21. graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(new File(DB_PATH));
  22. }
  23. /**创建数据*/
  24. public void create() {
  25. Transaction tx = graphDB.beginTx();
  26. Node case1 = graphDB.createNode(new CaseLabel("CASEINFO"));
  27. case1.setProperty("name", "案件1");
  28. case1.setProperty("address", "南山");
  29. Node case2 = graphDB.createNode(new CaseLabel("CASEINFO"));
  30. case2.setProperty("name", "案件2");
  31. case2.setProperty("address", "福田");
  32. Node case3 = graphDB.createNode(new CaseLabel("CASEINFO"));
  33. case3.setProperty("name", "案件3");
  34. case3.setProperty("address", "龙华");
  35. Node userA = graphDB.createNode(new CaseLabel("PERSON"));
  36. userA.setProperty("name", "A");
  37. userA.setProperty("idcard", "150302198012228239");
  38. userA.setProperty("tel", "13685246639");
  39. Node userB = graphDB.createNode(new CaseLabel("PERSON"));
  40. userB.setProperty("name", "B");
  41. userB.setProperty("idcard", "370634199208304929");
  42. userB.setProperty("tel", "13885246670");
  43. Node userC = graphDB.createNode(new CaseLabel("PERSON"));
  44. userC.setProperty("name", "C");
  45. userC.setProperty("idcard", "430721198802065735");
  46. userC.setProperty("tel", "13966704782");
  47. Node userD = graphDB.createNode(new CaseLabel("PERSON"));
  48. userD.setProperty("name", "D");
  49. userD.setProperty("idcard", "522730198707118747");
  50. userD.setProperty("tel", "13670478962");
  51. Node userE = graphDB.createNode(new CaseLabel("PERSON"));
  52. userE.setProperty("name", "E");
  53. userE.setProperty("idcard", "622926198609158032");
  54. userE.setProperty("tel", "13047829667");
  55. Node userF = graphDB.createNode(new CaseLabel("PERSON"));
  56. userF.setProperty("name", "F");
  57. userF.setProperty("idcard", "500114197706138305");
  58. userF.setProperty("tel", "13478296670");
  59. Node userG = graphDB.createNode(new CaseLabel("PERSON"));
  60. userG.setProperty("name", "G");
  61. userG.setProperty("idcard", "500114106138305152");
  62. userG.setProperty("tel", "13476670156");
  63. Node userH = graphDB.createNode(new CaseLabel("PERSON"));
  64. userH.setProperty("name", "H");
  65. userH.setProperty("idcard", "500114197704751236");
  66. userH.setProperty("tel", "13296156670");
  67. case1.createRelationshipTo(userA, RelTypes.CRIME);
  68. case1.createRelationshipTo(userB, RelTypes.CRIME);
  69. case1.createRelationshipTo(userC, RelTypes.CRIME);
  70. case1.createRelationshipTo(userD, RelTypes.CRIME);
  71. case2.createRelationshipTo(userB, RelTypes.CRIME);
  72. case2.createRelationshipTo(userC, RelTypes.CRIME);
  73. case2.createRelationshipTo(userE, RelTypes.CRIME);
  74. // case2.createRelationshipTo(userD, RelTypes.CRIME);
  75. case3.createRelationshipTo(userF, RelTypes.CRIME);
  76. case3.createRelationshipTo(userG, RelTypes.CRIME);
  77. case3.createRelationshipTo(userH, RelTypes.CRIME);
  78. tx.success();
  79. tx.close();
  80. }
  81. public void search(String username) {
  82. Transaction tx = graphDB.beginTx();
  83. Node startNode = graphDB.findNode(new CaseLabel("PERSON"), "name", username);
  84. Iterable<Relationship> iterable = startNode
  85. .getRelationships(RelTypes.CRIME, Direction.INCOMING);
  86. for (Relationship r : iterable) {
  87. Node node = r.getStartNode();
  88. long id = node.getId();
  89. String name = (String)node.getProperty("name");
  90. // String idcard = (String)node.getProperty("idcard");
  91. // String tel = (String)node.getProperty("tel");
  92. System.out.println(id + " " + name+ " ");
  93. }
  94. tx.success();
  95. tx.close();
  96. }
  97. /**遍历查找**/
  98. public void searchUser(String username) {
  99. Transaction tx = graphDB.beginTx();
  100. Node startNode = graphDB.findNode(new CaseLabel("PERSON"), "name", username);
  101. // Node startNode = graphDB.findNode(new CaseLabel("PERSON"), "name", "B");
  102. TraversalDescription td = graphDB.traversalDescription();
  103. td.relationships(RelTypes.CRIME, Direction.OUTGOING);//沿着关系 TONG
  104. td.depthFirst();//设置深度优先
  105. // td.evaluator(Evaluators.excludeStartPosition());
  106. //从开始节点开始
  107. Iterable<Node> it = td.traverse(startNode).nodes();
  108. for (Node node : it) {
  109. long id = node.getId();
  110. if( node.hasLabel( new CaseLabel("PERSON") )) {
  111. String name = (String)node.getProperty("name");
  112. String idcard = (String)node.getProperty("idcard");
  113. String tel = (String)node.getProperty("tel");
  114. System.out.println(id + " " + name+ " " + idcard + " " + tel);
  115. } else {
  116. // String name = (String)node.getProperty("name");
  117. // String address = (String)node.getProperty("address");
  118. // System.out.println(id + " " + name+ " " + address);
  119. }
  120. }
  121. tx.success();
  122. tx.close();
  123. }
  124. /**查询所有案件信息*/
  125. public void searchAllCase() {
  126. Transaction tx = graphDB.beginTx();
  127. ResourceIterator<Node> iterator = graphDB.findNodes(new CaseLabel("CASEINFO"));
  128. for (ResourceIterator<Node> it = iterator; it.hasNext(); ) {
  129. Node node = it.next();
  130. long id = node.getId();
  131. String name = (String)node.getProperty("name");
  132. String address = (String)node.getProperty("address");
  133. System.out.println(id + " " + name+ " " + address);
  134. }
  135. tx.success();
  136. tx.close();
  137. }
  138. /**查询所有作案人员信息*/
  139. public void searchAllPerson() {
  140. Transaction tx = graphDB.beginTx();
  141. ResourceIterator<Node> iterator = graphDB.findNodes(new CaseLabel("PERSON"));
  142. for (ResourceIterator<Node> it = iterator; it.hasNext(); ) {
  143. Node node = it.next();
  144. long id = node.getId();
  145. String name = (String)node.getProperty("name");
  146. String idcard = (String)node.getProperty("idcard");
  147. String tel = (String)node.getProperty("tel");
  148. System.out.println(id + " " + name+ " " + idcard + " " + tel);
  149. }
  150. tx.success();
  151. tx.close();
  152. }
  153. public static void main(String[] args) {
  154. CaseInfoAs as = new CaseInfoAs();
  155. as.init();
  156. // as.create();
  157. as.search("A");
  158. // as.search1();
  159. // as.searchAllCase();
  160. // as.searchUser("A");
  161. }
  162. }
  163. class CaseLabel implements Label {
  164. private String name;
  165. public CaseLabel(String name) {
  166. this.name = name;
  167. }
  168. public String name(){
  169. return name;
  170. }
  171. }

生成的图

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/672700
推荐阅读
相关标签
  

闽ICP备14008679号