Home > database >  Getting error when inserting a row on Amazon Keyspaces using the GoCQL driver
Getting error when inserting a row on Amazon Keyspaces using the GoCQL driver

Time:09-13

I created a keyspace in eu-west-3.

When I try with the same query in cqlsh it works but with golang doesn't. Someone can help me?

    cluster := gocql.NewCluster("cassandra.eu-west-3.amazonaws.com:9142")
    cluster.ConnectTimeout = time.Second * 10
    var auth sigv4.AwsAuthenticator = sigv4.NewAwsAuthenticator()
    auth.Region = "eu-west-3"
    auth.AccessKeyId = "ex"
    auth.SecretAccessKey = "ex"

    cluster.Authenticator = auth

    cluster.SslOpts = &gocql.SslOptions{
        CaPath:                 "./sf-class2-root.crt",
        EnableHostVerification: false,
    }
    cluster.Consistency = gocql.LocalQuorum
    cluster.DisableInitialHostLookup = true

    session, err := cluster.CreateSession()
    if err != nil {
        fmt.Println("err>", err)
        return
    }

    session.Query("INSERT INTO ex.accounts (id, username, email) VALUES (uuid(),'user1','[email protected]' ) ;")

CodePudding user response:

A quick glance over your code shows that your query contains a semi-colon (;) at the end. It should just be:

    session.Query("INSERT INTO ex.accounts (id, username, email) \
        VALUES (uuid(),'user1','[email protected]' )")

However, I don't know if that's the problem because you haven't provided sufficient detail in your question.

The general guidance is that you (a) provide a good summary of the problem that includes software/component versions, the full error message full stack trace; (b) describe what you've tried to fix the problem, details of investigation you've done; and (c) minimal sample code that replicates the problem. Cheers!

CodePudding user response:

I solved this problem using a batch. It also permits to add up to 30 rows of data. For example this code adds 100 rows with id from 0 to 99

    batch := session.NewBatch(gocql.UnloggedBatch)
    stmt := `INSERT INTO example.ex (id) VALUES (?)`

    for i := 0; i < 100; i   {
        batch.Query(stmt, strconv.Itoa(i))

        if i0 == 0 {
            err = session.ExecuteBatch(batch)
            if err != nil {
                log.Panic(err)
            }
            batch = session.NewBatch(gocql.UnloggedBatch)
        }
    }
    err = session.ExecuteBatch(batch)
    if err != nil {
        log.Panic(err)
    }
  • Related