Tuples are a handy data type in Cassandra for more complex data structures. Tuples are similar to User Defined Types ( UDTs ) but their fields are not named and they are frozen by default. This simple example shows how to write and read tuples using the Node.js DataStax Driver and Cassandra.
Contributor(s): Jorge Bay Gondra, Andrew Tolbert - derived from here
Objectives
- Demonstrate how to insert and select tuples using the Node.js DataStax Driver with Cassandra.
Project Layout
- app.js - The main application file which contains all the logic to handle tuples
How this Sample Works
Inserting Tuples
Inserting tuples in NodeJs requires using the cassandra.types.Tuples
class and then passing that in as a parameter to the prepared statement as shown below:
// Create a new instance of a Tuple const currencies = new cassandra.types.Tuple('USD', 'EUR'); const query = 'INSERT INTO examples.tuple_forex (name, time, currencies, value) VALUES (?, ?, ?, ?)'; const params = [ 'market1', cassandra.types.TimeUuid.now(), currencies, new cassandra.types.BigDecimal(11, 1) ]; return client.execute(query, params, { prepare: true});
Note The { prepare: true}
is how to make a statement in Node.js into a prepared statement, see Prepared Statements in Node.js example for more details.
Retrieving Tuples
Tuples are retrieved in NodeJs as a Map of objects. This means that to access them you need to use the .get(index)
syntax as shown below:
const row = result.first(); console.log('%s to %s: %s', row['currencies'].get(0), row['currencies'].get(1), row['value']);
Setup and Running
Prerequisites
- NodeJs version 8
- A Cassandra, DDAC, DSE cluster or Apollo database ( docker is a nice option for local install - see docs )
Note This application assumes that the cluster is running locally and that the datacenter is named dc1
. If this is not the case then you will need to change this in app.js:
const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'dc1' });
Running
To run this application use the following command:
node app.js
This will produce the following output:
Inserting USD to EUR: 1.1