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添加数据


image.png

使用postman查询数据


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,431评论 0 3
  • 推荐一款接口测试工具!POSTMAN!简单来说,四个词,简单实用大方美观! Postman是一款功能强大的网页调试...
    依北辰阅读 708,083评论 63 561
  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 851评论 0 0
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,500评论 0 9
  • 案例分析 最近看到同事UI发了一个张图片,想到好久都没有手动画过图了,我们先看看我们要的效果图 我们看其中一个进行...
    Cheep阅读 1,165评论 0 3