Nodejs Cassandra Client [node-cassandra-client] - node.js

I tried connecting to my cassandra cluster [version 1.0.6] via nodejs using node-cassandra-client
This is my sample script
var Connection = require('cassandra-client').Connection;
var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
console.log(con);
con.execute('UPDATE TestCF ?=? WHERE key=?', ['cola', '1', '20120132'], function(err) {
if (err) {
console.log("Failed");
} else {
console.log("success");
}
});
On executing the script
The "sys" module is now called "util". It should have a similar interface.
node-cassandra-client.driver: connecting x.x.x.x:9160 {}
{ validators: {},
client: null,
connectionInfo:
{ host: 'x.x.x.x',
port: 9160,
keyspace: 'Stats',
timeout: 10000 },
timeout: 10000 }
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot call method 'execute_cql_query' of null
at [object Object].execute (/home/tamil/workspace/TestProjects/node-cass/node_modules/cassandra-client/lib/driver.js:367:17)
at Object.<anonymous> (/home/tamil/workspace/TestProjects/node-cass/index.js:5:5)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
My nodetool stats
Address DC Rack Status State Load Owns Token
x.x.x.x datacenter1 rack1 Up Normal 1.03 MB 100.00% 0
What would be the error reason? and Need some help to fix this

the client inside your connection is null. You need to connect() first. Here's an example (ignores all errors):
var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
con.connect(function(err) {
assert.ifError(err);
con.execute('SELECT COUNT(*) FROM TestCF', [], function(err, rows) {
con.close(function(err) {
// you're done now.
});
});
});
I recommend using async to handle the ordering of connct, query, close, etc.

Looks like some kind of problem with the node.js thrift driver. I've had more luck with Helenus.

Related

Handshake inactivity timeout error AWS node.js

