简体   繁体   English

如何在 Javascript 中正确设置 Cassandra 客户端?

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

Right now, I'm running a docker with Cassandra on it.现在,我正在运行一个带有 Cassandra 的 docker。 I have a javascript file that sits outside the docker that needs to connect to Cassandra.我有一个位于 docker 外部的 javascript 文件,需要连接到 Cassandra。 I've found a node package that interfaces w/ JS, called cassandra-driver .我找到了一个与 JS 接口的节点包,称为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 .您的问题是您使用127.0.0.1作为localDataCenter参数的值,但不应将其设置为机器的地址,而应设置为 Cassandra 数据中心的名称 - 在您的情况下,这是datacenter1 Change the value of that parameter to datacenter1 , and it will start to work.将该参数的值更改为datacenter1 ,它将开始工作。

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 . PS 我建议阅读Node.js 驱动程序的文档,以及“使用 DataStax 驱动程序开发应用程序”指南

try first with a Cassandra client, ensure Cassandra is working properly and you can access it.首先尝试使用 Cassandra 客户端,确保 Cassandra 正常工作并且您可以访问它。 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.您也可以尝试使用 telnet 或 netcat 访问 127.0.0.1:9042 以查看端口是否打开并正在侦听。 You can use netstat too for this task.您也可以将 netstat 用于此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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