How do I connect to multiple keyspaces using one Cassandra Client object?
Asked Answered
S

2

1

I was using such a following code to connect to only 1 keyspace:

This is `/config/db.ts:

import cassandra from "cassandra-driver";

class Cpool {
static _pool : cassandra.Client;

static connect(options : any){
  this._pool = new cassandra.Client(options);
}


  static execute(query: string, params?: cassandra.ArrayOrObject | undefined, options?: cassandra.QueryOptions | undefined)
  {
    return this._pool.execute(query, params, options);
  }
}


export { Cpool };

And the below code is the index.ts:

  const default_options = {
    contactPoints: [process.env.CONTACT_POINTS],
    localDataCenter: process.env.LOCAL_DATA_CENTER,
    keyspace: process.env.KEYSPACE,
  };

  try {
    await Cpool.connect(default_options);
    console.log("Connected to Cassandra");
  } catch (err) {
    console.log(err);
  }

Now I want to have 2 or more keyspaces but don't know should I create a new client in order to each of them or I can use only 1 client to connect to multiple keyspaces? How?

Surber answered 10/2, 2023 at 2:19 Comment(0)
S
0

I found that it's not necessary to specify keyspace property while generating a Cassandra client and connect it to the Cassandra Database, so I changed:

  const default_options = {
    contactPoints: [process.env.CONTACT_POINTS],
    localDataCenter: process.env.LOCAL_DATA_CENTER,
    keyspace: process.env.KEYSPACE,
  };

To:

  const default_options = {
    contactPoints: [process.env.CONTACT_POINTS],
    localDataCenter: process.env.LOCAL_DATA_CENTER,
  };

And I specified keyspaces inside my queries instead, like INSERT INTO keyspace1.table_name ... in place of INSERT INTO table_name ....

Surber answered 11/2, 2023 at 19:45 Comment(0)
R
1

When you have multiple keyspaces in your database, you will need to fully qualify the tables in your query by explicitly specifying the keyspace name. For example:

query = "SELECT ... FROM keyspace_name.table_name WHERE ..."

or:

query = "INSERT INTO keyspace_name.table_name ... VALUES ..."

For best practice, only use one Client instance and reuse it for the life of your application. Cheers!

Righthanded answered 10/2, 2023 at 6:52 Comment(3)
Thank you but what can I do with const default_options = { contactPoints: [process.env.CONTACT_POINTS], localDataCenter: process.env.LOCAL_DATA_CENTER, keyspace: process.env.KEYSPACE, }; When I should specify the keyspace while creating a client.Surber
I use default_options to create a client like await Cpool.connect(default_options); and inside the defailt_options I am specifying the keyspace. How can I specify multiple keyspaces there?Surber
You don't specify multiple keyspaces when you create the Client object -- you specify the keyspace with the table in your queries. Cheers!Righthanded
S
0

I found that it's not necessary to specify keyspace property while generating a Cassandra client and connect it to the Cassandra Database, so I changed:

  const default_options = {
    contactPoints: [process.env.CONTACT_POINTS],
    localDataCenter: process.env.LOCAL_DATA_CENTER,
    keyspace: process.env.KEYSPACE,
  };

To:

  const default_options = {
    contactPoints: [process.env.CONTACT_POINTS],
    localDataCenter: process.env.LOCAL_DATA_CENTER,
  };

And I specified keyspaces inside my queries instead, like INSERT INTO keyspace1.table_name ... in place of INSERT INTO table_name ....

Surber answered 11/2, 2023 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.