2017-05-11 83 views
1
var keyspace = "mydb"; 
var datacentersReplicationFactors = new Dictionary<string, int>(0); 
var replication = ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(datacentersReplicationFactors); 

using (var cluster = Cluster.Builder().AddContactPoints("my_ip").Build()) 
using (var session = cluster.Connect()) 
{ 
    session.CreateKeyspaceIfNotExists(keyspace, replication, true); 
    session.ChangeKeyspace(keyspace); 

    var entityTable = new Table<Models.Entity>(session); 
    var attributeTable = new Table<Models.Attribute>(session); 

    entityTable.CreateIfNotExists(); // Worked 
    attributeTable.CreateIfNotExists(); // Worked 

    entityTable.Delete(); // Does nothing 
    attributeTable.Delete(); // Does nothing 
} 

编辑:没有使用原始查询session.Execute("DROP TABLE entities;");工作正常。如何使用C#Datastax驱动程序删除Cassandra上的表?

回答

2

除非已经存在的下降,我不知道,你可以使用这个扩展表的方法。

public static class DastaxTableExtensions 
{ 
    public static void Drop<T>(this Table<T> table) 
    { 
     table.GetSession().Execute($"DROP TABLE {table.Name};"); 
    } 

    public static void DropIfExists<T>(this Table<T> table) 
    { 
     table.GetSession().Execute($"DROP TABLE IF EXISTS {table.Name};"); 
    } 
} 

然后你可以使用它像这样

entityTable.Drop(); 
attributeTable.DropIfExists(); 
4

Delete()方法不适用于删除表。它返回DELETE cql语句的表示形式。如果你打电话给你,你只需要{DELETE FROM entities}

如果你需要删除表,最简单的办法就是执行DROP语句:

session.Execute("DROP TABLE entities;"); 
+0

我得到 “行0:-1不匹配输入 '' 期待K_WHERE” – Almis

+0

被删除返回查询什么()? – CodeFuller

+0

嗯,似乎这是DELETE命令,而不是DROP,我得到'{DELETE FROM entities}' – Almis

相关问题