Apache Cassandra

Cassandra Arithmetic Operators

Operators are very important when working with various data types. Using operators, you can carry out various operations in simple steps. For example, you can use addition operator to calculate the total of various columns etc.

In this post, we will discuss the various arithmetic operators supported in Cassandra and CQL version 3.x and above.

Cassandra Supported Arithmetic Operators

Apache Cassandra supports the following arithmetic operators:

  1. Addition – uses the plus (+) symbol for addition operations.
  2. Subtraction – uses the minus (-) for subtraction and unary negation.
  3. Multiplication – uses an asterisk (*) symbol for multiplication operations.
  4. Division – uses a forward slash (/) symbol for division operations.
  5. Modulo – uses the percentage (%) symbol for remainder division operations.

Keep in mind that operators has a higher and lower precedence level. For example, a division and modulo operator has a higher precedence than addition and subtraction operator.

Cassandra Operator Return Values

The following table shows the return value of various operands on left/right operands:

Example

The example below shows how to use the Cassandra arithmetic operators:

cqlsh> create keyspace height_info
   ... with replication = {
   ... 'class': 'SimpleStrategy',
   ... 'replication_factor': 1};
cqlsh> USE height_info;
cqlsh:height_info> CREATE TABLE recipients(
               ... id int,
               ... username text,
               ... height int,
               ... PRIMARY KEY(id, height));

Insert sample data as:

cqlsh:height_info> INSERT INTO recipients(id, username, height) values (0, 'user1', 210);
cqlsh:height_info> INSERT INTO recipients(id, username, height) values (1, 'user2', 115);
cqlsh:height_info> INSERT INTO recipients(id, username, height) values (2, 'user3', 202);
cqlsh:height_info> INSERT INTO recipients(id, username, height) values (3, 'user4', 212);
cqlsh:height_info> INSERT INTO recipients(id, username, height) values (4, 'user5', 216);

Finally, verify results exists in the table.

cqlsh:height_info> SELECT * FROM recipients;

Output:

id | height | username
----+--------+----------
  1 |    115 |    user2
  0 |    210 |    user1
  2 |    202 |    user3
  4 |    216 |    user5
  3 |    212 |    user4
(5 rows)

Calculate Average Height.

cqlsh:height_info> SELECT sum(height) / 5 FROM recipients;

In this example, we use the sum aggregate function to determine the total of all heights. We then use the division operator to find the mean of the heights.

The query above should return an output as shown:

system.sum(height) / 5
------------------------
                    191
(1 rows)

Conclusion

In this post, we discussed the various types of arithmetic operators supported by the latest Apache Cassandra version.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list