question

mukheja.nitin avatar image
mukheja.nitin asked Erick Ramirez commented

How does an application connect to Cassandra using the driver?

I have seen example of cassandra driver where cluster name or keyspace name is specified without specifying the host ip address.

If i have multi node cluster how does the connection string work in cassandra.

cassandradriver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

In its simplest form, you configure the driver with the "contact points" which are the list of nodes it can contact to discover the topology of the cluster. These contact points are similar to the way the seeds list work for the gossip protocol.

There are several drivers that DataStax maintain:

  • Java driver
  • Python driver
  • Node.js driver
  • C++ driver
  • C# driver

Note that this isn't the complete list of drivers for Cassandra. There are drivers maintained by other Cassandra enthusiasts in the community.

Let's use the Java driver as an example since it is the most popular. Here is a simple bit of code that creates a session to the cluster:

CqlSession session = CqlSession.builder()
   .addContactPoint(new InetSocketAddress("10.1.2.3", 9042))
   .withKeyspace("communityks")
   .withLocalDatacenter("DC1")
   .build();

Note the method .addContactPoint() which identifies one of the nodes in the cluster as a contact point. Now that we've created a session object, we can execute queries against the database without needing to specify the contact points again. For example, we can insert data into a table with:

session.execute(
    SimpleStatement.builder( "INSERT INTO communityks.users (name, address) VALUES (?,?,?)")
        .addPositionalValues("tom", "100 Main Rd")
        .build());

If you're interested, we have a Katacoda scenario for the Java Driver where you can quickly learn the basics of writing a Java app that writes to and reads from a Cassandra database. Try it out for yourself now. It only takes about 10 minutes.

Once you complete the scenario, it would be great if you complete the questionnaire at the end to provide us feedback as we would love to hear from you. Cheers!

3 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

mukheja.nitin avatar image mukheja.nitin commented ·

Thank you eric for detailed explanation...

0 Likes 0 ·
Erick Ramirez avatar image Erick Ramirez ♦♦ mukheja.nitin commented ·

No worries. Cheers!

0 Likes 0 ·

[Follow up question posted in #7702]

0 Likes 0 ·