You can create a table using the command CREATE TABLE. Given below is the syntax for creating a table.
You can define a column as shown below.
The primary key is a column that is used to exclusively identify a row. As a result, defining a primary key is compulsory while creating a table. A primary key is made of one or more columns of a table. You can define a primary key of a table as shown below.
Given below is an instance to create a table in Cassandra using cqlsh. Now we are:
It will have particulars such as employee name, id, city, salary, and phone number. Employee id is the primary key.
The select statement will give you the schema. Verify the table using the select statement as shown below.
Here you can observe the table created with the given columns. Since we have deleted the keyspace wisdomjobs, you will not find it in the keyspaces list.
You can create a table using the execute() method of Session class. Follow the steps given below to create a table using Java API.
First of all, create an instance of the Cluster.builder class of com.datastax.driver.core package as shown below.
Add a contact point (IP address of the node) using the addContactPoint() method of Cluster.Builder object. This method returns Cluster.Builder.
Using the new builder object, create a cluster object. To do so, you have a method called build() in the Cluster.Builder class. The following code shows how to create a cluster object.
You can build a cluster object using a single line of code as shown below.
Create an instance of Session object using the connect() method of Cluster class as shown below.
This method creates a new session and initializes it. If you previously have a keyspace, you can set it to the existing one by passing the keyspace name in string format to this method as shown below.
Here we are using the keyspace named tp. Therefore, create the session object as shown below.
You can execute CQL queries using the execute() method of Session class. Pass the query either in string format or as a Statement class object to the execute() method. Whatever you pass to this method in string format will be executed on the cqlsh.
In the next instance, we are creating a table named emp. You have to store the query in a string variable and pass it to the execute() method as shown below.
Given below is the complete program to create and use a keyspace in Cassandra using Java API.
Save the above program with the class name followed by .java, browse to the location where it is saved. Compile and execute the program as shown below.
Under normal conditions, it should produce the following output:
Apache Cassandra Related Tutorials
|Apache Tapestry Tutorial||Apache Solr Tutorial|
|Apache Storm Tutorial||Apache Hive Tutorial|
|Apache Pig Tutorial||Apache Flume Tutorial|
|Apache Kafka Tutorial||Apache Ant Tutorial|
|Apache Tajo Tutorial||Apache Impala Tutorial|
|Apache Presto Tutorial||Apache POI PPT Tutorial|
|Apache Struts 2 Tutorial|
Apache Cassandra Related Interview Questions
|Apache Tapestry Interview Questions||Apache Cassandra Interview Questions|
|Apache Spark Interview Questions||Apache Solr Interview Questions|
|Apache Storm Interview Questions||Apache Hive Interview Questions|
|Apache Pig Interview Questions||Apache Flume Interview Questions|
|Apache Kafka Interview Questions||Apache Ant Interview Questions|
|Apache Camel Interview Questions||Apache Tajo Interview Questions|
|Apache Impala Interview Questions||Apache Drill Interview Questions|
|Apache Presto Interview Questions||Apache POI PPT Interview Questions|
|Apache Struts 2 Interview Questions||Apache Ambari Interview Questions|
Apache Cassandra Related Practice Tests
|Apache HBase Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.