MQTT Query
The database query topic for MQTT is db/query
. Send a query request to this topic, and the server will respond with the result to the db/reply
topic or the topic specified in the reply
field of the request.
Query JSON
param | default | description |
---|---|---|
q | n/a | SQL query string |
reply | db/reply | The topic where to receive the result of query |
format | json | Result data format: json, csv, box |
timeformat | ns | Time format: s, ms, us, ns |
tz | UTC | Time Zone: UTC, Local and location spec |
compress | no compression | compression method: gzip |
rownum | false | including rownum: true, false |
heading | true | showing heading: true, false |
precision | -1 | precision of float value, -1 for no round, 0 for int |
More Parameters in format=json
Since v8.0.12
Those options are available only when format=json
param | default | description |
---|---|---|
transpose | false | produce cols array instead of rows. |
rowsFlatten | false | reduce the array dimension of the rows field in the JSON object. |
rowsArray | false | produce JSON that contains only array of object for each record. |
A basic query example shows the client subscribe to db/reply/#
and publish a query request to db/query
with reply field db/reply/my_query
so that it can identify the individual reply from multiple messages.
{
"q": "select name,time,value from example limit 5",
"format": "csv",
"reply": "db/reply/my_query"
}

A demonstration shows how to query and receive responses over MQTT. (Using MQTTX.app)
Client Examples
JSH app
Since v8.0.52In this example, you will learn how to subscribe to a reply topic, send an SQL query request, and receive the result over MQTT.
Subscribe to the Reply Topic
The client first subscribes to a specific reply topic, such asdb/reply/my_query
. This topic is where the server will send the query result.Publish the SQL Query Request
The client then publishes a message to thedb/query
topic. The message includes the SQL query (q
), the desired result format (format
), and the reply topic (reply
) where the result should be sent.Receive and Process the Response
When the server processes the query, it sends the result to the specified reply topic. The client receives this message and prints the result.
Below is the complete code example:
|
|
Node.js Client
npm install mqtt --save
|
|
$ node main.js
+----------+-------+
| TIME | VALUE |
+----------+-------+
| 05:46:19 | 69.4 |
| 05:46:22 | 26.4 |
| 05:46:25 | 42.8 |
+----------+-------+
Go client
Define data structure for response
type Result struct {
Success bool `json:"success"`
Reason string `json:"reason"`
Elapse string `json:"elapse"`
Data ResultData `json:"data"`
}
type ResultData struct {
Columns []string `json:"columns"`
Types []string `json:"types"`
Rows [][]any `json:"rows"`
}
Subscribe ‘db/reply’
|
|
Publish ‘db/query’
jsonStr := `{ "q": "select * from EXAMPLE order by time desc limit 5" }`
client.Publish("db/query", 1, false, []byte(jsonStr))