Search code examples
gogremlinazure-cosmosdb-gremlinapi

Golang cosmosdb-gremlin connection


Anyone has 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{accessKey}},
            Username: "",
            Password: accessKey,
        }
    })
    if err != nil {
        fmt.Println(err)
        return nil
    }
    return client
}

below is the error I am getting:

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.

Update:

I modified the code like below and gives different error:

package main

import (

    "crypto/tls"

    "fmt"

    gremlin "github.com/apache/tinkerpop/gremlin-go/v3/driver"

    "net/http"

    "time"

)

func createGremlinClient() *gremlin.DriverRemoteConnection {

    endpoint := "<endpoint>"

    accessKey := "<primary-key>"

    client, err := gremlin.NewDriverRemoteConnection(endpoint, func(settings *gremlin.DriverRemoteConnectionSettings) {

        settings.ConnectionTimeout = 45 * time.Second

        settings.ReadBufferSize = 4096

        settings.WriteBufferSize = 4096

        settings.TlsConfig = &tls.Config{InsecureSkipVerify: true}

        settings.AuthInfo = &gremlin.AuthInfo{

            Header:   http.Header{"Authorization": []string{accessKey}},

            Username: "/dbs/g",

            Password: accessKey,

        }

    })

    if err != nil {

        fmt.Println("Error creating Gremlin client:", err)

        return nil

    }

    return client

}

func main() {

    client := createGremlinClient()

    if client == nil {

        fmt.Println("Gremlin client creation failed.")

        return

    }

    g := gremlin.Traversal_().WithRemote(client)

    query := g.AddV().Iterate()

    count, _ := g.V().Count().Next()

    err := <-query

    if err != nil {

        fmt.Println(err)

        return

    }

    fmt.Println("Vertex count:", *count)

    defer client.Close()

}

Error I get is:

2024/04/19 10:52:19 Connecting.

2024/04/19 10:52:19 Read loop error 'websocket: close 1011 (internal server error): Internal-Server-Error', closing read loop.

2024/04/19 10:52:19 Read loop error 'websocket: close 1011 (internal server error): Internal-Server-Error', closing read loop.

2024/04/19 10:52:19 Connection error callback invoked, closing protocol.

websocket: close 1011 (internal server error): Internal-Server-Error

I think for Cosmos-Gremlin GraphDB the collection/containers dont apply, do the username I am passing may not make sense, I tried passing an empty string as well with no luck.


Solution

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