Skip to content

Batch Upload Data Via Client Libraries

Stephen Fortune edited this page Aug 15, 2017 · 3 revisions

There are two software libraries that make adding a batch of data to Bothan straightforward:

This tutorial will detail how to use the Ruby library to get a batch of data (say historical data for a metric you would like to track over a long time) onto Bothan.

This tutorial will demonstrate how the library can be used to consume a JSON file and store it on the Bothan platform. The tutorial is written such that you can post directly to your own Bothan instance - if you do not have your own bothan instance the credentials for posting to the Bothan demo instance will suffice.

Batch Upload: Single Value Metric

If you have data in a JSON file (named say, single-value-metric-data.json) structured like this:

[
  {
  "time": "2017-08-10T07:00:42.534+00:00",
  "value": 60
  },
  {
  "time": "2017-08-11T07:00:42.519+00:00",
  "value": 17
  },

]

JSON file that is an array of JSON objects it's straightforward to use ruby to store all the data on Bothan.

Create a ruby file and include the bothan gem and the json gem

require 'bothan'
require 'json'

next initialise a connection to your Bothan instance

@bothan = Bothan::Connection.new('your_username, 'your_password', 'https://yourbothaninstance.herokuapp.com')

Then open the JSON file and push its contents to Bothan using the following script

data_file = File.read('single-value-metric-data.json')
json_array = JSON.parse(data_file)
json_array.each {|array_element|
    @bothan.metrics.create("single-value-metric-endpoint", array_element["value"], array_element["time"])
}

JSON.parse consumes the datafile and creates an array of JSON objects (with key | value pairs)

Batch Upload: Multiple Value Metric

If you have data in a JSON file of multiple value metrics structured as follows

[
  {
    "time": "2017-08-10T07:00:42.544+00:00",
    "values": {
      "value1": 94,
      "value2": 79,
      "value3": 33,
      "value4": 50
    }
  },
  {
    "time": "2017-08-11T07:00:42.528+00:00",
    "values": {
      "value5": 66,
      "value6": 1,
      "value7": 44,
      "value8": 91
    }
  },

]

Create a ruby file, require the bothan and json gems and establish a connection to your Bothan instance

require 'bothan'
require 'json'
@bothan = Bothan::Connection.new('your_username, 'your_password', 'https://yourbothaninstance.herokuapp.com')

When utilising JSON parse for the multiple value endpoint the structure of the JSON file is important.

data_file = File.read('multiple-values-metric-data.json')
json_array = JSON.parse(data_file)
json_array.each {|array_element|
    @bothan.metrics.create_multiple("multiple-metric-values-endpoint", array_element["values"], array_element["time"])
}

In the above snippet the iterator is able to access the "values" attribute of each array element which happens to be well formed as the Bothan ruby library expects it to be - as such the entire JSON object can be passed by array reference .

Next Steps

  • How best to structure your JSON / use of schemas for conformity
  • Code for uploading a large file to Bothan (to use File streaming or a similarly non-blocking approach)
  • How to construct your data in a CSV file and add it to Bothan