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 Purchasing an ECS in Getting Started with Elastic Cloud Server.
- You have installed the Go environment on the ECS. If not, download the Go installation package.
Procedure
- 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.
- Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
- Edit the code for connecting to the GeminiDB Cassandra instance.
import ( "os" ) // Default LoadBalancingPolicy RoundRobinHostPolicy cluster := gocql.NewCluster("127.0.0.1,127.0.0.2,127.0.0.3") // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. username = os.Getenv("EXAMPLE_USERNAME_ENV"), password = os.Getenv("EXAMPLE_PASSWORD_ENV"), cluster.Authenticator = gocql.PasswordAuthenticator{ Username: username, Password: password } cluster.Keyspace = "ks1" // connect to the cluster session, err := cluster.CreateSession() if err != nil { log.Fatal(err) } defer session.Close()
- 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() }()
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.