May 17, 2021 Cassandra
You can use the command INSERT to insert data into the columns of rows in the table. The syntax for creating data in a table is given below.
INSERT INTO <tablename> (<column1 name>, <column2 name>....) VALUES (<value1>, <value2>....) USING <option>
Let's assume that there is a table called emp (emp_id,emp_name,emp_city,emp_phone,emp_sal) and that the following data must be inserted into the emp table.
emp_id | emp_name | emp_city | emp_phone | emp_sal |
---|---|---|---|---|
1 | Ram | Hyderabad | 9848022338 | 50000 |
2 | robin | Hyderabad | 9848022339 | 40000 |
3 | rahman | Chennai | 9848022330 | 45000 |
Fill the table with the required data using the commands given below.
cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(1,'ram', 'Hyderabad', 9848022338, 50000); cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(2,'robin', 'Hyderabad', 9848022339, 40000); cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(3,'rahman', 'Chennai', 9848022330, 45000);
After the data is inserted, use the SELECT statement to verify that the data has been inserted. If you use the SELECT statement to validate the emp table, it will give you the following output.
cqlsh:tutorialspoint> SELECT * FROM emp; emp_id | emp_city | emp_name | emp_phone | emp_sal --------+-----------+----------+------------+--------- 1 | Hyderabad | ram | 9848022338 | 50000 2 | Hyderabad | robin | 9848022339 | 40000 3 | Chennai | rahman | 9848022330 | 45000 (3 rows)
Here you can observe the table populated with the data we inserted.
You can create data in a table using the execute() method of the Session class. Follow these steps to create data in a table using the java API.
Create an instance of the Cluster.builder class called com.datastax.driver.core, as shown below.
//Creating Cluster.Builder object Cluster.Builder builder1 = Cluster.builder();
Use the addContactPoint() method of the Cluster.Builder object to add a contact point (the IP address of the node). This method returns Cluster.Builder.
//Adding contact point to the Cluster.Builder object Cluster.Builder builder2 = build.addContactPoint("127.0.0.1");
Create a cluster object with the new builder object. T o do this, there is a method called build() in the Cluster.Builder class. The following code shows how to create cluster objects.
//Building a cluster Cluster cluster = builder.build();
You can use a single line of code to build cluster objects, as shown below.
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
Create an instance of a Session object using the Connect() method of the Cluster class, as shown below.
Session session = cluster.connect( );
This method creates a new session and initializes it. If you already have a key space, you can do so by formatting the KeySpace name as an existing key space in string format, as shown below.
Session session = cluster.connect(“ Your keyspace name ” );
Here we use KeySpace called tp. Therefore, the session object is created as follows.
Session session = cluster.connect(“ tp” );
You can use the Execute() method of the Session class to perform CQL queries. P ass the query to the execute() method in string format or state class objects. Whether you pass this method in string format will execute on cqlsh.
In the following example, we insert data into the emp table. You must store the query in a string variable and pass it to the execute() method, as shown below.
String query1 = “INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(1,'ram', 'Hyderabad', 9848022338, 50000);” ; String query2 = “INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(2,'robin', 'Hyderabad', 9848022339, 40000);” ; String query3 = “INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal) VALUES(3,'rahman', 'Chennai', 9848022330, 45000);” ; session.execute(query1); session.execute(query2); session.execute(query3);
The complete program for inserting data into the Cassandra table using the Java API is shown below.
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Session; public class Create_Data { public static void main(String args[]){ //queries String query1 = "INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal)" + " VALUES(1,'ram', 'Hyderabad', 9848022338, 50000);" ; String query2 = "INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal)" + " VALUES(2,'robin', 'Hyderabad', 9848022339, 40000);" ; String query3 = "INSERT INTO emp (emp_id, emp_name, emp_city, emp_phone, emp_sal)" + " VALUES(3,'rahman', 'Chennai', 9848022330, 45000);" ; //Creating Cluster object Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); //Creating Session object Session session = cluster.connect("tp"); //Executing the query session.execute(query1); session.execute(query2); session.execute(query3); System.out.println("Data created"); } }
Use the class name and .java save the above program and browse to the save location. Compile and execute the program as shown in the following image.
$javac Create_Data.java $java Create_Data
Under normal conditions, it should produce the following output:
Data created