4.9 nodejs操作多种数据库

该博客详细介绍了如何从MySQL数据库中读取数据,然后分别写入Redis、MongoDB和Elasticsearch。过程包括配置数据库连接,使用Node.js操作数据库,以及设置数据映射和存储结构。这是一个多步骤的数据处理和存储流程,适用于需要实时数据同步的场景。
摘要由CSDN通过智能技术生成

1、目录结构

2、从mysql中取数据

npm install mysql

config/mysql_config.js

module.exports = {
    options: {
        host: '192.168.3.74',
        user: 'root',
        password: '123456',
        database: 'testdata'
    },
    sql: 'select * from testdata_check',
}

mysql/read_mysql.js

const mysql = require('mysql')

let connect = (options) => {
    let connection = mysql.createConnection(options)
    return new Promise((resolve, reject) => {
        connection.connect(function (err) {
            if (err) {
                console.error('error connecting: ' + err.stack);
                return;
            }
            console.log('mysql connected as id ' + connection.threadId);
            resolve(connection)
        });
    })
}

let read = (connection, sql) => {
    return new Promise(((resolve, reject) => {
        connection.query(sql, function (err, results, filed) {
            resolve(results)
        })
    }))
}

module.exports = {
    connect,
    read
}

2、写入redis,存储为hash

npm install redis

redis/operate_redis.js

const redis = require('redis')

let con = (url) => {
    return new Promise((resolve, reject) => {
        let client = redis.createClient({url: url})
        client.on('error', (err) => console.log('Redis Client Error', err));
        client.on('connect', () => console.log('Redis Client Connected'))
        client.connect().then(r => {
        })
        resolve(client)
    })
}
let write = (con, arraylist) => {
    return new Promise((resolve, reject) => {
        for (let i in arraylist) {
            if (arraylist.hasOwnProperty(i)) {
                if (arraylist[i]['id'] && arraylist[i]['task_instance_id']) {
                    con.hSet(arraylist[i]['id'], arraylist[i]['task_instance_id'], arraylist[i]['crawl_content']);
                }
            }
        }
        resolve(arraylist.length)
    })
}
module.exports = {
    con,
    write
}

 run.js

const {read, connect} = require('../mysql/read_mysql');
const {write, con} = require('./operate_redis');
const mysql_config = require('../config/mysql_config');
const redis_config = require('../config/redis_config');

(async () => {
    let c = await connect(mysql_config.options)
    let array = await read(c, mysql_config.sql)
    let redis_client = await con(redis_config.redis_url)
    await write(redis_client, array)
    await c.end()
    await redis_client.disconnect()
})()

config/redis_config.js

module.exports = {
    redis_url: 'redis://10.7.128.15:6379'
}

3、写入mongodb

npm install mongoose

mongodb/write.js

const mongoose = require('mongoose')

let con = (url) => {
    return new Promise(((resolve, reject) => {
        mongoose.connect(url).then(r => {
        })
        let db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function () {
            console.log('mongodb connected')
        });
        resolve(mongoose)
    }))
}

let create_schema = (obj) => {
    return new Promise(((resolve, reject) => {
        let test_schema = mongoose.Schema(obj)
        resolve(test_schema)
    }))
}

let create_model = (schema) => {
    return new Promise(((resolve, reject) => {
        let TestModel = mongoose.model('TestModel', schema)
        resolve(TestModel)
    }))
}

let save = (model, array) => {
    return new Promise(((resolve, reject) => {
        for (let i in array) {
            if (array.hasOwnProperty(i)) {
                let m = new model(array[i])
                m.save(function (err, m) {
                    if (err) return console.log(err)
                    else console.log('success')
                })
            }
        }
    }))
}

module.exports = {
    con,
    create_schema,
    create_model,
    save
}

 mongodb/run.js

