// 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 +".machlake.com/lakes/values/bulk";
// CASE - Insert Bulk Data with nano date time string format
app.get(
'/insert_bulk_with_nano_date_time_string', function(req, res) {
const Data = {
date_format: 'YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn',
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]]
}
var request = require('request');
var option = {
url: URLCLOUD_VENDOR";
const CLOUD_REGION="CLOUD_REGION";
const URL=`https://${CLOUD_VENDOR}.${CLOUD_REGION}.machlake.com/v1/lakes/${LAKE_ID}/values`;
var request = 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: [
["sensor1","2021-01-06 18:00:00 001:000:000", 2.0],
headers: {["sensor1","2021-01-06 18:00:00 002:000:000", 2.5],
["sensor1","2021-01-06 18:00:00 003:000:000", 2.0],
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
["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}
// }
});
/* ------------------------------------------------------------------------------------------------- */
// CASE - Insert Data with nano date time stamp
var Data = {
values: [
body: JSON.stringify(Data)
};["sensor1", 1609930804000000000, 2.0],
request.post( option, function(error, response, body) {["sensor1", 1609930805000000000, 2.5],
["sensor1", 1609930806000000000, 2.0],
if (!error) { ["sensor2", 1609930804000000000, 2.0],
res.writeHead(200); ["sensor2", 1609930805000000000, 2.5],
["sensor2", 1609930806000000000, res.end(body);2.0]
]
}
request.post({
url: URL,
console.log(body) // {"data":{"fail":0,"success":6},"status":"success"}
headers: {
"Content-Type": "application/json",
} else { "x-api-key": API_KEY
},
body: consoleJSON.logstringify(errorData)
},
function(error, response, }body) {
})
}
console.log(body)
// CASE - Insert Bulk Data with nano// dateReturn timeFormat
string format app.get( '/insert_bulk_with_nano_timestamp', function(req, res) // {
const// Data = { "success": true,
// values: [["sensor1", 1609930804000000000, 2.0], ["sensor1", 1609930805000000000, 2.5], ["sensor1", 1609930806000000000, 2.0], ["sensor2", 1609930804000000000, 2.0], ["sensor2", 1609930805000000000, 2.5], ["sensor2", 1609930806000000000, 2.0]]reason": "append success",
// "data": {"fail": 0,"success": 6}
// }
});
var request = require('request');
var option = {
url: URL,
headers: {
/* ------------------------------------------------------------------------------------------------- */
// CASE - Insert Data with UTC-0 timezone
var Data = {
timezone: "Africa/Abidjan",
values: [
["sensor1","2021-01-06 'Content-Type': 'application/json'18:00:00 001:000:000", 2.0],
'x-api-key': API_KEY["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 body: JSON.stringify(Data)18:00:00 001:000:000", 2.0],
};
["sensor2","2021-01-06 18:00:00 002:000:000", 2.5],
request.post( option, function(error, response, body) { ["sensor2","2021-01-06 18:00:00 003:000:000", 2.0]
]
}
if (!error) request.post({
url: URL,
headers: {
res.writeHead(200); "Content-Type": "application/json",
res.end(body);
"x-api-key": API_KEY
},
body: consoleJSON.logstringify(bodyData)
// {"data":{"fail":0,"success":6},"status":"success"} },
function(error, response, body) {
} else { console.log(body)
// Return Format
console.log(error) // {
} // })
"success": true,
} ) app.listen(8888, function() {// console.log('http://127.0.0.1:8888/insert_bulk_with_nano_date_time_string is result for insert bulk data with nano date time string')
console.log('http://127.0.0.1:8888/insert_bulk_with_nano_timestamp is result for insert bulk data with nano timestamp')
})"reason": "append success",
// "data": {"fail": 0,"success": 6}
// }
});
|