Nodejs操作Cassandra数据库

匿名 (未验证) 提交于 2019-12-02 23:05:13

  • 操作系统win10
  • 时间2019年02月
  • Nodejs版本:node v8.9.3
  • Cassandra版本:cassandra-3.11.3
  • 参考网址1
  1. 官网下载Cassandra压缩包
  2. 解压,并配置环境变量:
    |操作|变量名|变量值|
    |:---:|:---|:---|
    |新建|CASSANDRA_HOME|解压路径|
    |增加|PATH|解压路径\bin;|
npm install cassandra-driver -g

-g参数表示全局安装,这样在哪都能用了

C:\Users\wahaha>cassandra

select

包含一份日志配置,可以根据不同的日志级别输出成文件

/**  * Connect loacal cassandra database.  * Output the log as a file '*.log'.  */ const cassandra = require('cassandra-driver'); var PlainTextAuthProvider = cassandra.auth.PlainTextAuthProvider;  // config log const log4js = require('log4js');  log4js.configure({     appenders: {         out: {             type: 'console'         },         select_info: {             type: 'file',             category: 'select_info',             filename: './logs/select_info.log'         },         select_error: {             type: 'file',             category: 'select_error',             filename: './logs/select_error.log'         }     },     categories: {         default: {             appenders: ['out'],             level: 'info'         },         select_info: {             appenders: ['select_info'],             level: 'info'         },         select_error: {             appenders: ['select_error'],             level: 'error'         },     } });  const logger_info = log4js.getLogger('select_info'); const logger_error = log4js.getLogger('select_error');  // connect config const client = new cassandra.Client({     // 如果是远程数据库,则将IP和账号密码进行相应的更换     contactPoints: ['127.0.0.1'],     authProvider: new PlainTextAuthProvider('cassandra', 'cassandra'),     localDataCenter: 'datacenter1' });  const query1 = 'select * from test.user;'; const query1 = 'describe keyspaces;';  client.execute(query1, function (err, res) {     if (err) {         logger_error.error(err);     };     try {         client.shutdown();         var str_date = new Date().toLocaleString();         for (i = 0; i < res.rowLength; i++) {             console.log(res.rows[i]);         };         console.log('select %d successed at time %s', res.rowLength, str_date);         logger_info.info('select %d successed at time %s', res.rowLength, str_date);     } catch (error) {         console.log(error);         logger_error.error(error);     }; });

insert

包含一份日志配置,可以根据不同的日志级别输出成文件

/**  * Connect loacal cassandra database and insert data.  * Output the log as a file '*.log'.  */ const cassandra = require('cassandra-driver'); var PlainTextAuthProvider = cassandra.auth.PlainTextAuthProvider;  // config log const log4js = require('log4js');  log4js.configure({     appenders: {         out: {             type: 'console'         },         select_info: {             type: 'file',             category: 'select_info',             filename: './logs/select_info.log'         },         select_error: {             type: 'file',             category: 'select_error',             filename: './logs/select_error.log'         }     },     categories: {         default: {             appenders: ['out'],             level: 'info'         },         select_info: {             appenders: ['select_info'],             level: 'info'         },         select_error: {             appenders: ['select_error'],             level: 'error'         },     } });  const logger_info = log4js.getLogger('select_info'); const logger_error = log4js.getLogger('select_error');  // connect const client = new cassandra.Client({     // 如果是远程数据库,则将IP和账号密码进行相应的更换     contactPoints: ['127.0.0.1'],     authProvider: new PlainTextAuthProvider('cassandra', 'cassandra'),     localDataCenter: 'datacenter1' });  const query1 = 'insert into test.user (name, age, email) values (?, ?, ?);'; const params1 = ['zzz', 22, 'zzz@22.com'];  client.execute(query1, params1, {     prepare: true }, function (err, result) {     if (err) {         logger_error.error(err);     };     try {         client.shutdown();         var str_date = new Date().toLocaleString();         logger_info.info('insert success at time %s', str_date);     } catch (error) {         logger_error.error(error);     } });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!