DEV Community

Thulasiraj Komminar
Thulasiraj Komminar

Posted on • Originally published at thulasirajkomminar.Medium on

How to Write Data to InfluxDB v3 with GoLang: A Step-by-Step Tutorial

Introduction

In this blog post, we’ll delve into the process of writing data into InfluxDB v3 using Go. Our focus will be on harnessing the capabilities of the influxdb3-go client library, particularly its support for annotated structs. Through a practical example, we’ll demonstrate how to convert a slice into a structured format and efficiently batch write it into InfluxDB.

Setting Up a Go Project for IIoT Measurements: A Step-by-Step Guide

Let’s kickstart by initializing a Go project named iiot-measurements and structuring it with three essential files:

  1. main.go: Responsible for the main function.
  2. influxdb3.go: Contains the struct and functions for InfluxDB writing.
  3. config.go: Handles the loading of InfluxDB credentials from the environment.

Here’s how to set up the project:

mkdir iiot-measurements && cd iiot-measurements && go mod init iiot-measurements
Enter fullscreen mode Exit fullscreen mode

Next, let’s install the latest influxdb3 Go client:

go get github.com/InfluxCommunity/influxdb3-go
Enter fullscreen mode Exit fullscreen mode

Implementing Annotated Structs for InfluxDB Data Writing

With the influxdb3-go client, data can be provided in various formats such as line protocol, point, or as a struct. For this tutorial, we’ll opt for the annotated struct approach to ensure a fixed schema. This choice suits scenarios where maintaining a consistent schema is crucial. However, if your use case involves handling dynamic schemas, other options like line protocol or point may be more suitable.

Loading InfluxDB Credentials from Environment Variables

Before instantiating the InfluxDB client, it’s essential to load the credentials from the environment. To achieve this, we’ll utilize the env package to parse environment variables into our InfluxdbConfig struct. Additionally, we’ll create a NewConfig() function to facilitate the instantiation of the struct.

go get github.com/caarlos0/env/v11
Enter fullscreen mode Exit fullscreen mode

Instantiating the InfluxDB Client

After loading the credentials, the next step is to create a function that instantiates the InfluxDB client. We’ll define a NewClient() function responsible for this task, utilizing the configuration struct previously defined. This function ensures seamless creation of the client with the provided configuration.

Implementing Batch Write Function for InfluxDB Data

After setting up the InfluxDB client instantiation, the next step is to implement a function for batch writing data to InfluxDB v3. We’ll create a function named BatchWrite() responsible for this task. This function will take a slice of annotated Measurement structs and convert them into an []interface{} format, facilitating batch writing to InfluxDB v3.

Writing Data to InfluxDB v3

With all the required functions in place, we are now ready to instantiate the client and write a slice of data to InfluxDB v3. First, we’ll initialize the client using the configuration obtained from environment variables. Then, we’ll utilize the BatchWrite() function to efficiently write the data as a batch to InfluxDB.

Exploring Data

Conclusion

In this tutorial, we’ve explored the process of writing data to InfluxDB v3 using GoLang and the influxdb3-go client library. By leveraging annotated structs and environment-based configuration loading, we’ve established a robust foundation for efficiently managing InfluxDB data within Go applications.

We began by setting up a Go project and initializing the InfluxDB client with credentials loaded from environment variables. Through the implementation of a BatchWrite() function, we demonstrated how to streamline the process of writing data in batches to InfluxDB v3.

By following the steps outlined in this tutorial, you’re now equipped with the knowledge to seamlessly integrate InfluxDB data writing capabilities into your Go applications, ensuring reliability and scalability for your time-series data management needs.

References

https://github.com/komminarlabs/iiot-measurements-influxdb3

influxdb3-go/README.md at main · InfluxCommunity/influxdb3-go (github.com)

Line protocol | InfluxDB Cloud Serverless Documentation (influxdata.com)

Top comments (0)