使用restbed 寫入influxDB 資料庫

2021-09-26 03:18:17 字數 2253 閱讀 3996

influxdb是目前比較流行的時間序列資料庫。它和傳統的資料庫有所不同。

influxdb 與 mysql 對比

庫、表比較

influxdb

傳統資料庫中的概念

database

資料庫measurement

資料庫中的表

points

表裡面的一行資料

influxdb 資料的構成

point由時間戳(time)、資料(field)、標籤(tags)組成。

point屬性

傳統資料庫中的概念

time

每個資料記錄時間,是資料庫中的主索引

(會自動生成)

fields

各種記錄值(沒有索引的屬性)也就是記錄的值:溫度,

濕度 br#各種有索引的屬性:地區,海拔

預設埠

http service 預設執行埠:8086: influxdb

可以通過命令列,庫或者http api來訪問influxdb 資料庫。這裡我們介紹http api 訪問influxdb 。

測試http api 的方式可以使用 curl 命令列,也可以使用postman 軟體。當然,也可以自己程式設計序。

使用post 方法 建立資料庫

curl 命令

在postmap 中選擇 post 

raw 中寫入:q=create+database+mydb

curl 命令

在postmap 中選擇 post 

body 選擇 raw 輸入:vibration,host=localhost  value=32.145 

其它命令

刪除資料庫

下面是使用restbed 實現的c++ influxdb 客戶端。遇到的問題是 influxdb 的響應。它是一種 chunk 編碼的 json 。getbody 時會帶有乙個16進製制的chunk size。需要讀它,並跳過它。

**

#include #include #include #include #include #include #include #include "rapidjson/document.h"

#include "rapidjson/writer.h"

#include "rapidjson/stringbuffer.h"

#include #include using namespace std;

using namespace restbed;

using namespace rapidjson;

#define db_host "192.168.31.108:8086"

void read_chunk(std::shared_ptr< response > response ,const bytes& data );

int response_parser(string response_body )

}}int query_db(string url,string message_body)

else if ( request->has_header( "content-length" ) )

}return 0;}

void write_value(string db_name,string measurement, float val)

int main( const int, const char** )

return exit_success;

}

C 使用 StreamWriter 寫入資料

networkstream 類 memorystream類 和 filestream 類都提供了以位元組為基本單位的讀寫方法,但是這種方法首先將待寫入的資料轉換為位元組序列後才能進行讀寫,當操作的是使用字元編碼的文字資料時,使用很不方便。因此,在操作文字資料時,一般使用streamwriter 類與...

C 使用XmlWriter寫入XML檔案

xmlwriter 表示乙個編寫器,該編寫器提供一種快速 非快取和只進的方式來生成包含 xml 資料的流或檔案。假設我們要寫入以下結構的xml booklist book name 書籍1 id 1 category 計算機 book name 書籍2 id 2 category 計算機 bookl...

使用PHPExcel讀取寫入Excel檔案資料

首先是讀取excel 引入phpexcel類檔案 require once phpexcel 1.8 classes phpexcel.php require once phpexcel 1.8 classes phpexcel writer excel2007.php 建立新物件,並開啟資料檔案 ...