Bulk Insert: How to Append Time Series


  • store.BulkInsert is RavenDB's high-performance data insertion operation.
    Using its TimeSeriesFor interface's Append method resembles using session.TimeSeriesFor, but session liabilities are omitted so a much greater speed is gained.

  • You can bulk-insert a single time series at a time.

  • To bulk-insert an additional time series concurrently, open an additional BulkInsertOperation instance.

  • In this page:

Syntax

  • BulkInsert.TimeSeriesFor

    public TimeSeriesBulkInsert TimeSeriesFor(string id, string name)
    Parameters Type Description
    id string Document ID
    name string Time Series Name

  • There are two TimeSeriesFor.Append overloads:

    // Each appended entry has a single value.
    public void Append(DateTime timestamp, double value, string tag = null)
    // Each appended entry has multiple values.
    public void Append(DateTime timestamp, 
        ICollection<double> values, string tag = null)
    Parameters Type Description
    timestamp DateTime TS-entry's timestamp
    value double A single value
    values ICollection<double> Multiple values
    tag string TS-entry's tag (optional)

Usage Flow

  • Create a store.BulkInsert instance.
  • Pass the instance's TimeSeriesFor -
    • Document ID
    • Time series name
  • Call Append as many times as you like. Pass it -
    • The entry's Timestamp
    • The entry's Value or Values
    • The entry's Tag (optional)

Usage Samples

  • In this sample, we append two entries to a time series.

    // Use BulkInsert to append 2 time-series entries
    using (BulkInsertOperation bulkInsert = store.BulkInsert())
    {
        using (TimeSeriesBulkInsert timeSeriesBulkInsert = bulkInsert.TimeSeriesFor(documentId, "HeartRates"))
        {
            timeSeriesBulkInsert.Append(baseline.AddMinutes(2), 61d, "watches/fitbit");
            timeSeriesBulkInsert.Append(baseline.AddMinutes(3), 62d, "watches/apple-watch");
        }
    }
  • In this sample, we append a hundred entries to a time series.

    // Use BulkInsert to append 100 time-series entries
    using (BulkInsertOperation bulkInsert = store.BulkInsert())
    {
        using (TimeSeriesBulkInsert timeSeriesBulkInsert = bulkInsert.TimeSeriesFor(documentId, "HeartRates"))
        {
            for (int minute = 0; minute < 100; minute++)
            {
                timeSeriesBulkInsert.Append(baseline.AddMinutes(minute), new double[] { 80d }, "watches/fitbit");
            }
        }
    }
  • In this sample, we append two sets of values to a time series.

    // Use BulkInsert to append 2 sets of time series entries
    using (BulkInsertOperation bulkInsert = store.BulkInsert())
    {
    
        ICollection<double> ExerciseHeartRate = new List<double>
        { 89d, 82d, 85d };
    
        ICollection<double> RestingHeartRate = new List<double>
        {59d, 63d, 61d, 64d, 64d, 65d };
    
        using (TimeSeriesBulkInsert timeSeriesBulkInsert = bulkInsert.TimeSeriesFor(documentId, "HeartRate"))
        {
            timeSeriesBulkInsert.Append(baseline.AddMinutes(2), ExerciseHeartRate, "watches/fitbit");
            timeSeriesBulkInsert.Append(baseline.AddMinutes(3), RestingHeartRate, "watches/apple-watch");
        }
    }