Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.8 KiB

  1. ## Graph Data Base Basics
  2. ## Gremlin Installation
  3. ## Gremlin Syntax
  4. #### Add a vertex
  5. ```gremlin
  6. g.addV('student').property('name', 'Jeffery').property('GPA', 4.0);
  7. ```
  8. #### Update a Property
  9. ```gremlin
  10. g.V(1).property('name', 'Jeffery R');
  11. ```
  12. #### Selection
  13. ```gremlin
  14. g.V().hasLabel('student').valueMap();
  15. ```
  16. ```gremlin
  17. g.V().hasLabel('student').values('name');
  18. ```
  19. ```gremlin
  20. g.V().hasLabel('student').order().by('gpa', decr).valueMap();
  21. ```
  22. #### Adding Edges
  23. ```gremlin
  24. g.V(0).as('a').V(1).as('b').addE('knows')
  25. .from('a').to('b');
  26. ```
  27. #### Traversing Graph
  28. ## Using Gremlin With Java
  29. ```maven
  30. <!-- https://mvnrepository.com/artifact/com.tinkerpop/gremlin-core -->
  31. <dependency>
  32. <groupId>com.tinkerpop</groupId>
  33. <artifactId>gremlin-core</artifactId>
  34. <version>3.0.0.M7</version>
  35. </dependency>
  36. <!-- https://mvnrepository.com/artifact/org.apache.tinkerpop/gremlin-driver -->
  37. <dependency>
  38. <groupId>org.apache.tinkerpop</groupId>
  39. <artifactId>gremlin-driver</artifactId>
  40. <version>3.3.3</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.apache.tinkerpop</groupId>
  44. <artifactId>tinkergraph-gremlin</artifactId>
  45. <version>3.3.3</version>
  46. </dependency>
  47. ```
  48. ```java
  49. public class GraphConnection
  50. {
  51. /** Stores/manages client connections **/
  52. private Cluster cluster;
  53. /** Connection to the graph db */
  54. private Client client;
  55. public RemoteConnection()
  56. {
  57. Cluster.Builder b = Cluster.build();
  58. b.addContactPoint("localhost");
  59. b.port(8182);
  60. this.cluster = b.create();
  61. this.client = cluster.connect();
  62. }
  63. public synchronized ResultSet queryGraph(String q)
  64. {
  65. return this.client.submit(q);
  66. }
  67. public void closeConnection()
  68. {
  69. this.cluster.close();
  70. }
  71. }
  72. ```