之前我们实现了如何修改数据,还需要相应的删除动作。删除方式会有几种情况,以下分别一一介绍。

 
1.批量删除,适应于多行多列的情况。
public void Remove(string columnFamily, IList<RowMutation> rowMutations)
{
if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily"); Dictionary<byte[], Dictionary<string, List<Apache.Cassandra.Mutation>>> mutation_map = new Dictionary<byte[], Dictionary<string, List<Apache.Cassandra.Mutation>>>(); foreach (var rowMutation in rowMutations)
{
byte[] key = ByteEncoderHelper.UTF8Encoder.ToByteArray(rowMutation.Key); Dictionary<string, List<Apache.Cassandra.Mutation>> cfMutation = new Dictionary<string, List<Apache.Cassandra.Mutation>>();
List<Apache.Cassandra.Mutation> mutationList = new List<Apache.Cassandra.Mutation>(); Apache.Cassandra.Mutation mutation = new Apache.Cassandra.Mutation();
mutation.Deletion = new Deletion();
if (rowMutation.Mutations != null && rowMutation.Mutations.Count > )
{
mutation.Deletion.Predicate = new SlicePredicate()
{
Column_names = rowMutation.Mutations.Select(
m => ByteEncoderHelper.UTF8Encoder.ToByteArray(m.ColumnName)).ToList()
};
}
mutation.Deletion.Timestamp = DateTime.Now.ToUnixTimestamp();
mutationList.Add(mutation); cfMutation.Add(columnFamily, mutationList);
mutation_map.Add(key, cfMutation);
} if (mutation_map.Count == ) return;
_cluster.Execute(new ExecutionBlock(delegate(Apache.Cassandra.Cassandra.Client client)
{
client.batch_mutate(mutation_map, _consistencyLevel);
return null;
}), _keyspaceName); }
2.删除指定的行。
public void Remove(string columnFamily, string rowKey)
{
if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily"); byte[] key = ByteEncoderHelper.UTF8Encoder.ToByteArray(rowKey); ColumnPath columnPath = new ColumnPath()
{
Column_family = columnFamily,
}; _cluster.Execute(new ExecutionBlock(delegate(Apache.Cassandra.Cassandra.Client client)
{
client.remove(key, columnPath, DateTime.Now.ToUnixTimestamp(), _consistencyLevel);
return null;
}), _keyspaceName);
}
3.删除指定行指定列。
public void Remove(string columnFamily, string rowKey, string columnName)
{
if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily"); byte[] key = ByteEncoderHelper.UTF8Encoder.ToByteArray(rowKey); ColumnPath columnPath = new ColumnPath()
{
Column_family = columnFamily,
Column = ByteEncoderHelper.UTF8Encoder.ToByteArray(columnName)
}; _cluster.Execute(new ExecutionBlock(delegate(Apache.Cassandra.Cassandra.Client client)
{
client.remove(key, columnPath, DateTime.Now.ToUnixTimestamp(), _consistencyLevel);
return null;
}), _keyspaceName);
}
4.删除多行,使用批量删除方法完成。
public void Remove(string columnFamily, IList<string> rowKeys)
{
IList<RowMutation> batchMutations = new List<RowMutation>();
foreach (string rowKey in rowKeys)
{
batchMutations.Add(new RowMutation(rowKey));
}
Remove(columnFamily, batchMutations);
}
5.删除一行多列,同样调用指量删除方法完成。
public void Remove(string columnFamily, string rowKey, IList<string> columnNames)
{
IList<RowMutation> rowMutations = new List<RowMutation>(); RowMutation rowMutation = new RowMutation();
rowMutation.Key = rowKey; rowMutation.Mutations = new List<CellMutation>();
foreach (string columnName in columnNames)
{
rowMutation.Mutations.Add(new CellMutation(columnName));
}
rowMutations.Add(rowMutation); Remove(columnFamily, rowMutations);
}

在.net中使用aquiles访问Cassandra(三)的更多相关文章

  1. 在.net中使用aquiles访问Cassandra(一)

    aquiles是.net下基于Thrift协议访问Cassandra的第三方类库,官方地址是: http://aquiles.codeplex.com/   1.下载类库文件: http://aqui ...

  2. 在.net中使用aquiles访问Cassandra(二)

    上文中我们已经建立了项目的基本结构,今天实现数据的修改.在NoSQL中,通常添加和修改都认为是对数据的一种Mutation.   1.建立描述修改Row的实体. public class RowMut ...

  3. 在.net中使用aquiles访问Cassandra(四)

    数据的持久化我们都已经完成了,和所有应有程序一样,最重要的是要向用户展示数据.下面我们就推出这部分代码,读取任意行任何列: public IList<TRowResult> Execute ...

  4. 使用虚幻引擎中的C++导论(三-反射系统与迭代器)

    使用虚幻引擎中的C++导论(三) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...

  5. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

  6. 客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...

  7. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

  8. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...

  9. 在Tomcat中部署web项目的三种方式

    搬瓦工搭建SS教程 SSR免费节点:http://www.xiaokeli.me 在这里介绍在Tomcat中部署web项目的三种方式: 1.部署解包的webapp目录 2.打包的war文件 3.Man ...

随机推荐

  1. poj1014(还需要改动)

    #include <stdio.h> int n[6]; int main() { freopen("in.txt","r",stdin); int ...

  2. html table冻结列

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  3. C#之不借助第三变量交换两变量值

    源码: 1 2 3 4 5   int n1=10, n2=20;      n1 = n1 - n2;   // -10   n2 = n1 + n2;  //  10   n1 = n2 - n1 ...

  4. MariaDB kill命令

    MariaDB的KILL命令不只可以杀掉连接,而且可以只杀掉某连接当前的SQL,而不断开连接.KILL QUERY thread_id;kill thread_id可以杀掉当前的连接,而kill QU ...

  5. firefox的console log功能

    http://www.ruanyifeng.com/blog/2011/03/firebug_console_tutorial.html Firebug是网页开发的利器,能够极大地提升工作效率. 但是 ...

  6. (Python)异常处理try...except、raise

    一.try...except 有时候我们写程序的时候,会出现一些错误或异常,导致程序终止.例如,做除法时,除数为0,会引起一个ZeroDivisionError 例子: a=10 b=0 c=a/b ...

  7. centos 如何清理/dev/vda1系统盘

    df-h检查一台服务器磁盘使用空间,发现磁盘已经使用了100% 思路是: 1.cd /usr 2.du -sh * 看哪个目录占用空间大 3.重复前两步,根据实际情况删除或者移走 4.日志的话可以运行 ...

  8. 开发前准备 va2015安装

    1.下载vs2015 2.进行安装(同时安装node.js.npm与Android SDK,会省很多时间) 安装的时候要选择自定义安装 如果先安装了Android SDK的话就不要勾选了,我就是勾选了 ...

  9. UIScrollView的三个属性

    contentSize.contentOffset.contentInset   UIScrollView的frame的size 指的是可视范围   contentSize  内容大小,滚动的范围 创 ...

  10. yii安装 /You don't have permission to access on this server

    在安装yii的时候 ,当打开了init.bat进行配置的时候小黑本弹出了个小黑框立刻就关闭了,  进入cmd模式再打开init.bat就出现了"You don't have permissi ...