|
| 1 | +<p align="center"> |
| 2 | + <img src="assets/img/grammes-gopher-v4.png" alt="Grammes" title="Grammes" width="50%"> |
| 3 | +</p> |
| 4 | + |
| 5 | +# Grammes |
| 6 | + |
| 7 | +[](https://travis-ci.com/northwesternmutual/grammes) [](https://coveralls.io/github/northwesternmutual/grammes?branch=master) [](https://godoc.org/github.com/northwesternmutual/grammes) [](https://goreportcard.com/report/github.com/northwesternmutual/grammes) |
| 8 | + |
| 9 | +Grammes is an API/Wrapper for Gremlin and Janusgraph. It's written purely in Golang and allows for easy use of Gremlin without touching the Gremlin terminal. |
| 10 | + |
| 11 | +## Table of Contents |
| 12 | + |
| 13 | +- [Grammes](#grammes) |
| 14 | + - [Table of Contents](#table-of-contents) |
| 15 | + - [Local Setup](#local-setup) |
| 16 | + - [Cloning Grammes](#cloning-grammes) |
| 17 | + - [Setting up JanusGraph](#setting-up-janusgraph) |
| 18 | + - [Using Grammes](#using-grammes) |
| 19 | + - [Testing Grammes](#testing-grammes) |
| 20 | + - [Additional Resources](#additional-resources) |
| 21 | + - [Documentation on Gremlin](#documentation-on-gremlin) |
| 22 | + - [Examples](#examples) |
| 23 | + - [Troubleshooting](#troubleshooting) |
| 24 | + - [Fixing time outs when starting Janusgraph](#fixing-time-outs-when-starting-janusgraph) |
| 25 | + |
| 26 | +## Local Setup |
| 27 | + |
| 28 | +You need to setup all of the following tools to run the service locally |
| 29 | + |
| 30 | +- Go 1.12 |
| 31 | +- Git |
| 32 | +- Elastic Search |
| 33 | +- Cassandra |
| 34 | + - Java 8 |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +### Cloning Grammes |
| 39 | + |
| 40 | +Begin by opening up a terminal or command prompt and clone the grammes repository. |
| 41 | + |
| 42 | +```sh |
| 43 | +go get -u github.com/northwesternmutual/grammes |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### Setting up JanusGraph |
| 49 | + |
| 50 | +*if you have decided to use another graph database then you may move on to [project setup](#using-grammes)* |
| 51 | + |
| 52 | +First off, direct your terminal to the Grammes' `scripts` directory. |
| 53 | + |
| 54 | +```sh |
| 55 | +cd $GOPATH/src/github.com/northwesternmutual/grammes/scripts |
| 56 | +``` |
| 57 | + |
| 58 | +In here you can find the `gremlin.sh` and `janusgraph.sh` scripts. To set up JanusGraph just run the `janusgraph.sh` script. |
| 59 | + |
| 60 | +```sh |
| 61 | +./janusgraph.sh |
| 62 | +``` |
| 63 | + |
| 64 | +This should download and/or begin the graph and TinkerPop server. |
| 65 | + |
| 66 | +To make sure that everything is running try running `gremlin.sh`. |
| 67 | + |
| 68 | +```sh |
| 69 | +$ ./gremlin.sh |
| 70 | +SLF4J: Class path contains multiple SLF4J bindings. |
| 71 | +SLF4J: Found binding in [jar:file:/Users/<username>/projects/nm/gocode/src/github.com/northwesternmutual/grammes/bin/janusgraph-0.3.1-hadoop2/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] |
| 72 | +SLF4J: Found binding in [jar:file:/Users/<username>/projects/nm/gocode/src/github.com/northwesternmutual/grammes/bin/janusgraph-0.3.1-hadoop2/lib/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] |
| 73 | +SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. |
| 74 | +SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] |
| 75 | +15:05:59 WARN org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable |
| 76 | +gremlin> |
| 77 | +``` |
| 78 | + |
| 79 | +Once Gremlin starts then you may begin by running this command. |
| 80 | + |
| 81 | +```sh |
| 82 | +gremlin> :remote connect tinkerpop.server conf/remote.yaml |
| 83 | +===>Configured localhost/127.0.0.1:8182 |
| 84 | +``` |
| 85 | + |
| 86 | +If you see the message that Gremlin was configured to the localhost then quit Gremlin. |
| 87 | + |
| 88 | +```sh |
| 89 | +gremlin> :exit |
| 90 | +``` |
| 91 | + |
| 92 | +Finally, run the `janusgraph.sh` script again, but this time with the `status` flag. |
| 93 | + |
| 94 | +```sh |
| 95 | +./janusgraph.sh status |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### Using Grammes |
| 101 | + |
| 102 | +Once you have cloned the repository then you may begin implementing it into a project. Let's begin with creating a place for your code in the `$GOPATH`, i.e., |
| 103 | + |
| 104 | +```sh |
| 105 | +$GOPATH/src/github.com/<username-here>/<project-name-here> |
| 106 | +``` |
| 107 | + |
| 108 | +Next, you'll want to create a `main.go` file. For this example I will be using [MS Code](https://code.visualstudio.com/download), but you may use any editor you prefer. |
| 109 | + |
| 110 | +```sh |
| 111 | +code main.go |
| 112 | +``` |
| 113 | + |
| 114 | +In this file we can begin by making it a typical empty `main.go` file like this: |
| 115 | + |
| 116 | +``` go |
| 117 | +package main |
| 118 | + |
| 119 | +func main() { |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Next, import the grammes package and begin using it by connecting your client to a gremlin server. |
| 124 | + |
| 125 | +``` go |
| 126 | +package main |
| 127 | + |
| 128 | +import ( |
| 129 | + "log" |
| 130 | + |
| 131 | + "github.com/northwesternmutual/grammes" |
| 132 | +) |
| 133 | + |
| 134 | +func main() { |
| 135 | + // Creates a new client with the localhost IP. |
| 136 | + client, err := grammes.DialWithWebSocket("ws://127.0.0.1:8182") |
| 137 | + if err != nil { |
| 138 | + log.Fatalf("Error while creating client: %s\n", err.Error()) |
| 139 | + } |
| 140 | + |
| 141 | + // Executing a basic query to assure that the client is working. |
| 142 | + res, err := client.ExecuteStringQuery("1+3") |
| 143 | + if err != nil { |
| 144 | + log.Fatalf("Querying error: %s\n", err.Error()) |
| 145 | + } |
| 146 | + |
| 147 | + // Print out the result as a string |
| 148 | + log.Println(string(res)) |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +Once the client is created then you can begin querying the gremlin server via the `.ExecuteQuery` method in the client. To use this function you must put in a `Query` which is an interface for any kind of Query-able type in the package. These include: `graph.String` and `traversal.String`. For an example of querying the gremlin server for all of the Vertex labels: |
| 153 | + |
| 154 | +``` go |
| 155 | +package main |
| 156 | + |
| 157 | +import ( |
| 158 | + "log" |
| 159 | + |
| 160 | + "github.com/northwesternmutual/grammes" |
| 161 | +) |
| 162 | + |
| 163 | +func main() { |
| 164 | + // Creates a new client with the localhost IP. |
| 165 | + client, err := grammes.DialWithWebSocket("ws://127.0.0.1:8182") |
| 166 | + if err != nil { |
| 167 | + log.Fatalf("Error while creating client: %s\n", err.Error()) |
| 168 | + } |
| 169 | + |
| 170 | + // Executing a query to add a vertex to the graph. |
| 171 | + client.AddVertex("testingvertex") |
| 172 | + |
| 173 | + // Create a new traversal string to build your traverser. |
| 174 | + g := grammes.Traversal() |
| 175 | + |
| 176 | + // Executing a query to fetch all of the labels from the vertices. |
| 177 | + data, err := client.ExecuteQuery(g.V().Label()) |
| 178 | + if err != nil { |
| 179 | + log.Fatalf("Querying error: %s\n", err.Error()) |
| 180 | + } |
| 181 | + |
| 182 | + // Log out the response. |
| 183 | + log.Println(string(data)) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +After this is all written you may run this file by saving it and hopping back on to your terminal. After starting your Gremlin Server and graph database in the terminal let's run this command to run the file: |
| 188 | + |
| 189 | +```sh |
| 190 | +go run main.go |
| 191 | +``` |
| 192 | + |
| 193 | +For more examples look in the `examples/` directory of the project. In there you'll find multiple examples on how to use the Grammes package. |
| 194 | + |
| 195 | +## Testing Grammes |
| 196 | + |
| 197 | +Grammes uses [goconvey](https://www.github.com/smartystreets/goconvey/) by [smartystreets](https://www.github.com/smartystreets/) for its tests. Before trying to run the unit tests in Grammes you should update your version of this repository using this command. |
| 198 | + |
| 199 | +```sh |
| 200 | +go get -u github.com/smartystreets/goconvey/convey |
| 201 | +``` |
| 202 | + |
| 203 | +Once you have this downloaded you may run the tests in Grammes how you normally would in Golang. |
| 204 | + |
| 205 | +```sh |
| 206 | +go test ./... |
| 207 | +``` |
| 208 | + |
| 209 | +## Additional Resources |
| 210 | + |
| 211 | +### Documentation on Gremlin |
| 212 | + |
| 213 | +To learn more about how to use Gremlin I highly recommend looking through their [Tinkerpop3 documentation](http://tinkerpop.apache.org/docs/current/reference/). It's full of examples and documentation on every traversal step available. |
| 214 | + |
| 215 | +### Examples |
| 216 | + |
| 217 | +To find examples look in the `examples/` directory of Grammes. In there you'll find plenty of examples related to how to use this package. Make sure you're running Janusgraph before you begin running the examples. |
| 218 | + |
| 219 | +## Troubleshooting |
| 220 | + |
| 221 | +### Fixing time outs when starting Janusgraph |
| 222 | + |
| 223 | +If Nodetool times out or any other part of the setup times out then the most common issue is that Cassandra is already running on your machine. To fix this run this command. |
| 224 | + |
| 225 | +``` bash |
| 226 | +# only for Unix at the moment. |
| 227 | +launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist |
| 228 | +``` |
0 commit comments