在web/api应用程序中连接到Cassandra的正确方法

Mar*_*tin 7 c# asp.net-mvc cassandra asp.net-web-api datastax

我正在寻找在ASP.NET Web API项目中使用官方Cassandra C#驱动程序(2.0)的正确方法 - 由高流量站点使用.

我做了一个非常简单的示例应用程序,使用以下类连接到cassandra数据库:

public class CassandraContext
{
    private static ISession _session;
    public ISession Session { get { return _session; } }

    public CassandraContext()
    {
        var cluster = Cluster.Builder().AddContactPoint("cassandra.some.server").Build();
        _session = cluster.Connect("keyspace");
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我正在使用它:

public class TestController : ApiController
{
    static CassandraContext db = new CassandraContext();

    public IHttpActionResult Get()
    {
        var result = new List<string>();
        var rowSet = db.Session.Execute(@"SELECT * FROM ""Test"";");

        foreach (var row in rowSet)
            result.Add(row.GetValue<string>("data"));

        return Ok<List<string>>(result);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何例子,信息都会非常有用.

谢谢.

Aar*_*ron 3

我认为你走在正确的道路上。这是我过去所做的:

public class CassandraDAO
{
    private Cluster cluster;
    private Session session;
    private String NODE = ABCServiceTester.Properties.Settings.Default.CASSANDRA_NODE;
    private String USER = ABCServiceTester.Properties.Settings.Default.USERNAME;
    private String PASS = ABCServiceTester.Properties.Settings.Default.PASSWORD;

    public CassandraDAO()
    {
        connect();
    }

    private void connect()
    {
        cluster = Cluster.Builder().WithCredentials(USER, PASS)
            .AddContactPoint(NODE).Build();
        session = cluster.Connect();
    }

    protected Session getSession()
    {
        if (session == null)
        {
            connect();
        }

        return session;
    }
}
Run Code Online (Sandbox Code Playgroud)

通过将连接详细信息隔离在 CassandraDAO 类中,然后我为每个键空间或功能区域编写单独的 DAO。然后,这些 DAO 继承 CassandraDAO 类。

public class ProductsDAO : CassandraDAO
{
    public List<Product> getProducts(string _itemID)
    {
        string strCQL = "SELECT priceAvail, productGroup, productSpec, sizeProfile "
            + "FROM products.itemsmaster "
            + "WHERE itemID=?";
        Session localSession = getSession();
        PreparedStatement statement = localSession.Prepare(strCQL);
        BoundStatement boundStatement = new BoundStatement(statement);
        boundStatement.Bind(_itemID);

        //get result set from Cassandra
        RowSet results = localSession.Execute(boundStatement);

        List<Product> returnVal = new List<Product>();

        foreach (Row row in results.GetRows())
        {
            Product tempProd = new Product();
            tempProd.itemID= _itemID;
            tempProd.priceAvail = row.GetValue<int>("priceavail");
            tempProd.productGroup = row.GetValue<string>("productgroup");
            tempProd.productSpec = row.GetValue<string>("productspec");
            tempProd.sizeProfile = row.GetValue<string>("sizeprofile");

            returnVal.Add(tempProd);
        }

        return returnVal;
    }
Run Code Online (Sandbox Code Playgroud)

没有大量适用于 C# 的官方 DataStax 代码。我根据参加DataStax Academy的 Cassandra Java 开发课程所学到的知识改编了此内容。显然,我在这个例子中没有使用 MVC,但我希望它能有所帮助。