opentsdb_json_example.js 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const taos = require("td2.0-connector");

const conn = taos.connect({
  host: "localhost",
});

const cursor = conn.cursor();

function createDatabase() {
  cursor.execute("CREATE DATABASE test");
  cursor.execute("USE test");
}

function insertData() {
  const lines = [
    {
      metric: "meters.current",
      timestamp: 1648432611249,
      value: 10.3,
D
dingbo 已提交
20
      tags: { location: "California.SanFrancisco", groupid: 2 },
21 22 23 24 25
    },
    {
      metric: "meters.voltage",
      timestamp: 1648432611249,
      value: 219,
D
dingbo 已提交
26
      tags: { location: "California.LosAngeles", groupid: 1 },
27 28 29 30 31
    },
    {
      metric: "meters.current",
      timestamp: 1648432611250,
      value: 12.6,
D
dingbo 已提交
32
      tags: { location: "California.SanFrancisco", groupid: 2 },
33 34 35 36 37
    },
    {
      metric: "meters.voltage",
      timestamp: 1648432611250,
      value: 221,
D
dingbo 已提交
38
      tags: { location: "California.LosAngeles", groupid: 1 },
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    },
  ];

  cursor.schemalessInsert(
    [JSON.stringify(lines)],
    taos.SCHEMALESS_PROTOCOL.TSDB_SML_JSON_PROTOCOL,
    taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_NOT_CONFIGURED
  );
}

try {
  createDatabase();
  insertData();
} finally {
  cursor.close(); 
  conn.close();
}