【问题标题】:Getting error when inserting a row on Amazon Keyspaces using the GoCQL driver使用 GoCQL 驱动程序在 Amazon Keyspaces 上插入行时出错
【发布时间】:2022-11-18 06:04:51
【问题描述】:

我在 eu-west-3 中创建了一个键空间。

当我在 cqlsh 中尝试使用相同的查询时,它有效但 golang 无效。 有人可以帮助我吗?

    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' ) ;")

【问题讨论】:

    标签: go cassandra amazon-keyspaces gocql


    【解决方案1】:

    快速浏览一下您的代码会发现您的查询末尾包含一个分号 (;)。它应该只是:

        session.Query("INSERT INTO ex.accounts (id, username, email) 
            VALUES (uuid(),'user1','user1@gmail.com' )")
    

    但是,我不知道这是否是问题所在,因为您没有在问题中提供足够的细节。

    一般指导是您 (a) 提供一个很好的问题总结,包括软件/组件版本、完整的错误消息 + 完整的堆栈跟踪; (b) 描述您为解决问题所做的努力,您所做调查的详细信息; (c) 复制问题的最小示例代码。干杯!

    【讨论】:

    • 我在自己的计算机上使用最新版本的 golang 和 AWS 的键空间,如 db,通用查询的结果是:[query statement="INSERT INTO example.ex (id) VALUES ('2')" values=[] consistency=LOCAL_QUORUM],当我尝试读取数据时查询成功。我重复一遍,cqlsh(在 AWS keyspaces 站点)中的相同查询也可以正常工作。当我尝试插入数据时,我在终端中没有看到任何错误。我考虑过防火墙,但 wireshark 显示数据已发送。
    【解决方案2】:

    我使用批处理解决了这个问题。它还允许添加最多 30 行数据。 例如,这段代码添加 100 行,id 从 0 到 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)
        }
    

    【讨论】:

      【解决方案3】:

      您可以使用 Golang 连接并写入 Amazon Keyspaces。以下是如何连接的示例。如需更多示例,请访问我们的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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 2022-08-22
        • 2018-03-24
        • 2021-11-11
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2021-10-14
        • 1970-01-01
        相关资源
        最近更新 更多