Cassandra 简明教程
Cassandra - Drop Table
Dropping a Table
您可以使用命令 Drop Table 删除表。其语法如下所示 −
You can drop a table using the command Drop Table. Its syntax is as follows −
Deleting a Table using Java API
您可以使用 Session 类的 execute() 方法删除表。按照以下步骤使用 Java API 删除表。
You can delete a table using the execute() method of Session class. Follow the steps given below to delete a table using Java API.
Step1: Create a Cluster Object
首先,实例化 com.datastax.driver.core 包的 Cluster.builder 类,如下所示 −
First of all, create an instance of Cluster.builder class of com.datastax.driver.core package as shown below −
//Creating Cluster.Builder object
Cluster.Builder builder1 = Cluster.builder();
使用 Cluster.Builder 对象的 addContactPoint() 方法添加一个接触点(节点的 IP 地址)。此方法返回 Cluster.Builder 。
Add a contact point (IP address of the node) using addContactPoint() method of Cluster.Builder object. This method returns Cluster.Builder.
//Adding contact point to the Cluster.Builder object
Cluster.Builder builder2 = build.addContactPoint( "127.0.0.1" );
使用新建的对象,创建一个集群对象。为此,在 Cluster.Builder 类中有一个名为 build() 的方法。以下代码显示如何创建一个集群对象。
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.
//Building a cluster
Cluster cluster = builder.build();
您可以使用一行代码构建一个群集对象,如下所示。
You can build a cluster object using a single line of code as shown below.
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
Step 2: Create a Session Object
如以下所示,使用 Cluster 类的 connect() 方法创建一个 Session 对象的实例。
Create an instance of Session object using the connect() method of Cluster class as shown below.
Session session = cluster.connect( );
此方法创建一个新会话并对其进行初始化。如果您已拥有键空间,则可以通过将 KeySpace 名称作为字符串格式传递给此方法来将其设置为现有键空间,如下所示。
This method creates a new session and initializes it. If you already 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.
Session session = cluster.connect(“Your keyspace name”);
在这里,我们使用名为 tp 的 keyspace。因此,如下所示,创建会话对象。
Here we are using the keyspace named tp. Therefore, create the session object as shown below.
Session session = cluster.connect(“tp”);
Step 3: Execute Query
可以使用 Session 类的 execute() 方法执行 CQL 查询。以字符串格式或作为 Statement 类对象将查询传递给 execute() 方法。以字符串格式传递给此方法的任何内容都将在 cqlsh 上执行。
You can execute CQL queries using 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.
在下面的示例中,我们正在删除一个名为 emp 的表。您必须将查询存储在字符串变量中,并将其传递给 execute() 方法,如下所示。
In the following example, we are deleting a table named emp. You have to store the query in a string variable and pass it to the execute() method as shown below.
// Query
String query = "DROP TABLE emp1;”;
session.execute(query);
以下是使用 Java API 在 Cassandra 中删除表的完整程序。
Given below is the complete program to drop a table in Cassandra using Java API.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
public class Drop_Table {
public static void main(String args[]){
//Query
String query = "DROP TABLE emp1;";
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
//Creating Session object
Session session = cluster.connect("tp");
//Executing the query
session.execute(query);
System.out.println("Table dropped");
}
}
使用类名保存以上的程序,后跟 .java,浏览到它被保存的位置。编译并执行程序,如下所示。
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.
$javac Drop_Table.java
$java Drop_Table
在正常条件下,它应该产生以下输出 −
Under normal conditions, it should produce the following output −
Table dropped