Use Go to query data
Use the InfluxDB influxdb3-go
Go client library package and SQL or InfluxQL to query data stored in InfluxDB.
Execute queries and retrieve data over the Flight+gRPC protocol, and then process data using common Go tools.
Get started using Go to query InfluxDB
The following example shows how to use Go with the influxdb3-go
module to create a client and query an InfluxDB Cloud Dedicated database.
Install Go
Follow the Go download and installation instructions to install a recent version of the Go programming language for your system.
Create a Go module directory
Inside of your project directory, create a new module directory and navigate into it.
mkdir influxdb_go_client && cd $_
Enter the following command to initialize a new Go module:
go mod init influxdb_go_client
Install dependencies
In your terminal, enter the following command to download and install the client library:
go get github.com/InfluxCommunity/influxdb3-go
influxdb3-go
*: Provides theinfluxdb3
package and also installs the Apachearrow
module for working with Arrow data returned from queries.
With the dependencies installed, you’re ready to query and analyze data stored in an InfluxDB database.
Execute a query
The following examples show how to create an InfluxDB client, use client query methods to select all fields in a measurement, and then access query result data and metadata.
In your influxdb_go_client
module directory, create a file named query.go
and enter one of the following samples to query using SQL or InfluxQL.
Replace the following configuration values in the sample code:
DATABASE_NAME
: the name of the InfluxDB database to queryDATABASE_TOKEN
: an InfluxDB database token with read permission on the specified database
The sample code does the following:
- Defines a
main
package for your module and imports packages you’ll use in your code. - Defines a
Query()
function. - Instantiates the
influxdb3
client with InfluxDB credentials and assigns it to aclient
variable. - Defines a deferred function that closes the client when
Query()
has finished executing. - Defines an SQL query to execute.
- Calls the client’s
Query(ctx context.Context, query string)
method and passes the SQL string as thequery
argument.Query()
returns the following:*influxdb3.QueryIterator
: A custom iterator for reading data from the query result stream.error
: A Flight request error.
The sample code does the following:
Defines a
main
package for your module and imports packages you’ll use in your code.Defines a
Query()
function.Instantiates the
influxdb3
client with InfluxDB credentials and assigns it to aclient
variable.Defines a deferred function that closes the client when
Query()
has finished executing.Defines an InfluxQL query to execute.
Calls the following client method:
QueryWithOptions(ctx context.Context, options *QueryOptions, query string)
and passes the following arguments:
- options: A
QueryOptions
struct with theQueryType
property set toinfluxdb3.InfluxQL
. - query: A string. The SQL or InfluxQL query to execute.
QueryWithOptions
returns the following:*influxdb3.QueryIterator
: A custom iterator that provides access to query result data and metadata.error
: A Flight request error.
- options: A
Run the example
In your
influxdb_go_client
module directory, create a file namedmain.go
.In
main.go
, enter the following sample code to define amain()
executable function that calls theQuery()
function:package main func main() { Query() }
In your terminal, enter the following command to install the necessary packages, build the module, and run the program:
go build && go run influxdb_go_client
The program executes the
main()
function that writes the data and prints the query results to the console.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.