Influxdb-nodejs連接Influxdb

schema.js

const Influx= require('influxdb-nodejs');
const client = new Influx('http://localhost:8086/myTest');

const fieldSchema = {
    use: 'integer',
    code: 'integer',
    bytes: 'integer',
    url: 'string',
};

const tagSchema = {
    spdy: ['speedy', 'fast', 'slow'],
    method: '*',
    type: [1, 2, 3, 4, 5]
};

client.schema('', fieldSchema, tagSchema, {
    stripUnknown: true,
});

// 生成retention policy
client.createRetentionPolicy('myTest', '1h')
    .then(() => console.info('create retention policy success'))
    .catch(err => console.error(`create retention policy fail, ${err}`));

module.exports = client;

index.js

const express = require('express');
const app = express();
const client = require('./schema');
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

const checkDatabase = (req, res, next) => {
    client.showDatabases()
        .then(database => {
            if (!database.includes('myTest')) {
                return createDatabase();
            }
        })
        .catch(console.error);
    next();
};

const createDatabase = () => {
    client.createDatabase()
        .then(() => console.info('create database success'))
        .catch(err => console.error(`create database fail, ${err.message}`));
};

app.post('/create', checkDatabase, (req, res) => {
    const { tag, field } = req.body;
    client.write('http')
        .tag(tag)
        .field(field)
        .then(() => {
            console.info('write point success');
            res.send('write point success');
        })
        .catch(err => {
            console.error(err);
            res.send(err);
        });
});

app.get('/query', (req, res) => {
    const { type, spdy } = req.query;
    client.query('http')
        .where('type', type)
        .where('spdy', spdy)
        .then(result => {
            if (result.results[0].series) {
                console.log(result.results[0].series[0]);
                res.json(result.results[0].series[0]);
            }
        })
        .catch(err => {
            console.error(err);
            res.send(err);
        });
});

app.listen(3000, () => {
    console.log('listen on http://localhost:3000');
});

使用postman添加數(shù)據(jù)


image.png

使用postman查詢數(shù)據(jù)


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概要 64學時 3.5學分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,828評論 0 3
  • 推薦一款接口測試工具!POSTMAN!簡單來說,四個詞,簡單實用大方美觀! Postman是一款功能強大的網(wǎng)頁調(diào)試...
    依北辰閱讀 708,983評論 63 561
  • 對于java中的思考的方向,1必須要看前端的頁面,對于前端的頁面基本的邏輯,如果能理解最好,不理解也要知道幾點。 ...
    神尤魯?shù)婪?/span>閱讀 898評論 0 0
  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當在唯一索引所對應(yīng)的列上鍵入重復值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 5,957評論 0 9
  • 案例分析 最近看到同事UI發(fā)了一個張圖片,想到好久都沒有手動畫過圖了,我們先看看我們要的效果圖 我們看其中一個進行...
    Cheep閱讀 1,305評論 0 3

友情鏈接更多精彩內(nèi)容