Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/23/2020

Reading time:1 min

DataStax-Examples/tuples-nodejs

by DataStax-Examples

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 hereObjectivesDemonstrate how to insert and select tuples using the Node.js DataStax Driver with Cassandra.Project Layoutapp.js - The main application file which contains all the logic to handle tuplesHow this Sample WorksInserting TuplesInserting 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 Tupleconst 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 TuplesTuples 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 RunningPrerequisitesNodeJs version 8A 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' });RunningTo run this application use the following command:node app.jsThis will produce the following output:InsertingUSD to EUR: 1.1

Illustration Image

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

Related Articles

api.management
node
api

LoopBack

John Doe

3/7/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

node