const {con, create_model, create_schema, save} = require('./write');
const mongo_config = require('../config/mongo_config');
const mysql_config = require('../config/mysql_config');
const {connect, read} = require('../mysql/read_mysql');
(async () => {
    let c = await connect(mysql_config.options)
    let arr = await read(c, mysql_config.sql)
    let db = await con(mongo_config.mongo_url)
    let obj = {
        id: String,
        crawl_cfg_id: String,
        detection_type: String,
        crawl_file_info_id: String,
        task_instance_id: String,
        crawl_content: String,
        col6: String,
        col7: String,
        col8: String
    }
    let test_schema = await create_schema(obj)
    let TestModel = await create_model(test_schema)
    await save(TestModel, arr)
    await c.end()
    db.disconnect()
})()

config/mongo_config.js

module.exports = {
    mongo_url: 'mongodb://10.7.128.16/testdata'
}

4、写入elastchsearch

client方式

npm install @elastic/elasticsearch

const {Client } = require('@elastic/elasticsearch');

(async ()=>{
    const client = new Client({node:'http://10.7.128.15:9200'})
    const result = await client.search({
        index:'index',
        type:'testdata'
    })
    await console.log(result.body)
})()

rest方式

文档地址:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html

elastchsearch/write.js

const axios = require('axios');
const mysql_config = require('../config/mysql_config');
const es_config = require('../config/elastchsearch_config')
const {connect, read} = require('../mysql/read_mysql');

let options = {
    mappings: {}
}
options.mappings[`${es_config.entity_name}`] = {
    properties: {
        id: {
            type: "text"
        },
        crawl_cfg_id: {
            type: "text"
        },
        detection_type: {
            type: "text"
        },
        crawl_file_info_id: {
            type: "text"
        },
        task_instance_id: {
            type: "text"
        },
        crawl_content: {
            type: "text"
        },
        col6: {
            type: "text"
        },
        col7: {
            type: "text"
        },
        col8: {
            type: "text"
        },
    }
}
let config = {
    method: 'put',
    url: '',
    headers: {},
    body: null
};
let createIndex = (config) => {
    return new Promise(((resolve, reject) => {
        config.body = options
        config.url = `${es_config.url}/${es_config.index_name}`
        axios(config).then().catch(err => {
            console.log('Index already exist')
        })
        resolve()
    }))
}

let save = (config, array) => {
    return new Promise((resolve, reject) => {
        for (let i in array) {
            if (array.hasOwnProperty(i)) {
                config.url = `${es_config.url}/${es_config.index_name}/${es_config.entity_name}/${Number.parseInt(i) + 1}`
                config.body = array[i]
                config.headers['content-type'] = 'application/json; charset=UTF-8'
                axios.put(config.url, config.body, config.headers).then()
            }
        }
        resolve()
    })
};
(async () => {
    let c = await connect(mysql_config.options)
    let r = await read(c, mysql_config.sql)
    await createIndex(config)
    await save(config, r)
    c.end()
})()

config/elastchsearch_config.js

module.exports = {
    url:'http://10.7.128.15:9200',
    index_name:'index',
    entity_name:'testdata'
}
Node.js中操作MySQL数据库,你需要引入对应的数据库操作模块,并进行必要的配置。以下是一个示例代码: ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', // 数据库地址 user: 'root', // 用户名 password: '', // 密码 database: 'class', // 数据库名 multipleStatements: true, // 允许执行多条语句 }); // 建立与数据库的连接 connection.connect((err) => { if (err) throw err; console.log('连接成功'); }); // 执行查询操作 const sql = 'SELECT * FROM user'; connection.query(sql, (err, result) => { if (err) { console.log(err.message); } else { console.log('------------------'); console.log(result); console.log('查询完成'); console.log('------------------'); } }); // 关闭数据库连接 connection.end((err) => { if (err) throw err; console.log('关闭成功'); }); ``` 以上代码示例中,我们首先引入了mysql模块,并创建了一个与数据库的连接。然后,我们执行了一条查询语句,查询了user表中的全部内容,并将结果打印出来。最后,我们关闭了与数据库的连接。请注意,你需要根据你的实际情况修改连接配置和查询语句。 #### 引用[.reference_title] - *1* [nodeJs操作mysql数据库](https://blog.csdn.net/qq_40375518/article/details/107521107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [nodejs操作MySQL数据库](https://blog.csdn.net/weixin_52851967/article/details/128589773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Node.js——在Node.js中操作MySQL](https://blog.csdn.net/weixin_50233139/article/details/121470144)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值