Last active
May 25, 2024 00:49
-
-
Save diegofcornejo/4a1bd6893a7f44b358a1ce0cdb8dca00 to your computer and use it in GitHub Desktop.
Cassandra - NodeJS example (datastax/nodejs-driver)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cassandra = require('cassandra-driver'); | |
const CASSANDRA_HOST = process.env.CASSANDRA_HOST || 'localhost'; | |
const CASSANDRA_NODES = [`${CASSANDRA_HOST}:9042`, `${CASSANDRA_HOST}:9043`, `${CASSANDRA_HOST}:9044`] | |
const CASSANDRA_USER = process.env.CASSANDRA_USER || 'cassandra'; | |
const CASSANDRA_PASSWORD = process.env.CASSANDRA_PASSWORD || 'cassandra'; | |
const CASSANDRA_DATA_CENTER = process.env.CASSANDRA_DATA_CENTER || 'datacenter1'; | |
const KEYSPACE = 'mykeyspace'; | |
const TABLE = 'event'; | |
const QUERY_LIMIT = 50000; | |
const FETCH_SIZE = 5000; | |
// Configuración del clúster de Cassandra con autenticación | |
const client = new cassandra.Client({ | |
contactPoints: CASSANDRA_NODES, | |
localDataCenter: CASSANDRA_DATA_CENTER, | |
keyspace: KEYSPACE, | |
authProvider: new cassandra.auth.PlainTextAuthProvider(CASSANDRA_USER, CASSANDRA_PASSWORD) | |
}); | |
async function queryCassandra() { | |
try { | |
// Connect to the cluster | |
await client.connect(); | |
const totalStartTime = process.hrtime(); | |
//Set the fetch size in the options | |
const options = { prepare: true, fetchSize: FETCH_SIZE }; | |
// Query to retrieve all rows from the table | |
const query = `SELECT * FROM ${TABLE} LIMIT ${QUERY_LIMIT}`; | |
let pageState; | |
let totalRows = 0; | |
do { | |
const startTime = process.hrtime(); | |
const result = await client.execute(query, [], { ...options, pageState }); | |
const elapsedTime = process.hrtime(startTime); | |
const elapsedTimeInMs = (elapsedTime[0] * 1000) + (elapsedTime[1] / 1000000); | |
console.log(`Query executed in ${elapsedTimeInMs.toFixed(3)} ms`); | |
console.log(`Number of rows retrieved: ${result.rowLength}`); | |
totalRows += result.rowLength; | |
pageState = result.pageState; | |
} while (pageState); | |
const totalElapsedTime = process.hrtime(totalStartTime); | |
const totalElapsedTimeInMs = (totalElapsedTime[0] * 1000) + (totalElapsedTime[1] / 1000000); | |
console.log(`Total query executed in ${totalElapsedTimeInMs.toFixed(3)} ms`); | |
console.log(`Total number of rows retrieved: ${totalRows}`); | |
await client.shutdown(); | |
} catch (error) { | |
console.error('Error executing query:', error); | |
await client.shutdown(); | |
} | |
} | |
queryCassandra(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CASSANDRA_HOST="db01.cloud.myserver.com" | |
CASSANDRA_USER="cassandra" | |
CASSANDRA_PASSWORD="cassandra" | |
CASSANDRA_DATA_CENTER="datacenter1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment