Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/23/2020

Reading time:2 min

DataStax-Examples/switch-connection-nodejs

by DataStax-Examples

This application shows how to use the Node.js DataStax Driver to connect to an on-prem Cassandra database or a Astra database in the cloud at runtime using environment variables.Contributor(s): Dave BechbergerObjectivesShows the differences between a Cassandra connection configuration and an Astra connection configuration in a single app.Provide a demonstration of how to configure the database connection at runtime.See the documentation for more details about the Astra connection configuration for the Node.js DriverProject Layoutapp.js - The main application file which contains all the logic to switch between the configurationsHow this Sample WorksThis sample uses environment variables to specify the configuration parameters and whether to use a Cassandra configuration or an Astra configuration. All the logic to switch between the configurations occurs in the getClientConfiguration function.If you specify the USEASTRA environment variable:The environment variables are checked to see that DBUSERNAME, DBPASSWORD, SECURECONNECTBUNDLEPATH, and KEYSPACE existIf they exist then the Apollo connection is createdIf they do not exist then an error is thrownIf you so not specify the USEASTRA environment variable:The environment variables are checked to see that CONTACTPOINTS and DATACENTER existIf they exist then the standard connection is createdIf they do not exist then an error is thrownIf DBUSERNAME and DBPASSWORD exist then credentials are added to the configurationIf KEYSPACE exists then it is added to the configurationWhile these criteria added to the configuration are commonly used configuration parameters you are able to specify any additional ones in the code.Setup and RunningPrerequisitesNode 4 and aboveA Cassandra cluster or an Astra database to connect to with the appropriate connection informationAdditionally while this example uses environment variables to control which configuration is selected this could also be done via the use of configuration files or command line parameters.For clarity this sample does not contain any of the normal error handling process you would want to wrap around connecting to a cluster to handle likely errors that would occur.RunningTo connect to an Apollo database you first need to download the secure connect bundle following the instructions found here.Once you have this information you can run this to connect to Apollo by using the command below, with the appropriate configuration added:USEASTRA=true DBUSERNAME=XXX DBPASSWORD=XXX KEYSPACE=XXX SECURECONNECTBUNDLEPATH="/valid/path/to/secureconnectbundle.zip" node app.jsIf you would like to connect to a Cassandra cluster use the command below, with the appropriate configuration added:CONTACTPOINTS=XX.XX.XX.XX DATACENTER=XXXX node app.jsOnce you run this against either option you will get output that specifies the number of hosts and their IP addresses printed to the console:Connected to cluster with 3 host(s) ["XX.XX.XX.136:9042","XX.XX.XX.137:9042","XX.XX.XX.138:9042"]

Illustration Image

This application shows how to use the Node.js DataStax Driver to connect to an on-prem Cassandra database or a Astra database in the cloud at runtime using environment variables.

Contributor(s): Dave Bechberger

Objectives

  • Shows the differences between a Cassandra connection configuration and an Astra connection configuration in a single app.
  • Provide a demonstration of how to configure the database connection at runtime.
  • See the documentation for more details about the Astra connection configuration for the Node.js Driver

Project Layout

  • app.js - The main application file which contains all the logic to switch between the configurations

How this Sample Works

This sample uses environment variables to specify the configuration parameters and whether to use a Cassandra configuration or an Astra configuration. All the logic to switch between the configurations occurs in the getClientConfiguration function.

  • If you specify the USEASTRA environment variable:
    • The environment variables are checked to see that DBUSERNAME, DBPASSWORD, SECURECONNECTBUNDLEPATH, and KEYSPACE exist
      • If they exist then the Apollo connection is created
      • If they do not exist then an error is thrown
  • If you so not specify the USEASTRA environment variable:
    • The environment variables are checked to see that CONTACTPOINTS and DATACENTER exist
      • If they exist then the standard connection is created
      • If they do not exist then an error is thrown
      • If DBUSERNAME and DBPASSWORD exist then credentials are added to the configuration
      • If KEYSPACE exists then it is added to the configuration

While these criteria added to the configuration are commonly used configuration parameters you are able to specify any additional ones in the code.

Setup and Running

Prerequisites

  • Node 4 and above
  • A Cassandra cluster or an Astra database to connect to with the appropriate connection information

Additionally while this example uses environment variables to control which configuration is selected this could also be done via the use of configuration files or command line parameters.

For clarity this sample does not contain any of the normal error handling process you would want to wrap around connecting to a cluster to handle likely errors that would occur.

Running

To connect to an Apollo database you first need to download the secure connect bundle following the instructions found here.

Once you have this information you can run this to connect to Apollo by using the command below, with the appropriate configuration added:

USEASTRA=true DBUSERNAME=XXX DBPASSWORD=XXX KEYSPACE=XXX SECURECONNECTBUNDLEPATH="/valid/path/to/secureconnectbundle.zip" node app.js

If you would like to connect to a Cassandra cluster use the command below, with the appropriate configuration added:

CONTACTPOINTS=XX.XX.XX.XX DATACENTER=XXXX node app.js

Once you run this against either option you will get output that specifies the number of hosts and their IP addresses printed to the console:

Connected to cluster with 3 host(s) ["XX.XX.XX.136:9042","XX.XX.XX.137:9042","XX.XX.XX.138:9042"]

Related Articles

spring
rest
api

GitHub - DataStax-Examples/spring-data-starter: ⚡️ A sample Spring Data Cassandra REST API

John Doe

2/14/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

java