I'm using a NodeJS Application to connect to Cassandra using cassandra-driver
After playing around with cassandra-driver, I noticed that we have define schema for each table before-hand. Could anyone please help me understand if Schema of every table needs to be defined in Cassandra before-hand?
Here's how I'm trying to connect to Cassandra DB from my nodeJS application:
const cassandra = require("cassandra-driver");
const client = new cassandra.Client({
contactPoints: ['127.0.0.1'],
localDataCenter: 'datacenter1',
keyspace: 'testdb'
});
const executeCassandraQuery = async (query, params) => {
let fetchData;
try {
fetchData = await client.execute(query, params, { prepare: false });
console.log(`Successfully executed the query: ${JSON.stringify(fetchData)}`);
} catch (error) {
console.log(`There was an error executing the query: ${error}`)
}
return fetchData;
}
client.connect(async function (err, data) {
if (err) {
console.log(`There was an error connecting to Cassandra...${err}`)
}
else {
console.log(`Connected to Cassandra...${data}`);
const insertQuery = 'INSERT INTO testtable (id, firstname, lastname) VALUES(?,?,?)';
const insertQueryParams = [1, "Tony", "Mathew"];
const insertQueryResponse = await executeCassandraQuery(insertQuery, insertQueryParams);
console.log(`insertQueryResponse : ${insertQueryResponse}`);
const fetchQuery = 'SELECT * FROM employees';
}
});
Before running the above nodeJS script, I have created the 'testdb' keyspace and 'testtable' table using the following cql commands:
CREATE KEYSPACE testdb WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
CREATE TABLE testtable(id int PRIMARY KEY, name text);
No, it is not schema-less.
Related answer applies:
In Cassandra, how is it possible to save data in a column name while leaving the column value empty?
Relevant text:
No, not any more. This used to be possible. It required the old (pre-3.x storage engine) and use of a Thrift-based API. But tables built with CQL (and the new storage engine) require all columns to be defined-up front, and do not allow it at runtime (at least, not in the same way that Thrift did).