A modern, feature-rich and highly tunable C# client library for Apache Cassandra (2.0+) using Cassandra's binary protocol and Cassandra Query Language v3.
It also provides additional features for DataStax Enterprise:
IAuthenticator
implementations that use the authentication scheme negotiation in the server-side DseAuthenticator
.The driver supports .NET Framework 4.5.2+ and .NET Core 2.1+ (LTS).
PM> Install-Package CassandraCSharpDriver
You can use the project Mailing list or create a ticket on the Jira issue tracker. Additionally, you can ask questions on DataStax Community.
If you are upgrading from previous versions of the driver, visit the Upgrade Guide.
If you are upgrading from the DSE C# driver (which has been unified with this driver), there's also a section related to this on the Upgrade Guide.
// Configure the builder with your cluster's contact points
var cluster = Cluster.Builder()
.AddContactPoints("host1")
.Build();
// Connect to the nodes using a keyspace
var session = cluster.Connect("sample_keyspace");
// Execute a query on a connection synchronously
var rs = session.Execute("SELECT * FROM sample_table");
// Iterate through the RowSet
foreach (var row in rs)
{
var value = row.GetValue<int>("sample_int_column");
// Do something with the value
}
If you are using DataStax Astra you can configure your cluster instance by setting the secure bundle and the user credentials:
// Configure the builder with your cluster's cloud secure connection bundle and credentials
var cluster = Cluster.Builder()
.WithCloudSecureConnectionBundle("path/to/secure-connect-DATABASE_NAME.zip")
.WithCredentials("user_name", "p@ssword1")
.Build();
Prepare your query once and bind different parameters to obtain best performance.
// Prepare a statement once
var ps = session.Prepare("UPDATE user_profiles SET birth=? WHERE key=?");
// ...bind different parameters every time you need to execute
var statement = ps.Bind(new DateTime(1942, 11, 27), "hendrix");
// Execute the bound statement with the provided parameters
session.Execute(statement);
You can execute multiple statements (prepared or unprepared) in a batch to update/insert several rows atomically even in different column families.
// Prepare the statements involved in a profile update once
var profileStmt = session.Prepare("UPDATE user_profiles SET email=? WHERE key=?");
var userTrackStmt = session.Prepare("INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)");
// ...you should reuse the prepared statement
// Bind the parameters and add the statement to the batch batch
var batch = new BatchStatement()
.Add(profileStmt.Bind(emailAddress, "hendrix"))
.Add(userTrackStmt.Bind("hendrix", "You changed your email", DateTime.Now));
// Execute the batch
session.Execute(batch);
Session allows asynchronous execution of statements (for any type of statement: simple, bound or batch) by exposing the ExecuteAsync
method.
// Execute a statement asynchronously using await
var rs = await session.ExecuteAsync(statement);
The driver features a built-in Mapper and Linq components that can use to avoid boilerplate mapping code between cql rows and your application entities.
User user = mapper.Single<User>("SELECT name, email FROM users WHERE id = ?", userId);
See the driver components documentation for more information.
You can iterate indefinitely over the RowSet
, having the rows fetched block by block until the rows available on the client side are exhausted.
var statement = new SimpleStatement("SELECT * from large_table");
// Set the page size, in this case the RowSet will not contain more than 1000 at any time
statement.SetPageSize(1000);
var rs = session.Execute(statement);
foreach (var row in rs)
{
// The enumerator will yield all the rows from Cassandra
// Retrieving them in the back in blocks of 1000.
}
You can map your Cassandra User Defined Types to your application entities.
For a given udt
CREATE TYPE address (
street text,
city text,
zip_code int,
phones set<text>
);
For a given class
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public int ZipCode { get; set; }
public IEnumerable<string> Phones { get; set;}
}
You can either map the properties by name
// Map the properties by name automatically
session.UserDefinedTypes.Define(
UdtMap.For<Address>()
);
Or you can define the properties manually
session.UserDefinedTypes.Define(
UdtMap.For<Address>()
.Map(a => a.Street, "street")
.Map(a => a.City, "city")
.Map(a => a.ZipCode, "zip_code")
.Map(a => a.Phones, "phones")
);
You should map your UDT to your entity once and you will be able to use that mapping during all your application lifetime.
var rs = session.Execute("SELECT id, name, address FROM users where id = x");
var row = rs.First();
// You can retrieve the field as a value of type Address
var userAddress = row.GetValue<Address>("address");
Console.WriteLine("user lives on {0} Street", userAddress.Street);
You can set the options on how the driver connects to the nodes and the execution options.
// Example at cluster level
var cluster = Cluster
.Builder()
.AddContactPoints(hosts)
.WithCompression(CompressionType.LZ4)
.WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("west"));
// Example at statement (simple, bound, batch) level
var statement = new SimpleStatement(query)
.SetConsistencyLevel(ConsistencyLevel.Quorum)
.SetRetryPolicy(DowngradingConsistencyRetryPolicy.Instance)
.SetPageSize(1000);
If you are using the PasswordAuthenticator
which is included in the default distribution of Apache Cassandra, you can use the Builder.WithCredentials
method or you can explicitly create a PlainTextAuthProvider
instance.
For clients connecting to a DSE cluster secured with DseAuthenticator
, two authentication providers are included (on the Cassandra.DataStax.Auth
namespace):
DsePlainTextAuthProvider
: plain-text authentication;DseGssapiAuthProvider
: GSSAPI authentication.To configure a provider, pass it when initializing the cluster:
using Cassandra;
using Cassandra.DataStax.Auth;
ICluster cluster = Cluster.Builder()
.AddContactPoint("127.0.0.1")
.WithAuthProvider(new DseGssapiAuthProvider())
.Build();
ISession
has dedicated methods to execute graph queries:
using Cassandra.DataStax.Graph;
session.ExecuteGraph("system.createGraph('demo').ifNotExist().build()");
GraphStatement s1 = new SimpleGraphStatement("g.addV(label, 'test_vertex')").SetGraphName("demo");
session.ExecuteGraph(s1);
GraphStatement s2 = new SimpleGraphStatement("g.V()").SetGraphName("demo");
GraphResultSet rs = session.ExecuteGraph(s2);
IVertex vertex = rs.First().To<IVertex>();
Console.WriteLine(vertex.Label);
You can set default graph options when initializing the cluster. They will be used for all graph statements. For example, to avoid repeating SetGraphName("demo")
on each statement:
ICluster cluster = Cluster.Builder()
.AddContactPoint("127.0.0.1")
.WithGraphOptions(new GraphOptions().SetName("demo"))
.Build();
If an option is set manually on a GraphStatement
, it always takes precedence; otherwise the default option is used.This might be a problem if a default graph name is set, but you explicitly want to execute a statement targeting system
, for which no graph name must be set. In that situation, use GraphStatement.SetSystemQuery()
:
GraphStatement s = new SimpleGraphStatement("system.createGraph('demo').ifNotExist().build()")
.SetSystemQuery();
session.ExecuteGraph(s);
As explained, graph statements can be executed with the session's ExecuteGraph
method. There is also an asynchronous equivalent called ExecuteGraphAsync
that returns a Task
that can be awaited upon.
Graph queries return a GraphResultSet
, which is a sequence of GraphNode
elements:
GraphResultSet rs = session.ExecuteGraph(new SimpleGraphStatement("g.V()"));
// Iterating as IGraphNode
foreach (IGraphNode r in rs)
{
Console.WriteLine(r);
}
IGraphNode
represents a response item returned by the server. Each item can be converted to the expected type:
GraphResultSet rs = session.ExecuteGraph(new SimpleGraphStatement("g.V()"));
IVertex vertex = rs.First().To<IVertex>();
Console.WriteLine(vertex.Label);
Additionally, you can apply the conversion to all the sequence by using GraphResultSet.To<T>()
method:
foreach (IVertex vertex in rs.To<IVertex>())
{
Console.WriteLine(vertex.Label);
}
GraphNode
provides implicit conversion operators to string
, int
, long
and others in order to improve code readability, allowing the following C# syntax:
var rs = session.ExecuteGraph(new SimpleGraphStatement("g.V().has('name', 'marko').values('location')"));
foreach (string location in rs)
{
Console.WriteLine(location);
}
GraphNode
inherits from DynamicObject
, allowing you to consume it using the dynamic
keyword and/or as a dictionary.
dynamic r = session.ExecuteGraph(new SimpleGraphStatement("g.V()")).First();
Graph query parameters are always named. Parameter bindings are passed as an anonymous type or as aIDictionary<string, object>
alongside the query:
session.ExecuteGraph("g.addV(label, vertexLabel)", new { vertexLabel = "test_vertex_2" });
Note that, unlike in CQL, Gremlin placeholders are not prefixed with ":".
Prepared graph statements are not supported by DSE Graph.
DSE 5 comes with a set of additional types to represent geospatial data: PointType
, LineStringType
andPolygonType
:
cqlsh> CREATE TABLE points_of_interest(name text PRIMARY KEY, coords 'PointType');
cqlsh> INSERT INTO points_of_interest (name, coords) VALUES ('Eiffel Tower', 'POINT(48.8582 2.2945)');
The DSE driver includes C# representations of these types, that can be used directly in queries:
using Cassandra.Geometry;
Row row = session.Execute("SELECT coords FROM points_of_interest WHERE name = 'Eiffel Tower'").First();
Point coords = row.GetValue<Point>("coords");
var statement = new SimpleStatement("INSERT INTO points_of_interest (name, coords) VALUES (?, ?)",
"Washington Monument",
new Point(38.8895, 77.0352));
session.Execute(statement);
Note: DataStax products do not support big-endian systems.
You can use Visual Studio or msbuild to build the solution.
Check the documentation for building the driver from source and running the tests.
© DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart?showComments=true&showCommentArea=true#addcomment 介绍 本文的目的是让你对如何使用Mongodb的C#驱动有一个简单的了解,当你阅读完本文你就可以参考其他文章以了解更多信息。 下载C#驱动 你可以在这里下载需要的驱动。
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 简介 本文主要介绍由 10gen 提供的C#驱动,该驱动主要由两个库组成:BSON Library 和C# Driver。其中 BSON Library 可单独使用,而C# Driver则需要BSON Library库的支持。 驱动下载 省略 编译生成 省略 单元测试 省
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.R
简介 使用了一点Mongodb ,以下是自己的一点心得体会: 不提及他的分步式,效率等特性.至少以下一些特点.让我感觉没有必要再在普通应用场景再使用关系型数据库如:SQLServer那样的数据库了. 1. 省去了ORM:如NHibernate是一个优秀的ORM,性能也很好.但你得学习NHibernate的表达式语法.像NH那种文档健全的尚可接受. 我还有一些更可怕的经历,写
How to specify an Order or Sort using the C# driver for MongoDB? I'm trying to figure out how to sort a collection of documents server side by telling the C# driver what the sort order is, but it appe
https://github.com/joeandaverde/socket.io-csharp-client http://websocket4net.codeplex.com/ http://www.codeproject.com/Articles/463947/Working-with-Sockets-in-Csharp https://supersocket.codeplex.com/ h
本客户端改进自 ctripcorp/cat.net 改动点: 支持异步调用: 使用AsyncLocal替换原先的 ThreadLocal(线程上下文,不支持async/await) 支持异步并发调用;需要显示调用 Cat.NewForkedTransaction("remote", "<>")开启新的调用上下文。 Sample: # 并行调用 var tasks = Enumerable.Ran
此脚本为纯 C# 实现的脚本系统,最低支持 .net framework 4.0 和 .net standard 2.0,语法类似 JavaScript。 兼容大部分 .net 平台: .net framework 3.5 及以上 .net standard 2.0 及以上 .net core 2.0 及以上 unity3d asp.net asp.net core mono xamarin 脚本
This is an independent ENet implementation with a modified protocol for C, C++, C#, and other languages. Features: Lightweight and straightforward Low resource consumption Dual-stack IPv4/IPv6 support
Entitas is free, but powered by your donations Entitas - The Entity Component System Framework for C# and Unity Entitas is a super fast Entity Component System Framework (ECS) specifically made for C#
MongoDB-CSharp 是 C# 编程语言用来连接 MongoDB 的一个开发库。MongoDB是一个分布式的文档存储数据库。
CSharp Logger是apache继log4net项目后设计的又一个日志工具。它用来向Windows的事件日志写入debug、info、warn和error四个等级的信息。