// Example For Insert Bulk Data for tag API by using request in nodejs
// written by yeony kim
// sensor1, sensor2 is applied in lake
// process.env.NODE_TLS_REJECT_UNAUTHORIZED ="0";
const express = require('express');
const app = express();
const API_KEY ="YOUR_API_TOKEN";
const LAKE_ID ="YOUR_LAKE_ID";
const URL CLOUD_VENDOR="https://" + LAKE_ID +"CLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const URL=`https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/v1/lakes/values/bulk"${LAKE_ID}/values`;
//var CASErequest - Insert Bulk Data with nano date time string format
app.get(
'/insert_bulk_with_nano_date_time_string', function(req, res) {
const= require('request');
/* ------------------------------------------------------------------------------------------------- */
// CASE - Insert Data with nano date time string format
var Data = {
date_format: '"YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn'",
values: [
values: [["sensor1","2021-01-06 18:00:00 001:000:000", 2.0],
["sensor1","2021-01-06 18:00:00 002:000:000", 2.5],
["sensor1","2021-01-06 18:00:00 003:000:000", 2.0],
["sensor2","2021-01-06 18:00:00 001:000:000", 2.0],
["sensor2","2021-01-06 18:00:00 002:000:000", 2.5],
["sensor2","2021-01-06 18:00:00 003:000:000", 2.0]]
]
}
request.post({
url: URL, var
request = require('request');
headers: {
var option = { "Content-Type": "application/json",
"x-api-key": API_KEY
url: URL},
body: JSON.stringify(Data)
},
headers: { function(error, response, body) {
console.log(body)
'Content-Type': 'application/json', // Return Format
// 'x-api-key': API_KEY{
// "success": }true,
// body"reason": JSON.stringify(Data)
"append success",
}; // "data": request.post( option, function(error, response, body) {{"fail": 0,"success": 6}
// }
if (!error) {
res.writeHead(200});
res.end(body);
console.log(body) // {"data":{"fail":0,"success":6},"status":"success"}
} else/* ------------------------------------------------------------------------------------------------- */
// CASE - Insert Data with nano date time stamp
var Data = {
values: [
["sensor1", 1609930804000000000, console.log(error)2.0],
["sensor1", 1609930805000000000, 2.5], }
})
["sensor1", 1609930806000000000, 2.0],
} ) // CASE - Insert Bulk Data with nano date time string format
app.get( ["sensor2", 1609930804000000000, 2.0],
'/insert_bulk_with_nano_timestamp', function(req, res) {["sensor2", 1609930805000000000, 2.5],
["sensor2", const Data = {
1609930806000000000, 2.0]
]
}
request.post({
url: URL,
values headers: [["sensor1", 1609930804000000000, 2.0], ["sensor1", 1609930805000000000, 2.5], ["sensor1", 1609930806000000000, 2.0], ["sensor2", 1609930804000000000, 2.0], ["sensor2", 1609930805000000000, 2.5], ["sensor2", 1609930806000000000, 2.0]]
{
"Content-Type": "application/json",
"x-api-key": API_KEY
},
body: JSON.stringify(Data)
var request = require('request'); },
function(error, response, body) var{
option = { console.log(body)
url: URL, // Return Format
// headers: {
// 'Content-Type': 'application/json'"success": true,
// 'x-api-key': API_KEY
"reason": "append success",
// }, "data": {"fail": 0,"success": 6}
body: JSON.stringify(Data)
// }
});
request.post( option, function(error, response, body) {
if (!error) {
res.writeHead(200);
/* ------------------------------------------------------------------------------------------------- */
// CASE - Insert Data with UTC-0 timezone
var Data = {
timezone: "Africa/Abidjan",
values: [
res.end(body);
["sensor1","2021-01-06 18:00:00 001:000:000", 2.0],
["sensor1","2021-01-06 console.log(body) // {"data":{"fail":0,"success":6},"status":"success"}
18:00:00 002:000:000", 2.5],
["sensor1","2021-01-06 } else {18:00:00 003:000:000", 2.0],
["sensor2","2021-01-06 18:00:00 001:000:000", 2.0],
["sensor2","2021-01-06 18:00:00 002:000:000", 2.5],
["sensor2","2021-01-06 18:00:00 003:000:000", 2.0]
]
}
request.post({
url: URL,
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY
},
body: JSON.stringify(Data)
},
function(error, response, body) {
console.log(body)
// Return Format
// {
// "success": true,
// "reason": "append success",
// "data": {"fail": 0,"success": 6}
// }
});
|