百度智能云

All Product Document

          Time-Spatial Database

          Write Operation

          Write Single-Field Data Points

          Basic Process

          1. Create TsdbClient.
          2. If desired to run the writeDataPoints () method, you need to provide with specific information about the data written.

          You can refer to the following code to write the single-field data points:

          Note: When the metric, field, tags and timestamp written are all the same, the value written later overrides the value written first.

          // build the datapoints to write
          var datapoints = [
          	{
                  "metric": "cpu_idle",
                  "field": "test",
                  "tags": {
                      "host": "server1",
                      "rack": "rack1"
                  },
                  "type": "Long",
                  "timestamp": Math.round(new Date().getTime() / 1000),   // to generate the time stamp
                  "value": 51
              }
          ];
          // get and return the results
          client.writeDatapoints(datapoints)
              .then(response => console.log(response))         // got successfully
              .catch(error => console.error(error));           // failed to get and return the error type

          At this time, you can click on the query panel in the corresponding database and a new metric will appear under the option Metrics.

          For the same field, if the value of one data type is written, the same field is not allowed to write to other data types.

          Write Multiple-Field Data Points

          Write the single-field data points with basic process.

          Different fields do not need to be written at the same time. It can be considered that different fields are independent. However, if you want to find out with one statement during the query, you need to ensure that metric, all tags and timestamps are consistent.

          Following code can be referred to write the multiple-field data points:

          // build datapoints to write
          var datapoints = [
          	{
                  "metric": "cpu_idle3",
                  "field" : "field2",
                  "tags": {
                      "host": "server1",
                      "rack": "rack1"
              	  },
                  "type": "Long",
                  "timestamp": Math.round(new Date().getTime() / 1000),
                  "value": 51
              },
              {
                  "metric": "cpu_idle3",
                  "field": "field1",
                  "tags": {
                      "host": "server1",
                      "rack": "rack1"
              	 },
                  "type": "Long",
                  "values": [
                      [Math.round(new Date().getTime() / 1000), 60]   // to generate the time stamp
                  ]
              }
          ];
          // get and return the results
          client.writeDatapoints(datapoints)
              .then(response => console.log(response))         // wrote successfully
              .catch(error => console.error(error));           // failed to write and return the error type
          Previous
          Client Create a TsdbClient
          Next
          Query Operation