How to Write Points from CSV to InfluxDB v2 and InfluxDB Cloud

There are several ways to write CSV to InfluxDB v2 and InfluxDB Cloud, including:

I will demonstrate how to write CSV to an InfluxDB Cloud Free Tier account with these methods because signing up for a Free Tier account is the fastest way to get started with InfluxDB Cloud. However, the process is identical for InfluxDB v2 OSS. For each one of those write methods in the list above, we will write the following CSV to InfluxDB Cloud:

Using the Telegraf File Plugin to write CSV to InfluxDB Cloud

Telegraf is InfluxData’s plugin-driven server agent for collecting and reporting metrics. There are over 200 input plugins, which means there are many ways to get data into InfluxDB. However, I frequently see new Influx users inquiring about how to write points from CSV to InfluxDB on the InfluxData Community Site. Writing points from a CSV file is an easy way to insert familiar data into InfluxDB, which can make it easier to get acquainted with the platform.

First, I need to download a Telegraf config file with the appropriate input and output Plugins. As per the Getting Started with Telegraf documentation, I will use the following command in the terminal in the directory of my choosing.

telegraf -sample-config -input-filter file -output-filter influxdb_v2 > writeCSV.conf

  • The -sample-config flag will generate the telegraf config file.
  • The -input-filter and -output-filter flags specify the input and output sources of the data, respectively.
  • The text following the > names the config file.

After running the command, I open writeCSV.conf. My telegraf config is complete with the File Input Plugin and the InfluxDB v2 Output Plugin.

Change the input portion of your telegraf config to look like this:

You’ll need to gather the following account information to configure the output portion of your telegraf config:

  • Bucket. A bucket is where data is stored. You can think of it as a database. All buckets have retention policies. You assign a bucket a retention policy upon bucket creation. The easiest way to create a bucket is through the InfluxDB Cloud user interface (UI), as described here. However, you can also use the CLI or API to create a bucket. This bucket will be referred to as our destination bucket, “mybucket”.
  • Token: An InfluxDB authentication token ensures security for users and their data. Tokens belong to organizations and describe permissions. You’ll need to create a read/write or all-access token scoped to your destination bucket. The easiest way to create a bucket is through the InfluxDB Cloud UI, as described here. However, you can also use the CLI or API to create a token.
  • Organization: Your organization will be the email you used to set up your InfluxDB Cloud account.

Change the output portion of your telegraf config to look like this:

To test that your telegraf configuration is working as expected, use the --test flag. This command outputs the metrics in line protocol, the input format for InfluxDB, to stdout. It’s often worth running this command before to verify that your Telegraf configuration and resulting line protocol are correct before writing data to InfluxDB.

telegraf --config writeCSV.conf --test

If you’re new to Telegraf, it’s worth being aware of the debug option in the telegraf config under the agent portion of your config. This option is set to false by default:

debug = false

Set this value to true to output detailed logs about telegraf. This option is extremely valuable for debugging telegraf.

Finally, run telegraf and pass in the path to your config to write the CSV data to your InfluxDB Cloud account:

telegraf --config $pwd/writeCSV.conf

Using the CLI to write CSV to InfluxDB Cloud

First you must download the CLI in order to write a CSV to InfluxDB Cloud with the CLI. Use the following command to write a CSV to your InfluxDB destination bucket:

Supply the header flag to provide annotations or metadata about your CSV. Inject annotation headers in order of column appearance from your CSV. We identify the timestamp column and format, the tag columns, the field columns by their respective data type (double, float, int, etc.), and finally the measurement column.

To avoid having to provide your authentication credentials each time you use the InfluxDB CLI, I encourage you to create a CLI configuration. Creating and using a CLI config is the recommended way to interact with the InfluxDB CLI. This approach is described in detail in TL;DR InfluxDB Tech Tips — How to Use the CLI Locally to Access the Flux REPL and Write a Regular CSV to InfluxDB Cloud.

Finally, the dryrun command writes your data in line protocol to stdout. It’s often worth running that command to ensure that your headers and resulting line protocol are correct before writing to InfluxDB. In the example below, I have already set up an InfluxDB CLI config so I don’t need to pass in my org or token. The dryrun command outputs my line protocol and helps me verify my influx write will be successful:

