northwesternmutual / grammes

A Go package built to communicate with Apache TinkerPop™ Graph computing framework using Gremlin; a graph traversal language used by graph databases such as JanusGraph®, MS Cosmos DB, AWS Neptune, and DataStax® Enterprise Graph.
Apache License 2.0
125 stars 45 forks source link

CosmosDB support? #42

Open Kubera2017 opened 3 years ago

Kubera2017 commented 3 years ago

Hello, does this library support CosmosDB?

I tried, actually it needs GraphSONSerializersV2d0 instead of V3, I used grammes.WithGremlinVersion(2) for that, vertices are added, but retreival queries don't work, it returns empty labels and IDs. This is test code:

package main

import (
    "fmt"
    "log"

    "github.com/northwesternmutual/grammes"
)

func main() {
    cosmosdb_account := "g123"
    cosmosdb_password := "J7q==" // Primary key from Keys
    cosmosdb_database := "db"    // In Data Explorer, need to create yours
    cosmosdb_graph := "test" // In Data Explorer, need to create yours

    cosmosdb_partition_key := "partition_key" // the same as you set when creating the graph in Data Explorer
    cosmosdb_partition_value := "1"

    host := "wss://" + cosmosdb_account + ".gremlin.cosmosdb.azure.com:443/"
    username := "/dbs/" + cosmosdb_database + "/colls/" + cosmosdb_graph
    password := cosmosdb_password

    client, err := grammes.DialWithWebSocket(host,
        grammes.WithAuthUserPass(username, password),
        grammes.WithGremlinVersion(2),
    )
    if err != nil {
        log.Fatal(err.Error())
    }

    _, err = client.AddVertex("test", cosmosdb_partition_key, cosmosdb_partition_value)
    if err != nil {
        log.Fatal("failed to add vertex", err.Error())
    }

    g := grammes.Traversal()
    vertices, err := client.VerticesByQuery(g.V())
    if err != nil {
        log.Fatal("Couldn't gather vertices", err.Error())
    }

    for _, vertex := range vertices {
        fmt.Println(vertex.Label(), vertex.ID())
    }

}
zmmille2 commented 3 years ago

Thanks for pointing out the v2 here, saved me some work.

I wasn't able to get your code to work exactly, but what's the VerticiesByQuery method exactly? Do you mean to just run ExecuteQuery here?

    g := grammes.Traversal()
    vertices, err := client.ExecuteQuery(g.V())
    if err != nil {
        log.Fatal("Couldn't gather vertices", err.Error())
    }

    for _, vertex := range vertices {
        fmt.Println(string(vertex))
    }

seems to work just fine.

Kubera2017 commented 3 years ago

That's good that it works, but VerticesByQuery returns array of vertices already []model.Vertex, but ExecuteQuery returns raw result. This means you will unable to use the library API. https://github.com/northwesternmutual/grammes/blob/a0883ed30fe8f10f3c58ed9ff80849c029cf92c9/manager/getvertex.go#L79 https://github.com/northwesternmutual/grammes/blob/a0883ed30fe8f10f3c58ed9ff80849c029cf92c9/manager/query.go#L54