在.net中使用aquiles访问Cassandra(二)
public class RowMutation
{
public string Key { get; set; } public IList<CellMutation> Mutations { get; set; } public RowMutation()
{ } public RowMutation(string key)
{
Key = key;
} public RowMutation(string key, IList<CellMutation> mutations)
{
Key = key;
Mutations = mutations;
}
} public class CellMutation
{
public string ColumnName { get; set; } public string DataValue { get; set; } public CellMutation()
{ } public CellMutation(string name)
{
ColumnName = name;
} public CellMutation(string name, string value)
{
ColumnName = name;
DataValue = value;
}
}
2.引用aquiles命名空间。
using Aquiles.Helpers.Encoders;
using Aquiles.Cassandra10;
using Aquiles.Core.Cluster;
3.初始化成员变量
private string _clusterName { get; set; }
private ConsistencyLevel _consistencyLevel { get; set; }
private string _keyspaceName { get; set; }
private ICluster _cluster = null;
public AquilesDemo()
{
_clusterName = "xxxx";
_consistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
_keyspaceName = "xxxx";
_cluster = AquilesHelper.RetrieveCluster(_clusterName);
}
4.建立一个通用的修改多行多列的方法,其它改变方式可以在此基础上完成调用,如修改一多一列,一行多列等。
public void Mutate(string columnFamily, IList<RowMutation> rowMutations)
{
if (string.IsNullOrWhiteSpace(columnFamily)) throw new ArgumentNullException("columnFamily"); rowMutations = PrepareMutationList(rowMutations);
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>(); foreach (CellMutation cellMutation in rowMutation.Mutations)
{
if (cellMutation.DataValue == null) continue; Apache.Cassandra.Mutation mutation = new Apache.Cassandra.Mutation()
{
Column_or_supercolumn = new ColumnOrSuperColumn()
{
Column = new Column()
{
Name = ByteEncoderHelper.UTF8Encoder.ToByteArray(cellMutation.ColumnName),
Timestamp = DateTime.Now.ToUnixTimestamp(),
Value = ByteEncoderHelper.UTF8Encoder.ToByteArray(cellMutation.DataValue),
},
},
};
mutationList.Add(mutation);
} if (mutationList.Count > )
{
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); }
5. 由于调用端可能会将同一行的修改分为多个RowMutation传入,所以上面的代码使用PrepareMutationList方法根据RowKey作了聚合处理。
/// <summary>
/// 聚合列表中相同rowkey的项为同一Mutation。
/// </summary>
/// <param name="rowMutations"></param>
/// <returns></returns>
private IList<RowMutation> PrepareMutationList(IList<RowMutation> rowMutations)
{
if (rowMutations == null) return null; //按rowkey分组
var rowMutationGroups = rowMutations.GroupBy(rm => rm.Key); //分组后的总数量相同,则认为rowkey在列表中是唯一的,不用做聚合处理
if (rowMutations.Count == rowMutationGroups.Count()) return rowMutations; //遍历分组结果
IList<RowMutation> result = new List<RowMutation>();
foreach (var rmg in rowMutationGroups)
{
RowMutation rowMutation = new RowMutation();
rowMutation.Key = rmg.Key; //将同一分组的所有CellMutation放在一个列表中
List<CellMutation> cellMutations = new List<CellMutation>();
foreach (var rm in rmg)
{
cellMutations.AddRange(rm.Mutations);
} if (cellMutations.Count() > )
{
rowMutation.Mutations = cellMutations;
result.Add(rowMutation);
}
}
return result;
}
在.net中使用aquiles访问Cassandra(二)的更多相关文章
- 在.net中使用aquiles访问Cassandra(一)
aquiles是.net下基于Thrift协议访问Cassandra的第三方类库,官方地址是: http://aquiles.codeplex.com/ 1.下载类库文件: http://aqui ...
- 在.net中使用aquiles访问Cassandra(四)
数据的持久化我们都已经完成了,和所有应有程序一样,最重要的是要向用户展示数据.下面我们就推出这部分代码,读取任意行任何列: public IList<TRowResult> Execute ...
- 在.net中使用aquiles访问Cassandra(三)
之前我们实现了如何修改数据,还需要相应的删除动作.删除方式会有几种情况,以下分别一一介绍. 1.批量删除,适应于多行多列的情况. public void Remove(string columnF ...
- ArcGIS Engine中的数据访问
ArcGIS Engine中的数据访问 数据是GIS的基础, 访问数据也是进行任何复杂的空间分析及空间可视化表达的前提.ArcGIS支持的数据格式比较丰富,对不同的数据格式支持的程度也有很大差异.本文 ...
- 已禁用对分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具启用 DTC 以便在 MSDTC 安全配置中进行网络访问。
今天写ASP.NET程序,在网页后台的c#代码里写了个事务,事务内部对一张表进行批量插入,对另外一张表进行查询与批量插入. 结果第二张表查询后foreach迭代操作时报错:已禁用对分布式事务管理器(M ...
- 自建目录中jsp页面访问servlet路径出错404
---恢复内容开始--- 自建目录中jsp页面访问servlet路径出错404 使用eclipse建立的项目,总是会遇到路径问题,比如jsp页面访问servlet,jsp在默认的路径.jsp在自建目录 ...
- 在KVM虚拟机中使用spice系列之二(USB映射,SSL,密码,多客户端支持)
在KVM虚拟机中使用spice系列之二(USB映射,SSL,密码,多客户端支持) 发布时间: 2015-02-27 00:16 1.spice的USB重定向 1.1 介绍 使用usb重定向,在clie ...
- 【java】基础中的杂乱总结(二)
1 内部类进阶 package package8; //原则:先用内部类写 之后由于内部类匿名无法引用 用其继承的父类或实现的接口名 //再复写所有的抽象方法即可(是所有,否者还是抽象的,无法创建对象 ...
- Java基础进阶:多态与接口重点摘要,类和接口,接口特点,接口详解,多态详解,多态中的成员访问特点,多态的好处和弊端,多态的转型,多态存在的问题,附重难点,代码实现源码,课堂笔记,课后扩展及答案
多态与接口重点摘要 接口特点: 接口用interface修饰 interface 接口名{} 类实现接口用implements表示 class 类名 implements接口名{} 接口不能实例化,可 ...
随机推荐
- java 多态2
http://www.cnblogs.com/wqq0402/p/6180685.html package test05; public class DuoTai_Test02 { /**多个对象,一 ...
- 不一样的角度 解读微信小程序
不一样的角度 解读微信小程序 七月在夏天· 2 天前 前段时间看完了雨果奖中短篇获奖小说<北京折叠>.很有意思的是,张小龙最近也要把应用折叠到微信里,这些应用被他称为:小程序. 含着金钥匙 ...
- Jquery插件开发学习
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...
- mysql解压缩安装(一)
MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...
- LeetCode(97) Interleaving String
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- 优化后的 google提供的汉字转拼音类(针对某些htc等手机的不兼容情况)
/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- js 根据名字获取cookie 的方法
function getcookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf( ...
- echarts异步加载柱状图遇到的错误- Error: Component series. not exists. Load it first.
今天看了下echarts教程之中的异步加载柱状图,我按照教程中的代码敲出来之后再运行,就报了一个 Error: Component series. not exists. Load it first. ...
- node.js之path
说到node.js,可能实际中用到node进行后台开发的公司不多,大部分人都没有开发后台的经验.但是也要了解node相关模块的用法,因为现在前端自动化脚本的构建,模块的打包越来越离不开node.特别是 ...
- iTunes Connect 开发者上手经验(转)
原文:http://www.cnblogs.com/zhw511006/archive/2013/01/15/2860945.html iOS Developer通常需要用到 developer.ap ...