golang sample to connect to cosmosdb-gremlin

Ankur Gupta 0 Reputation points
2024-04-18T19:29:26.9366667+00:00

Hi, need a golang sample to connect to cosmosdb-gremlin over wss, preferably using Primary key or connection string? So far I have tried gremlingo, gramess etc but nothing resulted in successful connection. Thanks on advance for any pointers.

So far I have tried gremlingo, gramess etc but nothing resulted in successful connection.

Below is the code I tried for example:


func createGremlinClient() *gremlingo.DriverRemoteConnection{
    endpoint := "wss://<url-here>.cosmos.azure.com:443/" 
    accessKey := "my-primarykey-here"

    client, err := gremlingo.NewDriverRemoteConnection(endpoint,
        func(settings *gremlingo.DriverRemoteConnectionSettings) {
        settings.TraversalSource = "g"
        settings.NewConnectionThreshold = 4
        settings.EnableCompression = false
        settings.ReadBufferSize = 0
        settings.WriteBufferSize = 0
        settings.AuthInfo = &gremlingo.AuthInfo{Header:http.Header{"Authorization":[]string{"my-primarykey-here"}}, Username: "", Password: accessKey} //Authenticate using access key
    })
    if err != nil {
        fmt.Println(err)
        return nil
    }
    return client
}

2024/04/18 16:16:34 Failed to instantiate the new connection; setting connection state to closed.

2024/04/18 16:16:34 Error creating new connection for connection pool: websocket: bad handshake

2024/04/18 16:16:34 Error occurred during operation NewDriverRemoteConnection: 'E0104: no successful connections could be made: websocket: bad handshake'

After passing connection string instead of endpoint URL, I still get same error.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,446 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amira Bedhiafi 15,446 Reputation points
    2024-04-19T12:42:58.96+00:00

    From the message error, I undestood that you may need a valid authorization header that often includes generating a signature with the primary key :

    package main
    import (
    	"fmt"
    	"net/http"
    	"github.com/apache/tinkerpop/gremlingo"
    )
    func createGremlinClient() *gremlingo.Client {
        endpoint := "wss://your-account.gremlin.cosmos.azure.com:443/gremlin"
        primaryKey := "your-primary-key"
        settings := &gremlingo.ConnectionSettings{
            TraversalSource:    "g",
            Mime:               gremlingo.GraphSON3MimeType,
            WsPingInterval:     60, // Ping interval in seconds to keep the connection alive
            WsReadBufferSize:   1024,
            WsWriteBufferSize:  1024,
            MaxConcurrentUsage: 4,
            Authentication: gremlingo.AuthInfo{
                Header: http.Header{
                    "Authorization": []string{gremlingo.BuildGremlinAuthorizationHeader(gremlingo.AuthInfo{
                        Username: "/dbs/your-database/colls/your-collection",
                        Password: primaryKey,
                    })},
                },
            },
        }
        // Here you need to create a new Gremlin client
        client, err := gremlingo.NewClient(endpoint, settings)
        if err != nil {
            fmt.Println("Failed to create Gremlin client:", err)
            return nil
        }
        return client
    }
    func main() {
        client := createGremlinClient()
        if client == nil {
            fmt.Println("Failed to connect to Cosmos DB")
            return
        }
        fmt.Println("Connected to Cosmos DB with Gremlin successfully!")
    }
    
    

  2. Ankur Gupta 0 Reputation points
    2024-04-24T18:20:46.14+00:00

    gremlin-go doesnt seem to be compatible with Azure Cosmos db, I tried another driver named gremco and it worked.

    0 comments No comments