lb3vh1jj

lb3vh1jj1#

看看Cassandra Node.js driver
下面是一个示例代码,向您展示如何连接到Amazon Keyspaces:

const cassandra = require('cassandra-driver');
const fs = require('fs');
const auth = new cassandra.auth.PlainTextAuthProvider('ServiceUserName', 'ServicePassword');
const sslOptions1 = {
    ca: [
        fs.readFileSync('path_to_file/sf-class2-root.crt', 'utf-8')],      
            host: 'cassandra.us-west-2.amazonaws.com',
                rejectUnauthorized: true
        };
const client = new cassandra.Client({
    contactPoints: ['cassandra.us-west-2.amazonaws.com'],
        localDataCenter: 'us-west-2',
        authProvider: auth,
        sslOptions: sslOptions1,
        protocolOptions: { port: 9142 }
    });

const query = 'SELECT * FROM system_schema.keyspaces';
 
client.execute(query)
    .then( result => console.log('Row from Keyspaces %s', result.rows[0]))
    .catch( e=> console.log(`${e}`));

您需要下载数据库的证书。请参阅full instructions here
如果你感兴趣,我们有免费的教程,介绍如何在datastax.com/dev上为Cassandra构建应用程序,其中包括用于Javascript和其他语言的example apps + full working code。干杯!

相关问题