The Wayback Machine - https://web.archive.org/web/20240819224159/https://www.geeksforgeeks.org/inserting-json-data-into-a-table-in-cassandra/
Open In App

Inserting JSON data into a table in Cassandra

Last Updated : 02 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, you will be able to understand how you can insert JSON data into a table in Cassandra and will discuss with the help of an example and then finally conclude the importance of JSON insertion. Let’s discuss it one by one.

Overview :
It is a practical way than cqlsh to insert column and column values. In JSON values inserted in the form of string if they are not a number for example id with datatype uuid inserted as a string but will be stored as uuid. For better understanding now, first, we will see insertion data using cqlsh commands and then will discuss how you can insert data by using JSON format. 

Example :
Let’s consider you have existing keyspace namely cluster1 and then first we will create a user_data table by using the CQL command as follows.

use cluster1;
create table user_record
(
user_id uuid,
first_name varchar,
last_name varchar,
company varchar,
primary key(user_id)
);

Method-1 : 
Insertion by using cqlsh commands –

insert into user_record(user_id, first_name, last_name, company) 
values(101aa90a-4bba-211f-a4fb-00001a101cda,'Ashish','Rana','abc');

insert into user_record(user_id, first_name, last_name, company)
values(102aa90a-4bba-211f-a4fb-00002a102cda,'Ayush','NA','abc');

After insertion, you can use the following cql command to verify the inserted data.

select * from user_record;

Output –

user_id company first_name last_name
102aa90a-4bba-211f-a4fb-00002a102cda abc  Ayush NA
101aa90a-4bba-211f-a4fb-00001a101cda abc Ashish Rana

Method-2 :
Insertion by using JSON format –
To insert data in JSON format, JSON keyword will be added to the INSERT command as follows. 

 INSERT INTO cluster1.user_record JSON '{
"user_id" : "103aa90a-4bba-211f-a4fb-00001a101cda",
"first_name" : "Ashish",
"last_name" : "Rana",
"company" : "abc" }';

 Using JSON format if you do not insert any value for any column then a null value will be entered automatically as you can see in the below given example.

 INSERT INTO cluster1.user_record JSON '{
"user_id" : "104aa90a-4bba-211f-a4fb-00001a101cda",
"first_name" : "Ashish",
"last_name" : "Rana"
}';

After insertion, you can use the following cql command to verify the inserted data.

select * from user_record;

Output –

user_id company first_name last_name
104aa90a-4bba-211f-a4fb-00001a101cda null Ashish Rana
103aa90a-4bba-211f-a4fb-00001a101cda  abc   Ashish  Rana
102aa90a-4bba-211f-a4fb-00002a102cda abc  Ayush NA
101aa90a-4bba-211f-a4fb-00001a101cda abc Ashish Rana


Similar Reads

Inserting data using a CSV file in Cassandra
In this article, we will discuss how you can insert data into the table using a CSV file. And we will also cover the implementation with the help of examples. Let's discuss it one by one. Pre-requisite - Introduction to Cassandra Introduction :If you want to store data in bulk then inserting data from a CSV file is one of the nice ways. If you have
2 min read
Altering a table to add a collection data type in Cassandra
In this article, we will discuss how we can alter a table to add MAP collection data type and how we insert data after altering the table with the help of the UPDATE command. Let’s discuss one by one. First, we are going to create a table let’s consider E_book is a table name and Bookk_name, Author_name, Selling_price are the fields in the E_book t
2 min read
JSON format in Cassandra
In this article, we will discuss JSON JavaScript Object Notation format to insert data into the table and it is useful and inserting columns and its values programmatically are more practical than using cqlsh. For testing and verifying queries cqlsh is very convenient. Cassandra Query Language (CQL) supported JSON format in which data is inserted a
2 min read
Operations on table in Cassandra
In this article, we will discuss table operations like Create insert truncate drop in Cassandra with some sample exercise. Let’s have a look. Creating a table - Register: First, we are going to create a table namely as Register in which we have id, name, email, city are the fields. Let’s have a look. Create table Register ( id uuid primary key, nam
3 min read
Creating a table in Cassandra
In this article, we are going to discuss how we can create a table in Cassandra and also discuss column definitions, keys role, Table options (compact storage and clustering order), etc. In Cassandra, the CQL table has a name and it stores rows. when you create a table, you define the columns for the rows, a mandatory primary key to identify each r
3 min read
Table Partitioning in Cassandra
In this article, we are going to cover how we can our data access on the basis of partitioning and how we can store our data uniquely in a cluster. Let's discuss one by one. Pre-requisite — Data Distribution Table Partitioning : In table partitioning, data can be distributed on the basis of the partition key. If you did not specify any partitioning
2 min read
Creating Materialized View using Table definition in Cassandra
In this article, we are going to discuss how we can create a materialized view based on a particular problem and also cover the example for the same. Let's discuss one by one. Pre-requisite - Materialized view In this, you will see how you can create the materialized view based on table definition. Let's understand this by using the following steps
2 min read
Overview of Data modeling in Apache Cassandra
In this article we will learn about these three data model in Cassandra: Conceptual, Logical, and Physical. Learning Objective: To Build database using quick design techniques in Cassandra. To Improve existing model using a query driven methodology in Cassandra. To Optimize Existing model via analysis and validation techniques in Cassandra. Data mo
3 min read
Collection Data Type in Apache Cassandra
Collection Data Type in Cassandra In this article, we will describe the collection data type overview and Cassandra Query Language (CQL) query of each collection data type with an explanation. There are 3 types of collection data type in Cassandra. 1. SET 2. LIST 3. MAP Let discuss one by one. 1. SET: A Set is a collection data where we can store a
3 min read
Pre-defined data type in Apache Cassandra
Prerequisite - User Defined Type (UDT) in Cassandra In this article, we will discuss different types of data types in Cassandra which is used for various purpose in Cassandra such that in data modeling, to create a table, etc. Basically, there are 3 types of data type in Cassandra. Lets have a look. Figure - Data Types in Cassandra Now, here we are
4 min read
Data Manipulation in Cassandra
In this article, we will describe the following DML commands in Cassandra which help us to insert, update, delete, query data, etc. In CQL there are following data manipulation command. Let’s discuss one by one. 1. Insert 2. Update 3. Delete 4. Batch Let’s take an example: Table Name: Employee_info CREATE TABLE Employee_info ( E_id int, E_name text
3 min read
Data Distribution in Cassandra
In this article we will discussed Data distribution in Cassandra and how data distributes over cluster. So, let's have a look. In Cassandra data distribution and replication go together. In Cassandra distribution and replication depending on the three thing such that partition key, key value and Token range. Cassandra Table: In this table there are
2 min read
Updating MAP collection data type in Cassandra
In this article, we will discuss how we can update the MAP Collection Data Type and how we can update rows, how we can remove rows and how we can add rows by using the UPDATE clause for updating the map collection data types. First, we will create a table let’s consider Food_menu is a table name and Order_id, Order_Date, Oreder_cost, and Menu_items
3 min read
Data Backup and Restoration in Cassandra
In this article, we will discuss how we can Backup and Restore our data and also discuss how many ways we can restore our data in Cassandra. We can restore our data by using a snapshot and by using sstableloader utility and by using a nodetool refresh. Let's discuss one by one. First, we are going to create keyspace for backup the data. Let's discu
3 min read
Export and Import data in Cassandra
Prerequisite - Cassandra In this article, we are going to discuss how we can export and import data through cqlsh query. Let's discuss one by one. First, we are going to create table namely as Data in which id, firstname, lastname are the fields for sample exercise. Let's have a look. Table name: Data CREATE TABLE Data ( id UUID PRIMARY KEY, firstn
3 min read
Django ORM - Inserting, Updating & Deleting Data
Django lets us interact with its database models, i.e. add, delete, modify, and query objects, using a database-abstraction API called ORM(Object Relational Mapper). This article discusses all the functional operations we can perform using Django ORM. Prerequisite: Django models Django ORM Django's Object-Relational Mapping (ORM) system, a fundamen
3 min read
Static type using batch in Cassandra
Prerequisite - Cassandra In this article, we will discuss the static column using the BATCH statement and how it works with the BATCH statement. Static column can be used for conditional update using a batch statement. Let’s have a look. Let's consider bank_emp_details is a table name and Id, Name, Bank_name, E_start_date and E_end_date are the fie
2 min read
High Availability Mechanism in Cassandra
In this article we will learn about mechanism of high availability in Cassandra by using the following key terminology. 1. Seed Node 2. Gossip Communication Protocol 3. Failure Detection 4. Hinted Handoff lets discuss one by one. 1. Seed Node: In Apache Cassandra it is first nodes to start in cluster. If we want to Configured seed node, then we can
5 min read
Apache Cassandra tools
Prerequisites - Introduction to Apache Cassandra Apache Cassandra (NOSQL database) Architecture of Apache Cassandra In this article, we are going to discuss the tools of Apache Cassandra which help to perform in various aspects of tasks such that the status of the node, the status of the ring, back up and restore data, etc. The CQL shell (cqlsh) -
3 min read
Blob conversion function in Cassandra
In Cassandra, Blob data type represents constant hexadecimal data type and the number is defined as 0[xX](hex)+ where hex means hexadecimal. It is useful to store a short string or small images. In this article we will discuss Blob conversion function which converts the native types into binary data (blob): typeAsBlob(value) blobAsType(value) Let's
1 min read
Designing models in Cassandra
Prerequisite - Introduction to Cassandra, Overview of Data Modelling In this article, we are going to discuss how we can Designing the model in Cassandra. Modelling design is one of the key part for any application. So, let's discuss how we can make a better model for any application with an example we will see how we can do that. As you will see C
4 min read
Architecture of Apache Cassandra
Avinash Lakshman and Prashant Malik initially developed Cassandra at Facebook to power the Facebook inbox search feature. Facebook released Cassandra as an open source project on google code in July 2008. It became an Apache incubator project in March 2009. It became one of the top level project in 17 Feb 2010. Fueled by the internet revolution, mo
5 min read
Overview of User Defined Type (UDT) in Cassandra
User Defined Data Type: One of the advantages of User Defined Data(UDT) is to Attach multiple data fields to a column. In Cassandra, UDTs play a vital role which allows group related fields (such that field 1, field 2, etc.) can be of data together and are named and type. In Cassandra one of the advantage of UDTs which helps to add flexibility to y
4 min read
Concept of indexing in Apache Cassandra
Prerequisite - Introduction to Apache Cassandra Index: As we can access data using attributes which having the partition key. For Example, if Emp_id is a column name for Employee table and if it is partition key of that table then we can filter or search data with the help of partition key. In this case we can used WHERE clause to define condition
5 min read
Monitoring cluster in Cassandra
The tools for monitoring Cassandra cluster include nodetool, Jconsole and Opscenter. All these tools for monitoring cluster works by communicating through JMX(Java Management Extension). In Cassandra Through JMX, explores many metrics and commands which any of them. These tools can use to monitor and manage a Cassandra cluster. In this article we w
3 min read
Virtual machine installation for Cassandra CQL query
In this article we will discuss about virtual machine installation for doing exercise of Cassandra and can execute CQL query step by step. follow the step given below. Step-1: First download and install any one of virtual machine. here we will discuss about oracle Virtual Box. Download VirtualBox Figure - Oracle VirtualBox Step-2: After installing
2 min read
CAST function in Cassandra
CAST function helps in Converts data from one data type to another data type in Cassandra. In Cassandra CAST function Supported in SELECT statements. lets have a look how we can used CAST function in select statement. SELECT CAST([fieldname] AS [data type]) FROM [table name] Basic function of CAST: It Converts From any native data type to text data
3 min read
Aggregate functions in Cassandra
In this article, we will discuss Aggregate functions in Cassandra which can be used for various purposes such that to count, for finding min and max, sum, etc. Aggregate functions: 1. Count 2. Max and Min 3. Sum 4. Avg In Cassandra, these aggregate functions are pre-defined or in-built functions. Aggregate functions in Cassandra work on a set of ro
3 min read
Database Roles in CQL (Cassandra Query Language)
Cassandra Query Language (CQL) is the query language used to interact with Cassandra databases. Unlike traditional relational databases, Cassandra does not have a built-in concept of database roles. Instead, access control is managed at the object level, such as the keyspace, table, or column level. Cassandra provides a set of permissions that can
5 min read
Static type in Cassandra
In this article, we will discuss a static column in which we will able to understand how the static column is useful and what is the actual role. Let’s have a look. Static column: In Cassandra Query Language (CQL) it is a special column that is shared by all the rows of a partition. the static column is very useful when we want to share a column wi
2 min read
Article Tags :
three90RightbarBannerImg