【问题标题】:Correct way to connect to Cassandra in a web/api app在 web/api 应用程序中连接到 Cassandra 的正确方法
【发布时间】:2014-07-02 05:58:18
【问题描述】:

我正在寻找在 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");
    }
}

在我的控制器中,我是这样使用它的:

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);
    }
}

任何示例,信息都会非常有帮助。

谢谢。

【问题讨论】:

  • 您有更好的解决方案来从.net 连接到 Cassandra 吗?

标签: c# asp.net-mvc asp.net-web-api cassandra datastax


【解决方案1】:

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

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;
    }
}

在我的 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;
    }

C# 的官方 DataStax 代码并不多。我通过在DataStax Academy 上的 Cassandra Java 开发课程中学到的知识对此进行了调整。显然,在这个例子中我没有做 MVC,但我希望它会有所帮助。

【讨论】:

  • 该代码看起来会在您每次访问 ProductsDAO 时调用 Cluster.Bulder()cluster.Connect() - 可以吗?做什么?
  • 我这样做只是为了尽量保持简短。在 CassandraDAO 中,您可以为 session 提供一个受保护的 getter,并且只有在 session 为空时才调用 connect。我将进行编辑以反映这一点。
猜你喜欢
  • 2021-07-20
  • 2011-10-01
  • 2019-10-24
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多