未验证 提交 8ec62c1d 编写于 作者: B Bo Ding 提交者: GitHub

docs: minor change for 09-emq-broker.md (#13352)

上级 97281b47
...@@ -21,17 +21,17 @@ Depending on the current operating system, users can download the installation p ...@@ -21,17 +21,17 @@ Depending on the current operating system, users can download the installation p
### Take the Docker installation of TDengine as an example ### Take the Docker installation of TDengine as an example
```bash ```bash
docker exec -it tdengine bash docker exec -it tdengine bash
taos taos
``` ```
### Create Database and Table ### Create Database and Table
```sql ```sql
CREATE DATABASE test; CREATE DATABASE test;
USE test; USE test;
CREATE TABLE sensor_data (ts timestamp, temperature float, humidity float, volume float, PM10 float, pm25 float, SO2 float, NO2 float, CO float, sensor_id NCHAR(255), area TINYINT, coll_time timestamp); CREATE TABLE sensor_data (ts timestamp, temperature float, humidity float, volume float, PM10 float, pm25 float, SO2 float, NO2 float, CO float, sensor_id NCHAR(255), area TINYINT, coll_time timestamp);
``` ```
Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario. Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario.
...@@ -81,21 +81,21 @@ Edit the resource configuration to add the key/value pairing for Authorization. ...@@ -81,21 +81,21 @@ Edit the resource configuration to add the key/value pairing for Authorization.
## Compose program to mock data ## Compose program to mock data
```javascript ```javascript
// mock.js // mock.js
const mqtt = require('mqtt') const mqtt = require('mqtt')
const Mock = require('mockjs') const Mock = require('mockjs')
const EMQX_SERVER = 'mqtt://localhost:1883' const EMQX_SERVER = 'mqtt://localhost:1883'
const CLIENT_NUM = 10 const CLIENT_NUM = 10
const STEP = 5000 // Data interval in ms const STEP = 5000 // Data interval in ms
const AWAIT = 5000 // Sleep time after data be written once to avoid data writing too fast const AWAIT = 5000 // Sleep time after data be written once to avoid data writing too fast
const CLIENT_POOL = [] const CLIENT_POOL = []
startMock() startMock()
function sleep(timer = 100) { function sleep(timer = 100) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(resolve, timer) setTimeout(resolve, timer)
}) })
} }
async function startMock() { async function startMock() {
const now = Date.now() const now = Date.now()
for (let i = 0; i < CLIENT_NUM; i++) { for (let i = 0; i < CLIENT_NUM; i++) {
const client = await createClient(`mock_client_${i}`) const client = await createClient(`mock_client_${i}`)
...@@ -119,12 +119,12 @@ Edit the resource configuration to add the key/value pairing for Authorization. ...@@ -119,12 +119,12 @@ Edit the resource configuration to add the key/value pairing for Authorization.
await sleep(AWAIT) await sleep(AWAIT)
} }
console.log(`Done, use ${(Date.now() - now) / 1000}s`) console.log(`Done, use ${(Date.now() - now) / 1000}s`)
} }
/** /**
* Init a virtual mqtt client * Init a virtual mqtt client
* @param {string} clientId ClientID * @param {string} clientId ClientID
*/ */
function createClient(clientId) { function createClient(clientId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const client = mqtt.connect(EMQX_SERVER, { const client = mqtt.connect(EMQX_SERVER, {
clientId, clientId,
...@@ -141,11 +141,11 @@ Edit the resource configuration to add the key/value pairing for Authorization. ...@@ -141,11 +141,11 @@ Edit the resource configuration to add the key/value pairing for Authorization.
reject(e) reject(e)
}) })
}) })
} }
/** /**
* Generate mock data * Generate mock data
*/ */
function generateMockData() { function generateMockData() {
return { return {
"temperature": parseFloat(Mock.Random.float(22, 100).toFixed(2)), "temperature": parseFloat(Mock.Random.float(22, 100).toFixed(2)),
"humidity": parseFloat(Mock.Random.float(12, 86).toFixed(2)), "humidity": parseFloat(Mock.Random.float(12, 86).toFixed(2)),
...@@ -158,7 +158,7 @@ Edit the resource configuration to add the key/value pairing for Authorization. ...@@ -158,7 +158,7 @@ Edit the resource configuration to add the key/value pairing for Authorization.
"area": Mock.Random.integer(0, 20), "area": Mock.Random.integer(0, 20),
"ts": 1596157444170, "ts": 1596157444170,
} }
} }
``` ```
Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients. Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册