車(chē)聯(lián)網(wǎng)屬于物聯(lián)網(wǎng)的一個(gè)分支,通過(guò)車(chē)載終端采集數(shù)據(jù),利用無(wú)線網(wǎng)絡(luò)傳輸?shù)皆品?wù)平臺(tái)進(jìn)行持久化存儲(chǔ),最終提供基于實(shí)時(shí)/歷史數(shù)據(jù)的個(gè)性化服務(wù)。
目前初創(chuàng)型的車(chē)輛網(wǎng)企業(yè),接入的車(chē)輛通常低于10萬(wàn),數(shù)據(jù)采集頻率遠(yuǎn)遠(yuǎn)大于1秒。這個(gè)級(jí)別的數(shù)據(jù)規(guī)模,如果采用HBase系的技術(shù)方案,需要至少6臺(tái)8核32G配置的機(jī)器,而采用TDengine Database作為數(shù)據(jù)存儲(chǔ)引擎,一臺(tái)2核8G的機(jī)器就可以完成。
技術(shù)架構(gòu)
TDengine Database作為時(shí)序處理引擎,可以完全不用Kafka、HDFS/HBase/Spark、Redis等軟件,大幅簡(jiǎn)化大數(shù)據(jù)平臺(tái)的設(shè)計(jì),降低研發(fā)成本和運(yùn)營(yíng)成本。因?yàn)樾枰傻拈_(kāi)源組件少,因而系統(tǒng)可以更加健壯,也更容易保證數(shù)據(jù)的一致性。
- 基于HBase的解決方案,架構(gòu)圖如下

- 而基于TDengine的解決方案,架構(gòu)圖如下