i'm quite new with node.js. usually i'm connect the AWS through java but this time i needed to change from java based to node.js with lambda service. i have simple connection from node.js to AWS RDS Database,
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.dbname,
});
pool.getConnection(function(err, connection) {
// Use the connection
if (err) {
console.log(err);
} else {
connection.query('Select 1+1 as numberss', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) throw error;
else console.log(results);
process.exit();
// Don't use the connection here, it has been returned to the pool
});
}
});
i've tried so many ways that i'm search in internet like adding timeout, check host information and etc.
but it's seem to return
{ Error: Handshake inactivity timeout
at Handshake.<anonymous> (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at Handshake.emit (events.js:189:13)
at Handshake._onTimeout (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/sequences/Sequence.js:124:8)
at Timer._onTimeout (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Timer.js:32:23)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/node_modules/mysql/lib/Pool.js:48:16)
at Object.<anonymous> (/Users/HAUZDevTeam/HAUZ-HQ-Lambda/local.js:9:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: 10000 }
is there any way to overcome this error or is there any wrong thing that i did on my code?

Redshift data access from node jdbc

I am getting below error when I try to data from redshift with the below mentioned code
var jdbc = new ( require('jdbc') );
var config = {
libpath: 'C:/Users/ABCD/Desktop/jar/RedshiftJDBC41-1.1.6.1006.jar',
//libs: [__dirname + 'path/to/other/jars.jar'],
drivername: 'com.amazon.redshift.jdbc41.Driver',
url: 'jdbc:redshift://examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com:5439/dev',
user: 'xxxx',
password: 'xxxxx'
};
jdbc.initialize(config, function(err, res) {
if (err) {
console.log(err);
}
});
var genericQueryHandler = function(err, results) {
if (err) {
console.log(err);
} else if (results) {
console.log(results);
}
jdbc.close(function(err) {
if(err) {
console.log(err);
} else {
console.log("Connection closed successfully!");
}
});
};
jdbc.open(function(err, conn) {
if (conn) {
// SELECT statements are called with executeQuery
jdbc.executeQuery("select * from information_schema.tables", genericQueryHandler);
}
});
Error:
C:\Users\ABCD> node redshift.js
C:\Users\ABCD\node_modules\jdbc\lib\jdbc.js:62>
if(this._props.getPropertySync('user') === undefined){>
^ TypeError: undefined is not a function
at JDBCConn.initialize
(C:\Users\ABCD\node_modules\jdbc\lib\jdbc.js:62:20)
at Object.<anonymous>
(C:\Users\ABCD\Desktop\AngularJS\redshift.js:15:6)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Could you please let me know whether there is nay issue with the above mentioned node jdbc usage for redshift?
Please be aware that this answer applies to node-jdbc 0.0.15 and earlier only. It will not work for node-jdbc 0.1.1 or later, as the API has been completely reworked and is not backwards-compatible.
Try replacing the two lines in your configuration
user: 'xxxx',
password: 'xxxxx'
with
properties: [
['user', 'xxxx'],
['password', 'xxxxx']
]
I got the same error as you attempting to use Node to connect to a local Oracle XE database. After making the change above I was able to connect. I don't believe the error you are getting is specific to RedShift - I believe it affects all databases.
Note that the properties above have to be specified as an array of 2-element arrays. An object such as the following would seem the obvious way to specify the properties, but it doesn't work:
// Don't do this, it doesn't work.
properties: {
user: 'xxxx',
password: 'xxxxx'
}
To be honest, the fix I've proposed above is a workaround. I only found it by reading the source of the jdbc module. I can't say I'm terribly impressed with this module, what with the example code given in the documentation not working, and with a somewhat counter-intuitive and undocumented format for specifying custom properties.

TypeError: Object #<Object> has no method 'Request' with elastic.js

i tried the following sample code:
var es=require("elasticsearch");
var ejs = require("ejs");
var client = new es.Client({
host: 'localhost:9200',
log: 'trace'
});
client.ping({
requestTimeout: 3000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
client.search({
index: 'message-1',
type: 'message',
body: ejs.Request().query(ejs.MatchAllQuery())
}).then(function (resp) {
var hits = resp.hits.hits;
}, function (err) {
console.trace(err.message);
});
i am getting the following error when i try to run: node metric.js
/home/karunakar/test/metricagg/metric.js:27
body: ejs.Request().query(ejs.MatchAllQuery())
^
TypeError: Object #<Object> has no method 'Request'
at Object.<anonymous> (/home/karunakar/test/metricagg/metric.js:27:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
i could not locate the problem.
-- Thanks
You want the elastic.js module and not ejs, which is a templating engine module.
You can keep the first require("elasticsearch") but the second one should be require("elastic.js") instead of require("ejs")

Getting error while running node-mysql tutorial

I am running the code in the tutorial part of node-mysql and its giving me error
Code
var MySQLPool = require("mysql-pool").MySQLPool;
var pool = new MySQLPool({
poolSize: 4,
user: 'root',
password: 'root',
database: 'test'
});
pool.query("SELECT 'Hello, World!' AS hello", function(err, rows, fields) {
if(err) throw err;
console.log(rows[0].hello);
});
for(var i = 0; i < 10; ++i) {
pool.query("SELECT SLEEP(2), ? AS i", [i], function(err, rows, fields) {
if(err) throw err;
console.log("Slept: " + rows[0].i);
});
}
ERROR:
/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:158
for(var key in Client.prototype) {
^
TypeError: Cannot read property 'prototype' of undefined
at MySQLPool._populate (/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:158:23)
at new MySQLPool (/home/comapq/Works/Nodejs/Codes/node_modules/mysql-pool/lib/mysql-pool/pool.js:44:7)
at Object. (/home/comapq/Works/Nodejs/Codes/test-mysql-3.js:2:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
After a bit of research I found the problem. You probably installed mysql like this:
npm install mysql
This grabs the latest mysql package that is 2.0.0-alpha5(development phase, mostly untested). You need to install the latest stable version of mysql:
npm install mysql#0.9.6

Node.js + node-tds + Sql Server

I'm trying to follow the node-tds example on how to connect and retrieve data from Sql server with node.js http://cretz.github.com/node-tds/
I got no error on connect, but when I try to retrieve data I get the following:
"Error: Client must be in LOGGED IN state before executing sql
at TdsClient.sqlBatch (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds-client.js:101:13)
at Statement.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:256:37)
at Statement.execute (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:2:59)
at Object.<anonymous> (C:\Program Files (x86)\nodejs\edas\app.js:67:6)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)"
I'm logged in, so… I can't figure out what is happening.
You should execute your statement when the connection has already established, i.e.
var tds = require("tds");
var conn = new tds.Connection({
host: "localhost",
port: 1433,
userName: "user",
password: "secret",
database: "MyDb"
});
conn.connect(function(error) {
if (error != null) {
console.error("Received error", error);
} else {
console.log("Now connected, can start using");
}
});
conn.on("connect", function() {
var statement = conn.createStatement("SELECT ProductID, ProductName FROM Products");
statement.on("row", function(row) {
console.log("Received row: ", row.getValue(0));
});
statement.execute();
});

Resources