Nodejs操作Cassandra数据库

Posted 野火冫吹又生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nodejs操作Cassandra数据库相关的知识,希望对你有一定的参考价值。


前言

  • 操作系统win10
  • 时间2019年02月
  • Nodejs版本:node v8.9.3
  • Cassandra版本:cassandra-3.11.3
  • 参考网址1

安装Cassandra

安装Cassandra数据库

  1. 官网下载Cassandra压缩包
  2. 解压,并配置环境变量:
    |操作|变量名|变量值|
    |:---:|:---|:---|
    |新建|CASSANDRA_HOME|解压路径|
    |增加|PATH|解压路径in;|

安装Nodejs的Cassandra依赖包

npm install cassandra-driver -g

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

测试

C:Userswahaha>cassandra

Nodejs操作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, ‘[email protected]‘];

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);
    }
});




以上是关于Nodejs操作Cassandra数据库的主要内容,如果未能解决你的问题,请参考以下文章

nodejs常用代码片段

使用 NodeJS 和 JSDOM/jQuery 从代码片段构建 PHP 页面

javascript 用于在节点#nodejs #javascript内设置react app的代码片段

怎么用jdbc连接cassandra数据库

NodeJs GraphQL 片段解析器

如何安装和配置Cassandra