數(shù)據(jù)模型
車(chē)載終端采集的數(shù)據(jù)字段非常多,很多企業(yè)按照國(guó)標(biāo)ISO 22901建立數(shù)據(jù)模型,也有公司按照業(yè)務(wù)需要使用自定義的數(shù)據(jù)模型。但通常,采集數(shù)據(jù)都包含如下字段,本文也采用這種方法構(gòu)造數(shù)據(jù)模型。
- 采集時(shí)間(時(shí)間戳)
- 車(chē)輛標(biāo)志(字符串)
- 經(jīng)度(雙精度浮點(diǎn))
- 維度(雙精度浮點(diǎn))
- 海拔(浮點(diǎn))
- 方向(浮點(diǎn))
- 速度(浮點(diǎn))
- 車(chē)牌號(hào)(字符串)
- 車(chē)輛型號(hào)(字符串)
- 車(chē)輛vid(字符串)
不同于其他時(shí)序數(shù)據(jù)引擎,TDengine為每輛車(chē)單獨(dú)創(chuàng)建一張數(shù)據(jù)表,數(shù)據(jù)字段為采集時(shí)間、車(chē)輛標(biāo)志、經(jīng)度、緯度、海拔、方向、速度等與時(shí)間序列相關(guān)的采集數(shù)據(jù);標(biāo)簽字段為車(chē)牌號(hào)、車(chē)輛型號(hào)等車(chē)輛本身固定的描述信息。這里面有一個(gè)小技巧,浮點(diǎn)數(shù)據(jù)壓縮比相對(duì)整型數(shù)據(jù)壓縮比很差,經(jīng)度緯度通常精確到小數(shù)點(diǎn)后7位,因此將經(jīng)度緯度增大1E7倍轉(zhuǎn)為長(zhǎng)整型存儲(chǔ),將海拔、方向、速度增大1E2倍轉(zhuǎn)為整型存儲(chǔ)。
創(chuàng)建數(shù)據(jù)庫(kù)的語(yǔ)句為
create database db cache 8192 ablocks 2 tblocks 1000 tables 10000;
創(chuàng)建超級(jí)表的SQL語(yǔ)句為
create table vehicle(ts timestamp, longitude bigint, latitude bigint, altitude int, direction int, velocity int) tags(card int, model binary(10));
以車(chē)輛vid作為表名(例如vid為1,車(chē)牌號(hào)為25746,類(lèi)型為bmw),那么創(chuàng)建數(shù)據(jù)表的語(yǔ)句為
create table v1 using tags(25746, ‘bmw’);
數(shù)據(jù)寫(xiě)入
仍然以車(chē)輛v1為例,寫(xiě)入一條記錄到表v1的SQL語(yǔ)句為
insert into v1 values(1562150939000,1,2,3,4,5);
測(cè)試數(shù)據(jù)的生成,可以采用批量數(shù)據(jù)寫(xiě)入方法,類(lèi)似
insert into v1 values(1562150939000,1,1,1,1,1) (1562150969000,2,2,2,2,2) (1562150999000,3,3,3,3,3) (……)(……);
本文采用C語(yǔ)言編寫(xiě)了一個(gè)車(chē)輛模擬數(shù)據(jù)生成程序,該程序首先10萬(wàn)張數(shù)據(jù)表,然后每張數(shù)據(jù)表寫(xiě)入1個(gè)月的數(shù)據(jù)(數(shù)據(jù)間隔1分鐘,計(jì)44000條數(shù)據(jù))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "time.h"
#include "taos.h"
int main(int argc, char *argv[]) {
taos_init();
TAOS *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("failed to connect to server, reason:%s\n", taos_errstr(taos));
exit(1);
}
if (taos_query(taos, "create database db cache 8192 ablocks 2 tblocks 1000 tables 10000") != 0) {
printf("failed to create database, reason:%s\n", taos_errstr(taos));
exit(1);
}
taos_query(taos, "use db");
char sql[65000] = "create table vehicles(ts timestamp, longitude bigint, latitude bigint, altitude int, direction int, velocity int) tags(card int, model binary(10))";
if (taos_query(taos, sql) != 0) {
printf("failed to create stable, reason:%s\n", taos_errstr(taos));
exit(1);
}
int begin = time(NULL);
for (int table = 0; table < 100000; ++table) {
sprintf(sql, "create table v%d using vehicles tags(%d, 't%d')", table, table, table);
if (taos_query(taos, sql) != 0) {
printf("failed to create table t%d, reason:%s\n", table, taos_errstr(taos));
exit(1);
}
for (int loop = 0; loop < 44; loop++) {
int len = sprintf(sql, "insert into v%d values", table);
for (int row = 0; row < 1000; row++) {
len += sprintf(sql + len, "(%ld,%d,%d,%d,%d,%d)", 1561910400000L + 60000L * (row + loop * 1000L), row, row, row, row, row);
}
if (taos_query(taos, sql) != 0) {
printf("failed to insert table t%d, reason:%s\n", table, taos_errstr(taos));
}
}
}
int end = time(NULL);
printf("insert finished, time spend %d seconds", end - begin);
}
}
將改C文件命名為test.c,在相同目錄下創(chuàng)建makefile文件
ROOT = ./ TARGET = exe LFLAGS = -Wl,-rpath,/usr/lib/ -ltaos -lpthread -lm -lrt CFLAGS = -O3 -g -Wall -Wno-deprecated -fPIC -Wno-unused-result -Wconversion -Wno-char-subscripts -D_REENTRANT -Wno-format -D_REENTRANT -DLINUX -msse4.2 -Wno-unused-function -D_M_X64 -std=gnu99 -I/usr/local/include/taos/ all: $(TARGET) exe: gcc $(CFLAGS) ./test.c -o $(ROOT)/test $(LFLAGS) clean: rm $(ROOT)test
編譯之后,將測(cè)試程序和數(shù)據(jù)庫(kù)在同一臺(tái)2核8G的臺(tái)式機(jī)上運(yùn)行,寫(xiě)入時(shí)間共計(jì)為3946秒,相當(dāng)于4400000000條/3946秒=111.5萬(wàn)條/秒,折算成點(diǎn)數(shù)為111.5*5=557萬(wàn)點(diǎn)/秒。
insert finished, time spend 3946 seconds
該程序是單線程運(yùn)行的,如將其修改成多線程,速度還會(huì)有更大提升,但是僅就目前的性能來(lái)看,對(duì)于車(chē)輛網(wǎng)的場(chǎng)景也已經(jīng)足夠。
數(shù)據(jù)查詢(xún)
TDengine在數(shù)據(jù)查詢(xún)方面做了很多針對(duì)時(shí)序數(shù)據(jù)的優(yōu)化?;谏厦嫔傻臏y(cè)試數(shù)據(jù)集進(jìn)行查詢(xún),這是一些常見(jiàn)SQL語(yǔ)句的運(yùn)行結(jié)果,性能還是有點(diǎn)嚇人的。
- 查詢(xún)總數(shù)

