forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3] .Add added an example for connecting to cosmosdb
- Loading branch information
1 parent
d77873f
commit e0e6b62
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Examples | ||
|
||
The provided exampled needs a germlin-server as counterpart. They can be either a local one or a cosmos db running on azure. | ||
|
||
## Against Local Gremlin-Server | ||
|
||
```bash | ||
# start a local gremlin server | ||
cd .. | ||
make infra.up | ||
|
||
# execute the example | ||
go run ./examples/local | ||
``` | ||
|
||
## Against CosmosDB | ||
|
||
### Prerequisites | ||
|
||
1. Obtain the host/ endpoint of the CosmosDB | ||
|
||
- Therefore one has to login into the Azure web console and navigate to the azure cosmos DB panel. | ||
- Here just select the db you want to connect to, go to the overview tab and there copy the URL of the `Gremlin Endpoint` | ||
- It should look like this `wss://xyz-my-db-abc.gremlin.cosmos.azure.com:443/` | ||
|
||
2. Obtain the username/ database name | ||
|
||
- In our case the username is composed out of the name of the database and the name of the collection one want's to use | ||
- Schema: `/dbs/<db-name>/colls/<name of the collection>` | ||
- Example: `/dbs/mydatabase/colls/mygraph` | ||
|
||
3. Obtain the key of the CosmosDB | ||
|
||
- Therefore one has to login into the Azure web console and navigate to the azure cosmos DB panel. | ||
- Here just select the db you want to connect to, go to the keys tab and there copy the `PRIMARY KEY` | ||
- It should look like this `Amx5obCQPF53Vf7WyqtXiu5qsZ89tS8envY9oON3KNRIGAjILWduRKbfwqvYZ2e8vtIUNNv1w0LlEoecfhNsk9w==` | ||
|
||
4. Set these parameters as environment variables | ||
|
||
```bash | ||
export CDB_HOST=wss://xyz-my-db-abc.gremlin.cosmos.azure.com:443 && \ | ||
export CDB_KEY=Amx5obCQPF53Vf7WyqtXiu5qsZ89tS8envY9oON3KNRIGAjILWduRKbfwqvYZ2e8vtIUNNv1w0Ll EoecfhNsk9w== && \ | ||
export CDB_USERNAME=/dbs/mydatabase/colls/mygraph | ||
``` | ||
|
||
Then one can run the example via: | ||
|
||
```bash | ||
cd .. | ||
go run ./examples/cosmos | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
|
||
"github.com/schwartzmx/gremtune" | ||
) | ||
|
||
var panicOnErrorOnChannel = func(errs chan error) { | ||
err := <-errs | ||
if err == nil { | ||
return // ignore if the channel was closed | ||
} | ||
log.Fatalf("Lost connection to the database: %s", err) | ||
} | ||
|
||
func main() { | ||
|
||
host := os.Getenv("CDB_HOST") | ||
username := os.Getenv("CDB_USERNAME") | ||
password := os.Getenv("CDB_KEY") | ||
|
||
if len(host) == 0 { | ||
log.Fatal("Host not set. Use export CDB_HOST=<CosmosDB Gremlin Endpoint> to specify it") | ||
} | ||
|
||
if len(username) == 0 { | ||
log.Fatal("Username not set. Use export CDB_USERNAME=/dbs/<cosmosdb name>/colls/<graph name> to specify it") | ||
} | ||
|
||
if len(password) == 0 { | ||
log.Fatal("Key not set. Use export CDB_KEY=<key> to specify it") | ||
} | ||
|
||
log.Println("Connecting using:") | ||
log.Printf("\thost: %s\n", host) | ||
log.Printf("\tusername: %s\n", username) | ||
log.Printf("\tpassword: %s\n", password) | ||
|
||
errs := make(chan error) | ||
go panicOnErrorOnChannel(errs) | ||
|
||
dialer := gremtune.NewDialer(host, gremtune.SetAuthentication(username, password)) | ||
gremlinClient, err := gremtune.Dial(dialer, errs) // Returns a gremtune client to interact with | ||
if err != nil { | ||
log.Fatalf("Failed to create the gremlin client: %s", err) | ||
} | ||
|
||
// Sends a query to Gremlin Server | ||
res, err := gremlinClient.Execute("g.V()") | ||
if err != nil { | ||
log.Fatalf("Failed to execute a gremlin command: %s", err) | ||
} | ||
|
||
jsonEncodedResponse, err := json.MarshalIndent(res[0].Result.Data, "", " ") | ||
if err != nil { | ||
log.Fatalf("Failed to encode the raw json into json: %s", err) | ||
} | ||
|
||
log.Printf("Received data: \n%s\n", jsonEncodedResponse) | ||
} |
File renamed without changes.