Using the csv.from() Flux function to write CSV to InfluxDB Cloud

The data scripting language for InfluxDB is called Flux. Flux enables you to…

Today, we’ll use the csv.from() function to convert a CSV to annotated CSV, the InfluxDB output format, in InfluxDB Cloud. Then we’ll apply some additional Flux functions to format the output so that we can visualize it in the InfluxDB UI. Finally, we’ll use the to() function to actually write the data to our destination bucket.

First, you must import the Flux CSV package. There are two ways you can pass CSV data into the csv.from() function:

  • Store your CSV data in a variable.
  • Specify the path to your CSV. Please note that the CSV must exist in the same file system running the fluxd process. In other words, this approach doesn’t work in the InfluxDB UI for InfluxDB Cloud.

It’s also worth being aware of a second csv.from() function from the Flux Experimental Package, which allows you to write annotated CSV from a remote url.

We store our raw CSV data in a variable called csvData and use the first csv.from() function to convert our raw CSV to annotated CSV. To learn more about annotated CSV, the output format of Flux queries, please read TL;DR InfluxDB Tech Tips — How to Interpret an Annotated CSV.

The above screenshot shows the annotated CSV output from the csv.from(csv: csvData, mode: "raw") line. Data is viewed by toggling on the raw data view in the InfluxDB UI. The csv.from() function converts data to a string type, as indicated by datatype annotation row.

  • Then we use the rename() function to rename the usage_user column to “_value”. This column is renamed because the InfluxDB UI visualizes columns with the name “_value” by default.
  • The csv.from() function will convert our data to strings by default.
  • The map() function applies the time() function to each record in the “_time” column.
  • The time() function converts the string value to a time.

Using the Python client to write CSV to InfluxDB Cloud

The InfluxDB Python Client supports Pandas and pandas dataframes which I believe is the easiest way to write the CSV to InfluxDB. Your code would look like this:

After installing pandas and installing the InfluxDB v2 Python Client, import the libraries. Then instantiate the InfluxDB Client by providing the URL of your InfluxDB instance, your token, and your org. Use the to_datetime() function to set the timestamp column of your dataframe to a datetime object. Set the datetime column as the index of your dataframe with the set_index() function. Use the write method to write data to InfluxDB. Specify the destination bucket you wish to write your dataframe to and pass to the record parameter. Also specify your dataframe tag columns and measurement column.

Conclusions about writing CSV to InfluxDB v2 Cloud and OSS

I hope this InfluxDB blog post helps you to get started using InfluxDB and writing CSV to InfluxDB. If you are using the new InfluxDB and need help, please ask for some in our community site or Slack channel. If you’re developing a cool IoT application or monitoring your application on top of InfluxDB, we’d love to hear about it, so make sure to share your story! Additionally, please share your thoughts, concerns, or questions in the comments section. We’d love to get your feedback and help you with any problems you run into!

Further reading

While this post aims to provide a comprehensive overview of how to write CSV to InfluxDB, the following resources might also interest you:

  1. TL;DR InfluxDB Tech Tips — How to Use the CLI Locally to Access the Flux REPL and Write a Regular CSV to InfluxDB Cloud: This post shares how to use the InfluxDB CLI with InfluxDB Cloud to write a CSV to InfluxDB.
  2. TL;DR InfluxDB Tech Tips — How to Interpret an Annotated CSV: This post describes how to interpret an Annotated CSV, the Flux query result format for InfluxDB.
  3. Getting Started with Python and InfluxDB v2.0: This post describes how to get started with the Python Client Library.
  4. Getting Started with InfluxDB and Pandas: This post shares how to get started with Pandas to write dataframes to InfluxDB and return dataframes with a Flux query.
  5. Top 5 Hurdles for Flux Beginners and Resources for Learning to Use Flux: This post describes common hurdles for Flux beginners and how to tackle them by using the InfluxDB UI, understanding Annotated CSV, and more.
  6. Top 5 Hurdles for Intermediate Flux Users and Resources for Optimizing Flux: This post describes common hurdles for intermediate and advanced Flux users while providing more detail on pushdown patterns, how the Flux engine works, and more.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store