Inserting data on SlicingDice

You'll learn how to insert data in your SlicingDice's databases through API, SQL or using our Data Loading & Preparation Module.


Inserting data using the Data Loading & Preparation Module

If you already have external data sources that hold your data, you can use the Data Loading & Preparation Module to populate your SlicingDice's databases.

To do it, you'll need to use this module to connect to your data source and then load your data, inserting it in your SlicingDice's databases. Learn how to do it accessing the Data Loading & Preparation guides.


Inserting data using API

Another way of inserting data on SlicingDice's databases is through our API endpoints that can be accessed by our SDK clients or using direct HTTP REST requests.

You can do it defining an API POST request, containing a JSON object, to the /insert endpoint as you can see on the example below.

$ curl -X POST https://api.slicingdice.com/v1/insert \
    -H 'Authorization: MASTER_OR_WRITE_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
        "Entity1": {
            "dimension": "default",
            "state": "CA",
            "city": "San Francisco"
        },
        "Entity2": {
            "dimension": "default",
            "state": "NY",
            "city": "New York"
        }
    }'
from pyslicer import SlicingDice
slicingdice = SlicingDice(master_key='MASTER_API_KEY')

insert_data = {
    "Entity1": {
    		"dimension": "default",
        "city": "San Francisco",
        "state": "CA"
    },
    "Entity2": {
        "dimension": "default",
        "state": "NY",
        "city": "New York"
    }
}

print(slicingdice.insert(insert_data))
import com.slicingdice.jslicer.SlicingDice;
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONArray;

public class Example {
    public static void main(String[] args) throws IOException {
        SlicingDice slicingdice = new SlicingDice("MASTER_API_KEY");

        JSONObject insertData = new JSONObject()
        .put("Entity1", new JSONObject()
            .put("dimension", "default")
            .put("city", "San Francisco")
            .put("state", "CA"))
        .put("Entity2", new JSONObject()
            .put("dimension", "default")
            .put("city", "New York")
            .put("state", "NY"))

        JSONObject result = slicingdice.insert(insertData);
        System.out.println(result.toString());
    }
}
require 'rbslicer'
slicingdice = SlicingDice.new(master_key: 'MASTER_API_KEY')

insert_data = {
    "Entity1" => {
        "dimension" => "default",
        "city" => "San Francisco",
        "state" => "CA"
    },
    "Entity2" => {
        "dimension" => "default",
        "city" => "New York",
        "state" => "NY"
    }
}

puts slicingdice.insert(insert_data)
const SlicingDice = require('slicerjs');

const slicingdice = new SlicingDice({masterKey: 'MASTER_API_KEY'});

const insertData = {
    "Entity1": {
        "dimension": "default",
        "city": "San Francisco",
        "state": "CA"
    },
    "Entity2": {
        "dimension": "default",
        "state": "NY",
        "city": "New York"
    }
};

slicingdice.insert(insertData).then((resp) => {
    console.log(resp);
}, (err) => {
    console.error(err);
});
<?php
use Slicer\SlicingDice;
$slicingdice = new SlicingDice(array("masterKey" => "MASTER_API_KEY"));

$insertData = array(
    "Entity1" => array(
        "dimension" => "default",
        "city" => "San Francisco",
        "state" => "CA"
    ),
    "Entity2" => array(
        "dimension" => "default",
        "state" => "NY",
        "city" => "New York"
    )
);

print_r($slicingdice->insert($insertData));
?>
using System.Collections.Generic;
using Slicer;
using Newtonsoft.Json;

namespace SlicerTester.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var slicingdice = new SlicingDice(masterKey: "MASTER_API_KEY");

            var insertData = new Dictionary<string, dynamic>{
                {"Entity1", new Dictionary<string, dynamic>{
                    {"dimension", "default"},
                    {"city", "San Francisco"},
                    {"state", "CA"}
                }},
                {"Entity1", new Dictionary<string, dynamic>{
                    {"dimension", "default"},
                    {"city", "New York"},
                    {"state", "NY"}
                }}
            };

            var result = slicingdice.Insert(insertData);
            System.Console.WriteLine(JsonConvert.SerializeObject(result).ToString());
        }
    }
}
package main
import (
    "fmt"
    "github.com/SlicingDice/slicingdice-go/slicingdice"
)
func main() {
    keys := new(slicingdice.APIKey)
    keys.MasterKey = "MASTER_API_KEY"
    slicingdice := slicingdice.New(keys, 60)

    insertData := map[string]interface{}{
        "Entity1": map[string]interface{}{
            "dimension": "default",
            "city": "San Francisco",
            "state": "CA",
        },
        "Entity2": map[string]interface{}{
            "dimension": "default",
            "city": "New York",
            "state": "NY",
        },
    }

    fmt.Println(slicingdice.Insert(insertData))
}

Notice that every key used on the JSON object is one entity-id that contains another JSON object mapping the columns you may have with their respective values. Each entity-id also may contain the dimension that this data will be inserted.

If everything works fine you'll receive a success message like this one, informing how many entities were inserted and how many values were inserted:

{
    "status": "success",
    "inserted-entities": 1,
    "inserted-values": 4,
    "took": 0.014
}

Quick Tip: Using the auto-create parameter

The auto create parameter is optional and can be used to create dimensions and columns automatically if they don't exist yet when you make an insertion. You can see how to use this auto-create parameter here:

