Baidu AI Cloud
中国站

百度智能云

Time-Spatial Database

Query Operation

Get the Metric

Basic Process

  1. Create a TsdbClient.
  2. Run the getMetrics () method.

The following code can get the metric list:

// get and print the Metric
client.getMetrics()
    .then(response => console.log(response.body))        // got successfully
    .catch(error => console.error(error));               // failed to get and return the error type

The running result is as follows:

//  return results from the termination 
{
	metrics: [ 
		'cpu_idle',
		'cpu_idle1' 
	] 
}

Get the Field

Basic Process

  1. Create a TsdbClient.
  2. Run the getFields () method, and you need to provide with the metricName of the query.

The following code can get the Field list:

var metricName = <metricName>;
// get and print Field
client.getFields(<metricName>)
    .then(response => console.log(response.body))        // got successfully
    .catch(error => console.error(error));               // failed to get and return the error type

The running result is as follows:

//  similar results from the termination 
{ 
	"fields": [
		 "field1",
		 "field2"
	]
 }

Get the Tag

Basic Process

  1. Create a TsdbClient.
  2. Run the getTags() method, and you need to provide with the metricName of the query.

The following code can get the Tag list:

var metricName = <metricName>;
// get and print Tag
client.getTags(<metricName>)
    .then(response => console.log(response.body))        // got successfully
    .catch(error => console.error(error));               // failed to get and return the error type

The running result is as follows:

//  return results from the termination 
{ 
	tags: [ 
		'tags1',
		'tags2' 
	] 
}

Query Data Points

Query Data Points in Single Field

Basic Process

  1. Create a TsdbClient.
  2. Run the getDatapoints() method, and you need to provide with the query list built with the requirements.

The following code can query data points:

// build the queryList to query
var queryList = [
    {
	    "metric": "cpu_idle1",
	    "filters": {
	        "start": "1 hour ago",
	        "tags": {
	            "host": [
	                "server1",
	                "server2"
	            ]
	        },
	        "value": ">= 10"
	    },
	    "groupBy": [
	        {
	            "name": "Tag",
	            "tags": [
	                "rack"
	            ]
	        }
	    ]
	}
];

// get and print the Query result
client.getDatapoints(<queryList>)
    .then(response => console.log(JSON.stringify(response.body)))            // got successfully
    .catch(error => console.error(error));                   // failed to get and return the error type

The running result is as follows:

//  similar results return from the termination
{ 
	results: [
		{ 
		   metric: 'humidity', 
		   field: 'value',
		   groups: [], 
		   rawCount: 0 
		} 
	] 
}

Query Data Points in Multiple Fields

Basic Process

  1. Create a TsdbClient.
  2. Run the getDatapoints() method, and you need to provide with the query list built with the requirements.

The following code can query data points:

// build the queryList to query
var queryList = [
    {
        "metric": "cpu_idle3",
        "fields": [
            "field1",
            "field2"
        ],
        "tags": [
            "rack",
            "host"
        ],
        "filters": {
            "start": "5 hour ago",
            "fields": [
                {
                    "field": "field1",
                    "value": ">= 10"
                },
                {
                    "field": "field2",
                    "value": "<= 10"
                }
            ],
            "tags": {
                "rack": [
                    "rack1"
                ],
                "host": [
                    "server1"
                ]
            },
            
        },
        "groupBy": [
            {
                "name": "Tag",
                "tags": [
                    "rack",
                    "host"
                ]
            }
        ],
        "limit": 1000
    }
];
// get and print the Query result
client.getDatapoints(<queryList>)
    .then(response => console.log(JSON.stringify(response.body)))           // got successfully
    .catch(error => console.error(error));                   // failed to get and return the error type
????

The running result is as follows:

//  similar results return from the termination
{ 
	results: [ 
		{ 
			metric: 'humidity',
	 		field: 'value', 
			groups: [], 
			rawCount: 0
	   }
	]
}

Query Data Points Using Interpolation

Basic Process

  1. Create a TsdbClient.
  2. Run the getDatapoints() method, and you need to provide with the query list built with the requirements.

The following code can fill the data points:

// build the queryList to query
var queryList = [
    {
        "metric": "cpu_idle3",
        "field": "field1",
        "filters": {
           "start": "1 hour ago",
           "tags": {
              "host": [
                 "server1"
                 ]
              }
         },
         "fill": {
            "type": "Linear",
            "interval": "5 minutes",
            "maxWriteInterval": "30 minutes"
         }
     }
 ];
// get and print the Query result
client.getDatapoints(<queryList>)
     .then(response => console.log(JSON.stringify(response.body)))             // got successfully
     .catch(error => console.error(error));                   // failed to get and return the error type

The running result is as follows:

//  similar results return from the termination
{ 
	results: [ 
		{ 
			metric: 'humidity', 
			field: 'value',
 			groups: [],
 			rawCount: 0 
  		} 
  	]
}

Paging Query Data Points

Basic Process

  1. Create a TsdbClient.
  2. Run the getDatapoints() method, and you need to provide with the query list built with the requirements.
  3. According to the result.truncated of the returned result, judge whether there is any data on the next page, if there is, Run 2, otherwise end.

The code example is as follows:

// build the query
var query = {
    "metric": "cpu_idle1",
    "filters": {
        "start": "1 hour ago",
    }
};
var fetchNext = nextMarker => {
    query.marker = nextMarker;                             // set the marker to get the following data
    client.getDatapoints([query,])                         // get data
        .then(deealWithResponse)                           // set the callback of the result
        .catch(dealWithError);                             // set the callback of dealing with error
};
var deealWithResponse = response => {                      // dealing result
    console.log(JSON.stringify(response.body))             // print result
    if (response.body.results[0].truncated) {              // there are more data behind
        fetchNext(response.body.results[0].nextMarker);    // get the next page
    }
}
var dealWithError = error => console.error(error);         // deall with the error

client.getDatapoints([query,])                             // get data
    .then(deealWithResponse)                               // set the callback of the result
    .catch(dealWithError);                                 // set the callback of dealing with error

SQL Query Interface

NodeSDK supports SQL query interface in version 0.3. 1 and supports standard ANSI SQL semantics.

Basic Process

  1. Create a TsdbClient.
  2. Run the getRowsWithSql(sql) method

The following code can query data points by using SQL:

  var sql = 'select * from cpu_idle';
  client.getRowsWithSql(sql)
      .then(function (response) {
            console.log(response.body);
      });
Previous
Write Operation
Next
URL Generate the Pre-Signed URL of Querying Data Points