Updated on 2023-11-21 GMT+08:00

Connecting to an Instance Using Go

This section describes how to connect to a GeminiDB Cassandra instance using Go.

Prerequisites

  • A GeminiDB Cassandra instance has been created and is running normally. For details about how to create a GeminiDB Cassandra instance, see Buying an Instance.
  • For details about how to create an ECS, see "Getting Started > Purchasing an ECS" in the Elastic Cloud Server User Guide.
  • You have installed the Go environment on the ECS. If not, download the Go installation package.

Procedure

  1. Obtain the private IP address and port number of the GeminiDB Cassandra instance.

    For details about how to obtain the private IP address and port number, see Viewing the IP Address and Port Number.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Edit the code for connecting to the GeminiDB Cassandra instance.

    // Default LoadBalancingPolicy RoundRobinHostPolicy
    cluster := gocql.NewCluster("127.0.0.1,127.0.0.2,127.0.0.3")
    cluster.Authenticator = gocql.PasswordAuthenticator{
    		Username: "user",
    		Password: "password"
     }
    cluster.Keyspace = "ks1"
    // connect to the cluster
    session, err := cluster.CreateSession()
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()

  4. Run sample code to check whether the result is normal.

Executing Write and Read Operations

Create a session query. Query parameters cannot be used in other statements and cannot be modified after the query starts.

Use Query.Exec if you need to read the query results after a query is executed:

 err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`,
        "me", gocql.TimeUUID(), "hello world").WithContext(ctx).Exec()

Use Query.Scan if you want to read one line of data:

 err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ? LIMIT 1`,
        "me").WithContext(ctx).Consistency(gocql.One).Scan(&id, &text)

Use Iter.Scanner if you want to read multiple lines of data:

scanner := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`,
    "me").WithContext(ctx).Iter().Scanner()
 for scanner.Next() {
    var (
        id gocql.UUID
        text string
    )
    err = scanner.Scan(&id, &text)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Tweet:", id, text)
 }
 // scanner.Err() closes the iterator, so scanner nor iter should be used afterwards.
 if err := scanner.Err(); err != nil {
    log.Fatal(err)
 }

Executing Multiple Queries Concurrently

It is safe to share a session in multiple goroutines. If necessary, you execute multiple queries using multiple goroutines.

results := make(chan error, 2)
go func() {
    results <- session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`,
        "me", gocql.TimeUUID(), "hello world 1").Exec()
}()
go func() {
    results <- session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`,
        "me", gocql.TimeUUID(), "hello world 2").Exec()
}()