简体   繁体   中英

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

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','user1@gmail.com' ) ;")

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','user1@gmail.com' )")

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!

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 i%30 == 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)
    }

You can connect and write to Amazon Keyspaces using Golang. Here is an example of how to connect. For more examples visit our Keyspaces examples page.

func main() {

    //Determine Contact Point
    contactPoint := fmt.Sprintf("cassandra.%s.amazonaws.com", awsRegion)
    fmt.Println("Using Contact Point ", contactPoint)

    // Configure Cluster
    cluster := gocql.NewCluster(contactPoint)

    cluster.Port = 9142

    cluster.NumConns = 4

    awsAuth := sigv4.NewAwsAuthenticator()
    cluster.Authenticator = awsAuth

    //Retry Policy
    amazonKeyspacesRetry := &AmazonKeyspacesExponentialBackoffRetryPolicy{Max: 100 * time.Millisecond , Min: 10 * time.Millisecond, NumRetries: 20}
    cluster.RetryPolicy = amazonKeyspacesRetry

    amazonKeyspacesConnectionObserver, _ := NewAmazonKeyspacesObserver()
    cluster.ConnectObserver = amazonKeyspacesConnectionObserver

    // Configure Connection TrustStore for TLS
    cluster.SslOpts = &gocql.SslOptions{
        CaPath: "certs/sf-class2-root.crt",
        EnableHostVerification: false,
    }

    cluster.Consistency = gocql.LocalQuorum
    cluster.DisableInitialHostLookup = false

    cassandraSession, err := cluster.CreateSession()

    if err != nil {
        fmt.Println("Cassandra Session Creation Error - ", err)
        os.Exit(-2)
    }

    defer cassandraSession.Close()

    // Perform Query
    var keyspaceName string
    iter := cassandraSession.Query("SELECT keyspace_name FROM system_schema.keyspaces;").Iter()

    defer iter.Close()

    for iter.Scan(&keyspaceName) {
        fmt.Println("keyspace_name : ", keyspaceName)
    }
    if err := iter.Close(); err != nil {
        log.Fatal(err)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM