How to bulk insert (from c#)?

Hi @ben_bln,

thanks for using our client.

The WriteApi is suppose to run as a long live singleton, because it uses background thread for batching and ingesting data into InfluxDB.

For your purpose - bulk-insert - is better to use WriteApiAsync.

You can use something like:

using System.Collections.Generic;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;

namespace Examples
{
    public static class InfluxDBExample
    {
        public static async Task Main(string[] args)
        {
            const string url = "http://localhost:9999";
            const string token = "my-token";
            const string org = "my-org";
            const string bucket = "my-bucket";
            using var client = InfluxDBClientFactory.Create(url, token.ToCharArray());

            var list_of_lines = new List<string>();
            for (var min_idx = 0; min_idx < 500; min_idx++)
            {
                var datarecord = "aseries ";

                for (var col_idx = 0; col_idx < 100 - 1; col_idx++)
                {
                    datarecord += $"header{col_idx}={col_idx + min_idx},";
                }

                datarecord = datarecord.Remove(datarecord.Length - 1, 1);
                datarecord += " " + min_idx;

                list_of_lines.Add(datarecord);
            }


            await client.GetWriteApiAsync().WriteRecordsAsync(bucket, org, WritePrecision.Ns, list_of_lines);
        }
    }
}

You can also split your records into batches by:

// configure batching
const int batchSize = 100;
var batches = list_of_lines
    .Select((s, i) => list_of_lines.Skip(i * batchSize).Take(batchSize).ToList())
    .Where(it => it.Any())
    .ToList();

foreach (var batch in batches)
{
    Console.WriteLine($"Ingesting batch... {batch.Count}");

    await client.GetWriteApiAsync().WriteRecordsAsync(bucket, org, WritePrecision.Ns, batch);
}

Regards