$ curl -X POST https://api.slicingdice.com/v1/insert \
    -H 'Authorization: MASTER_OR_WRITE_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
        "Entity1": {
            "dimension": "new-dim",
            "state": "CA",
            "city": "San Francisco"
        },
        "Entity2": {
            "dimension": "new-dim",
            "state": "NY",
            "city": "New York"
        },
        "auto-create": ["dimension", "column"]
    }'
from pyslicer import SlicingDice
slicingdice = SlicingDice(master_key='MASTER_API_KEY')

insert_data = {
    "Entity1": {
    		"dimension": "new-dim",
        "city": "San Francisco",
        "state": "CA"
    },
    "Entity2": {
        "dimension": "new-dim",
        "state": "NY",
        "city": "New York"
    },
    "auto-create": ["dimension", "column"]
}

print(slicingdice.insert(insert_data))
import com.slicingdice.jslicer.SlicingDice;
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONArray;

public class Example {
    public static void main(String[] args) throws IOException {
        SlicingDice slicingdice = new SlicingDice("MASTER_API_KEY");

        JSONObject insertData = new JSONObject()
        .put("Entity1", new JSONObject()
            .put("dimension", "new-dim")
            .put("city", "San Francisco")
            .put("state", "CA"))
        .put("Entity2", new JSONObject()
            .put("dimension", "new-dim")
            .put("city", "New York")
            .put("state", "NY"))
        .put("auto-create", new JSONArray()
            .put("dimension")
            .put("column"));

        JSONObject result = slicingdice.insert(insertData);
        System.out.println(result.toString());
    }
}
require 'rbslicer'
slicingdice = SlicingDice.new(master_key: 'MASTER_API_KEY')

insert_data = {
    "Entity1" => {
        "dimension" => "new-dim",
        "city" => "San Francisco",
        "state" => "CA"
    },
    "Entity2" => {
        "dimension" => "new-dim",
        "city" => "New York",
        "state" => "NY"
    },
    "auto-create" => [
        "dimension",
        "column"
    ]
}

puts slicingdice.insert(insert_data)
const SlicingDice = require('slicerjs');

const slicingdice = new SlicingDice({masterKey: 'MASTER_API_KEY'});

const insertData = {
    "Entity1": {
        "dimension": "new-dim",
        "city": "San Francisco",
        "state": "CA"
    },
    "Entity2": {
        "dimension": "new-dim",
        "state": "NY",
        "city": "New York"
    },
    "auto-create": [
        "dimension",
        "column"
    ]
};

slicingdice.insert(insertData).then((resp) => {
    console.log(resp);
}, (err) => {
    console.error(err);
});
<?php
use Slicer\SlicingDice;
$slicingdice = new SlicingDice(array("masterKey" => "MASTER_API_KEY"));

$insertData = array(
    "Entity1" => array(
        "dimension" => "new-dim",
        "city" => "San Francisco",
        "state" => "CA"
    ),
    "Entity2" => array(
        "dimension" => "new-dim",
        "state" => "NY",
        "city" => "New York"
    ),
    "auto-create" => array(
        "dimension",
        "column"
    )
);

print_r($slicingdice->insert($insertData));
?>
using System.Collections.Generic;
using Slicer;
using Newtonsoft.Json;

namespace SlicerTester.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var slicingdice = new SlicingDice(masterKey: "MASTER_API_KEY");

            var insertData = new Dictionary<string, dynamic>{
                {"Entity1", new Dictionary<string, dynamic>{
                    {"dimension", "new-dim"},
                    {"city", "San Francisco"},
                    {"state", "CA"}
                }},
                {"Entity2", new Dictionary<string, dynamic>{
                    {"dimension", "new-dim"},
                    {"city", "New York"},
                    {"state", "NY"}
                }},
                {"auto-create", new List<dynamic>{
                    "dimension",
                    "column"
                }}
            };

            var result = slicingdice.Insert(insertData);
            System.Console.WriteLine(JsonConvert.SerializeObject(result).ToString());
        }
    }
}
package main
import (
    "fmt"
    "github.com/SlicingDice/slicingdice-go/slicingdice"
)
func main() {
    keys := new(slicingdice.APIKey)
    keys.MasterKey = "MASTER_API_KEY"
    slicingdice := slicingdice.New(keys, 60)

    insertData := map[string]interface{}{
        "Entity1": map[string]interface{}{
            "dimension": "new-dim",
            "city": "San Francisco",
            "state": "CA",
        },
        "Entity2": map[string]interface{}{
            "dimension": "new-dim",
            "city": "New York",
            "state": "NY",
        },
        "auto-create": []interface{}{
            "dimension",
            "column",
        },
    }

    fmt.Println(slicingdice.Insert(insertData))
}

Inserting data using SQL

You can insert data on your SlicingDice's databases using SQL in two different ways: through a SQL workbench that you may connect using our ODBC/JDBC drivers or through a POST request using our SQL-based API endpoint (/sql). Below you can check how this insertion can be done:

INSERT INTO
  default ([entity-id], state, city)
VALUES
  ("Entity1", "CA", "San Francisco");
INSERT INTO
  default ([entity-id], state, city)
VALUES
  ("Entity2", "NY", "New York");'
$ curl -X POST https://api.slicingdice.com/v1/sql \
    -H 'Authorization: MASTER_OR_WRITE_API_KEY' \
    -H 'Content-Type: application/sql' \
    -d 'INSERT INTO
  default ([entity-id], state, city)
VALUES
  ("Entity1", "CA", "San Francisco");
INSERT INTO
  default ([entity-id], state, city)
VALUES
  ("Entity2", "NY", "New York");'