根据github提供的apI编写,使用的是express框架
cql.js
var express = require('express');
var router = express.Router();
var cql = require('node-cassandra-cql');
var client = new cql.Client({hosts:['localhost:9042','localhost:9160'],keyspace: 'demo'});
/* GET users listing. */
router.get('/', function(req, res, next) {
client.execute('SELECT * FROM test where id=?', [1],
function(err, result){
if (err){
console.log('execute failed');
}
else {
console.log('got user profile with users ');
console.log(result.rows[0].name);
res.render('cql',{result: result.rows[0],title: 'execute'});
}
}
);
});
router.get('/executeAsPrepared', function(req, res, next) {
var query = 'select id,name,address,age from demo.test where id=?';
var params = [2];
var consistency = cql.types.consistencies.quorum;
client.executeAsPrepared(query, params, consistency, function(err,result) {
if (err) console.log('Something when wrong and the row was not selected');
else {
console.log('select on the cluster name %s address %s',result.rows[0].name,result.rows[0].address);
res.render('cql',{result: result.rows[0],title: 'executeAsPrepared'});
}
});
});
//版本驱动不支持
router.get('/executeBatch', function(req, res, next) {
var userId = cql.types.uuid();
var messageId = cql.types.uuid();
var queries = [
{
query: 'INSERT INTO demo.test (id, name,address,age) values (?, ?)',
params: [userId, 'Zoro','beijing',25]
}
];
var consistency = cql.types.consistencies.quorum;
client.executeBatch(queries, consistency, function(err,result) {
if (err) console.log('The rows were not inserted on the cluster');
else {
console.log('Data updated on cluster',result.rows[0].name);
//res.render('cql',{result: result.rows[0]});
}
});
});
router.get('/eachRow', function(req, res, next) {
client.eachRow('select id,name,address,age from demo.test where id=?', [2],
function(n, result) {
//the callback will be invoked per each result as soon as they are received
console.log('name value', n, result.name);
res.render('cql',{result: result,title: 'eachRow'});
var express = require('express');
var router = express.Router();
var cql = require('node-cassandra-cql');
var client = new cql.Client({hosts:['localhost:9042','localhost:9160'],keyspace: 'demo'});
/* GET users listing. */
router.get('/', function(req, res, next) {
client.execute('SELECT * FROM test where id=?', [1],
function(err, result){
if (err){
console.log('execute failed');
}
else {
console.log('got user profile with users ');
console.log(result.rows[0].name);
res.render('cql',{result: result.rows[0],title: 'execute'});
}
}
);
});
router.get('/executeAsPrepared', function(req, res, next) {
var query = 'select id,name,address,age from demo.test where id=?';
var params = [2];
var consistency = cql.types.consistencies.quorum;
client.executeAsPrepared(query, params, consistency, function(err,result) {
if (err) console.log('Something when wrong and the row was not selected');
else {
console.log('select on the cluster name %s address %s',result.rows[0].name,result.rows[0].address);
res.render('cql',{result: result.rows[0],title: 'executeAsPrepared'});
}
});
});
//版本驱动不支持
router.get('/executeBatch', function(req, res, next) {
var userId = cql.types.uuid();
var messageId = cql.types.uuid();
var queries = [
{
query: 'INSERT INTO demo.test (id, name,address,age) values (?, ?)',
params: [userId, 'Zoro','beijing',25]
}
];
var consistency = cql.types.consistencies.quorum;
client.executeBatch(queries, consistency, function(err,result) {
if (err) console.log('The rows were not inserted on the cluster');
else {
console.log('Data updated on cluster',result.rows[0].name);
//res.render('cql',{result: result.rows[0]});
}
});
});
router.get('/eachRow', function(req, res, next) {
client.eachRow('select id,name,address,age from demo.test where id=?', [2],
function(n, result) {
//the callback will be invoked per each result as soon as they are received
console.log('name value', n, result.name);
res.render('cql',{result: result,title: 'eachRow'});
},
function (err, resultLength) {
if (err) console.log('Oh dear...');
console.log('%d rows where returned', resultLength);
}
);
});
router.get('/streamField', function(req, res, next) {
client.streamField('select id,name from demo.test where id=?', [3],
function(err, result, photoStream) {
//the callback will be invoked per each result as soon as they are received.
if (err) console.log('Shame...');
else {
//The stream is a Readable Stream2 object
//stdout.pipe(photoStream);
console.log(result);
res.send(result);
}
});
});
// Streaming query rows
router.get('/streamRows', function(req, res, next) {
client.streamRows('select id,name,address,age from demo.test where id=?', [4],
function(err, result) {
//the callback will be invoked per each row as soon as they are received
if (err) console.log("Oh dear...That wrong");
else {
console.log('streamRows name value', result.name);
res.render('cql',{result: result,title: 'streamRows'});
}
});
});
router.get('/stream', function(req, res, next) {
client.stream('select id,name,address,age from demo.test where id=?', [5])
.on('readable', function () {
//readable is emitted as soon a row is received and parsed
var result;
while (result = this.read()) {
console.log('name %s and age %s', result.name, result.age);
res.render('cql',{result: result,title: 'stream'});
}
})
.on('end', function () {
//stream ended, there aren't any more rows
})
.on('error', function (err) {
console.log(err);
});
});
module.exports = router;
cassandra-cql测试用例(┬_┬)
<%= title%>
编号:
姓名:
地址:
年龄:
<%= result.id%>
<%= result.name %>
<%= result.address %>
<%= result.age %>