代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
cassandra操作封装类
作者:
若菲
/ 发布于
2014/6/17
/
537
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CassandraDemo { using Apache.Cassandra; using Thrift; using Thrift.Protocol; using Thrift.Transport; public class CassandraHelper { string m_space; string m_family; string m_host; int m_port; public CassandraHelper(string host, int port, string space, string family) { m_space = space; m_family = family; m_host = host; m_port = port; } public void DisplayKey(string key) { TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; // get the entire row for a key SliceRange sr = new SliceRange(); sr.Start = Encoding.UTF8.GetBytes(""); sr.Finish = Encoding.UTF8.GetBytes(""); SlicePredicate sp = new SlicePredicate(); sp.Slice_range = sr; sp.__isset.slice_range = true; // set __isset for the columns instead if you use them KeyRange range = new KeyRange(); range.Start_key = Encoding.UTF8.GetBytes(key); range.End_key = Encoding.UTF8.GetBytes(key); range.__isset.start_key = true; range.__isset.end_key = true; List<KeySlice> results = cass.get_range_slices(cparent, sp, range, ConsistencyLevel.ONE); if (results == null || results.Count == 0) { Console.WriteLine("key:" + key + " dos't insert.."); } foreach (KeySlice result in results) { Console.WriteLine("Key: " + Encoding.UTF8.GetString(result.Key)); foreach (ColumnOrSuperColumn csc in result.Columns) { Console.WriteLine("Column: " + Encoding.UTF8.GetString(csc.Column.Name) + " Value: " + Encoding.UTF8.GetString(csc.Column.Value)); } } } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } } public bool IsKeyExist(string key) { TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; // get the entire row for a key SliceRange sr = new SliceRange(); sr.Start = Encoding.UTF8.GetBytes(""); sr.Finish = Encoding.UTF8.GetBytes(""); SlicePredicate sp = new SlicePredicate(); sp.Slice_range = sr; sp.__isset.slice_range = true; // set __isset for the columns instead if you use them int count = cass.get_count(Encoding.UTF8.GetBytes(key), cparent, sp, ConsistencyLevel.ONE); return count > 0; } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return false; } public bool InsertColumn(string key, string colname, byte[] colvalue) { TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; SliceRange sr = new SliceRange(); sr.Start = Encoding.UTF8.GetBytes(""); sr.Finish = Encoding.UTF8.GetBytes(""); SlicePredicate sp = new SlicePredicate(); sp.Slice_range = sr; sp.__isset.slice_range = true; // set __isset for the columns instead if you use them KeyRange range = new KeyRange(); range.Start_key = Encoding.UTF8.GetBytes(key); range.End_key = Encoding.UTF8.GetBytes(key); range.__isset.start_key = true; range.__isset.end_key = true; List<KeySlice> results = cass.get_range_slices(cparent, sp, range, ConsistencyLevel.ONE); Column c = new Column(); c.Name = Encoding.UTF8.GetBytes(colname); c.Value = colvalue; c.__isset.value = true; if (results != null && results.Count > 0) { c.Timestamp = results[0].Columns[0].Column.Timestamp; } else { c.Timestamp = DateTime.Now.Ticks; } c.__isset.timestamp = true; // insert the "name" column cass.insert(Encoding.UTF8.GetBytes(key), cparent, c, ConsistencyLevel.ONE); return true; } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return false; } public bool InsertColumn(string key, string colname, byte[] colvalue, long timestamp) { if (timestamp <= 0) { return InsertColumn(key, colname, colvalue); } TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; Column c = new Column(); c.Name = Encoding.UTF8.GetBytes(colname); c.Value = colvalue; c.__isset.value = true; c.Timestamp = timestamp; c.__isset.timestamp = true; // insert the "name" column cass.insert(Encoding.UTF8.GetBytes(key), cparent, c, ConsistencyLevel.ONE); // insert another column, "JobId" return true; } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return false; } public byte[] FetchColumn(string key, string colname) { byte[] colvalue = null; TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnPath cp = new ColumnPath(); cp.__isset.column = true; // this must be set of you'll get an error re: Padraig O'Sullivan cp.Column = Encoding.UTF8.GetBytes(colname); cp.Column_family = m_family; cp.Super_column = Encoding.UTF8.GetBytes(""); ColumnOrSuperColumn sc = cass.get(Encoding.UTF8.GetBytes(key), cp, ConsistencyLevel.ONE); colvalue = sc.Column.Value; } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return colvalue; } public bool RemoveColumn(string key) { TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; // get the entire row for a key SliceRange sr = new SliceRange(); sr.Start = Encoding.UTF8.GetBytes(""); sr.Finish = Encoding.UTF8.GetBytes(""); SlicePredicate sp = new SlicePredicate(); sp.Slice_range = sr; sp.__isset.slice_range = true; // set __isset for the columns instead if you use them KeyRange range = new KeyRange(); range.Start_key = Encoding.UTF8.GetBytes(key); range.End_key = Encoding.UTF8.GetBytes(key); range.__isset.start_key = true; range.__isset.end_key = true; List<KeySlice> results = cass.get_range_slices(cparent, sp, range, ConsistencyLevel.ONE); ColumnPath cp = new ColumnPath(); cp.__isset.column = true; // this must be set of you'll get an error re: Padraig O'Sullivan foreach (KeySlice result in results) { foreach (ColumnOrSuperColumn csc in result.Columns) { cp.Column = csc.Column.Name; cp.Column_family = m_family; cp.Super_column = Encoding.UTF8.GetBytes(""); cass.remove(Encoding.UTF8.GetBytes(key), cp, csc.Column.Timestamp, ConsistencyLevel.ALL); } } return true; } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return false; } public long FetchTimestamp(string key) { long timestamp = 0; TTransport m_socket = null; TTransport m_trans = null; TProtocol m_proto = null; try { m_socket = new TSocket(m_host, m_port); m_trans = new TFramedTransport(m_socket); m_proto = new TBinaryProtocol(m_trans); m_trans.Open(); Cassandra.Client cass = new Cassandra.Client(m_proto); cass.set_keyspace(m_space); ColumnParent cparent = new ColumnParent(); cparent.Column_family = m_family; SliceRange sr = new SliceRange(); sr.Start = Encoding.UTF8.GetBytes(""); sr.Finish = Encoding.UTF8.GetBytes(""); SlicePredicate sp = new SlicePredicate(); sp.Slice_range = sr; sp.__isset.slice_range = true; // set __isset for the columns instead if you use them KeyRange range = new KeyRange(); range.Start_key = Encoding.UTF8.GetBytes(key); range.End_key = Encoding.UTF8.GetBytes(key); range.__isset.start_key = true; range.__isset.end_key = true; List<KeySlice> results = cass.get_range_slices(cparent, sp, range, ConsistencyLevel.ONE); if (results != null && results.Count > 0) { timestamp = results[0].Columns[0].Column.Timestamp; } } catch (TTransportException te) { Console.WriteLine("TTransportException: " + te); } catch (InvalidRequestException ire) { Console.WriteLine("InvalidRequestException: " + ire); } catch (NotFoundException nfe) { Console.WriteLine("NotFoundException: " + nfe); } catch (UnavailableException ue) { Console.WriteLine("UnavailableException: " + ue); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (m_trans != null) { m_trans.Close(); } } return timestamp; } } }
试试其它关键字
cassandra
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
若菲
贡献的其它代码
(
2
)
.
cassandra操作封装类
.
CSS3实现360度旋转
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3