Prerequisites
- Get a bearer token
- Create a keyspace
- Create a table in your keyspace(optional): REST
Add dependencies
To begin, add the required dependency to your project:
The next sections explain the parts of a script to use the Stargate functionality.A full working script is included below.
Authentication
This example assumes that you're running Stargate on Astra DB.For more information regarding authentication please see theManage application tokens section.
The following code snippet can be used to securely connect to Astra DB:
Set up client
To connect to your Stargate instance, set up the client as follows.
A simple query can be performed by passing a CQL query to the client using the`ExecuteQuery()` function for standard query execution:
Data definition (DDL) queries are supported in the same manner:
In general, users will create a keyspace and table first.
Parameterized queries are also supported:
The ExecuteQuery function can be used to execute a single query.
If you need to group several commands together as abatch statement,the client also provides an `ExecuteBatch()` function to execute a batch query:
This example inserts two values into the keyspace table test.users .Only INSERT , UPDATE , and DELETE operations can be used in a batch query.
After executing a query, a response returns rows that match the SELECT statement.You can call `getResultSet() on the response to grab a ResultSet to process.Note the function may return undefined if no ResultSet` is returned; check if it is defined or cast.
Since the result type is known, the `ToString` function transforms the value into a native string.Additional functions also exist for other types such as `int`, `map`, and `blob`.The full list can be found invalues.go.
To put all the pieces together, here is a sample script that combines all the piecesshown above:
Getting started
Clone the repo, then install dependencies:
- 1
Testing
The tests for this project can be run from the root using the following command:
The addition of -tags integration also runs the integration tests.
Generating gRPC code stubs
To update the protobuf files being used, add the new files to the top level protodirectory and then run make proto from the root of the project.After running, you will find the new generated ``*.pb.go files in stargate/pkg/proto`.
Coding style
This project uses golangci-lint to lint code.These standards are enforced automatically in the CI pipeline.
The Stargate gRPC Go Client repository is located athttps://github.com/stargate/stargate-grpc-go-client.