- 單輛車(chē)的明細(xì)數(shù)據(jù)
| 查詢(xún)類(lèi)型 | 查詢(xún)時(shí)間 |
|---|---|
| 1車(chē)當(dāng)前值查詢(xún) | 2.3ms |
| 1車(chē)1小時(shí)明細(xì)查詢(xún) | 2.1ms |
| 1車(chē)1日明細(xì)查詢(xún) | 6.3ms |
| 1車(chē)10日明細(xì)查詢(xún) | 15.4ms |
| 1車(chē)31日明細(xì)查詢(xún) | 31.6ms |

- 單輛車(chē)的聚合查詢(xún)
| 查詢(xún)類(lèi)型 | 查詢(xún)時(shí)間 |
|---|---|
| 1車(chē)1小時(shí)聚合查詢(xún) | 1.9ms |
| 1車(chē)1日聚合查詢(xún) | 1.7ms |
| 1車(chē)10日聚合查詢(xún) | 2.3ms |
| 1車(chē)31日聚合查詢(xún) | 2.2ms |

- 多輛車(chē)的單日聚合查詢(xún)
| 查詢(xún)類(lèi)型 | 查詢(xún)時(shí)間 |
|---|---|
| 1車(chē)單日聚合查詢(xún) | 3.2ms |
| 10車(chē)單日聚合查詢(xún) | 5.1ms |
| 100車(chē)單日聚合查詢(xún) | 10.4ms |
| 1000車(chē)單日聚合查詢(xún) | 51.4ms |
| 10000車(chē)單日聚合查詢(xún) | 455.9ms |
| 100000車(chē)單日聚合查詢(xún) | 2074.8ms |

- 多輛車(chē)單月聚合查詢(xún)
| 查詢(xún)類(lèi)型 | 查詢(xún)時(shí)間 |
|---|---|
| 1車(chē)單月聚合查詢(xún) | 3.1ms |
| 10車(chē)單月聚合查詢(xún) | 4.1ms |
| 100車(chē)單月聚合查詢(xún) | 7.7ms |
| 1000車(chē)單月聚合查詢(xún) | 33.7ms |
| 10000車(chē)單月聚合查詢(xún) | 289.5ms |
| 100000車(chē)單月聚合查詢(xún) | 1197.ms |

- 多輛車(chē)單月曲線查詢(xún)
| 查詢(xún)類(lèi)型 | 查詢(xún)時(shí)間 |
|---|---|
| 1車(chē)單月曲線查詢(xún) | 6.9ms |
| 10車(chē)單月曲線查詢(xún) | 13.2ms |
| 100車(chē)單月曲線查詢(xún) | 75.6ms |
| 1000車(chē)單月曲線查詢(xún) | 710.9ms |
| 10000車(chē)單月曲線查詢(xún) | 7137.6ms |
| 100000車(chē)單月曲線查詢(xún) | 32130.8ms |

- 資源消耗

數(shù)據(jù)庫(kù)服務(wù)進(jìn)程只消耗了約2.7GB的內(nèi)存,CPU占用可以忽略不計(jì)。

結(jié)果分析
TDengine Database提供的時(shí)序數(shù)據(jù)解決方案,單機(jī)情況下的平均寫(xiě)入速度在百萬(wàn)條/秒級(jí)別,單輛車(chē)的所有查詢(xún)均能做到實(shí)時(shí),多輛車(chē)的查詢(xún)速度也非常快,是車(chē)聯(lián)網(wǎng)乃至物聯(lián)網(wǎng)的必備利器。



互聯(lián)網(wǎng).png)



-1.png)




.png)


證.png)


伙伴.png)
伙伴.png)
伙伴.png)



