简体   繁体   中英

How do I properly set up the Cassandra client in Javascript?

Right now, I'm running a docker with Cassandra on it. I have a javascript file that sits outside the docker that needs to connect to Cassandra. I've found a node package that interfaces w/ JS, called cassandra-driver . However, with the following code:

var cassandra = require('cassandra-driver');
var PlainTextAuthProvider = cassandra.auth.PlainTextAuthProvider;
const client = new cassandra.Client({
    contactPoints: ['127.0.0.1:9042'],
    localDataCenter: '127.0.0.1',
    keyspace: 'wasabi_experiments',
    authProvider: new PlainTextAuthProvider('cassandra', 'cassandra')
});

I get

(node:17836) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): NoHostAvailableError: All host(s) tried for query failed. First host tried, 127.0.0.1:9042: ArgumentError: localDataCenter was configured as '127.0.0.1', but only found hosts in data centers: [datacenter1]. See innerErrors.
(node:17836) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): NoHostAvailableError: All host(s) tried for query failed. First host tried, 127.0.0.1:9042: ArgumentError: localDataCenter was configured as '127.0.0.1', but only found hosts in data centers: [datacenter1]. See innerErrors.

How can I get this to work?

Your problem is that you're using the 127.0.0.1 as value for localDataCenter parameter, but it should be set not to the address of the machine, but to the name of the Cassandra data center - in your case this is datacenter1 . Change the value of that parameter to datacenter1 , and it will start to work.

It would be:

const { Client, auth } = require('cassandra-driver');
const client = new cassandra.Client({
    contactPoints: ['127.0.0.1:9042'],
    localDataCenter: 'datacenter1', // here is the change required
    keyspace: 'wasabi_experiments',
    authProvider: new auth.PlainTextAuthProvider('cassandra', 'cassandra')
});

client.connect();

PS I recommend to read documentation for Node.js driver , and also "Developing applications with DataStax drivers" guide .

try first with a Cassandra client, ensure Cassandra is working properly and you can access it. After that try with the code. Also you can try to access the 127.0.0.1:9042 using telnet or netcat to see if the port is open and listening. You can use netstat too for this task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM