Unable to connect to Cosmos DB through Cassandra API(gocql)

Velvaluri, Vivek 41 Reputation points
2020-06-17T12:16:19.217+00:00

I have provisioned a Cosmos DB (Cassandra API) instance on Azure.
I'm able to connect to it through cqlsh. But, getting different errors when trying to connect to it through gocql.

The following code generates this error:

gocql: unable to dial control conn 40.112.241.29: read tcp 192.168.0.106:58864->40.112.241.29:10350: read: connection reset by peer

Here's the code snippet:

cluster := gocql.NewCluster("<host here>")  
cluster.Port = 10350  
cluster.Timeout = 10000  
cluster.SocketKeepalive = 60 * time.Second  
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}  
cluster.Consistency = gocql.LocalOne  
cluster.ProtoVersion = 1  
cluster.ReconnectionPolicy = &gocql.ConstantReconnectionPolicy{MaxRetries: 10, Interval: 8 * time.Second}  
cluster.Keyspace = "<keyspace here>"  
cluster.DisableInitialHostLookup = true  
cluster.IgnorePeerAddr = true  
cluster.Authenticator = gocql.PasswordAuthenticator{Username: "<username here>", Password: "<password here>"}  
  
_, err := cluster.CreateSession()  
if err != nil{  
// handle the error  
}  

Just to add to this, gocql is listed as an officially supported driver under this page:

https://video2.skills-academy.com/en-us/azure/cosmos-db/cassandra-support#cassandra-driver

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

Accepted answer
  1. Theo van Kraay 81 Reputation points Microsoft Employee
    2020-06-18T13:24:53.09+00:00

    Hello. One aspect missing is SSL. You either need to enable client-side SSL server cert verification, or set sslOptions.EnableHostVerification to false. Also, ensure the values you are passing for the Cosmos DB Cassandra API account are correct. Here is a snippet which hopefully works better for you:

    // connect to the cluster
    cluster := gocql.NewCluster("<ACCOUNTNAME>.cassandra.cosmos.azure.com")
    cluster.Port = 10350
    var sslOptions = new(gocql.SslOptions)
    
    // If you want to enable client-side SSL server cert verification do this:
    // sslOptions.EnableHostVerification = true
    // sslOptions.CaPath = "<path/to/cert.pem>"
    // sslOptions.Config = &tls.Config{}
    // sslOptions.ServerName = `ACCOUNTNAME.cassandra.cosmos.azure.com`
    // cluster.SslOpts = sslOptions
    
    //If you do NOT want to enable client-side SSL, set sslOptions.EnableHostVerification to false and ignore the other options.
    sslOptions.EnableHostVerification = false
    cluster.SslOpts = sslOptions
    
    cluster.ProtoVersion = 4
    cluster.Authenticator = gocql.PasswordAuthenticator{Username: "<ACCOUNTNAME>", Password: "<PASSWORD>"}
    session, _ := cluster.CreateSession()
    defer session.Close()
    
    var pk int32
    var ck int32
    var col1 string
    var col2 int64
    
    if err := session.Query(`SELECT * FROM kspc1.tbl1;`).Scan(&pk, &ck, &col1, &col2); err != nil {
        log.Fatal(err)
    }
    fmt.Println("Row:", pk, ck, col1, col2)
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful