Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to connect to Cassandra in a web/api app

I'm looking for the correct way to use the official Cassandra C# driver (2.0) in a ASP.NET Web API project - used by a high traffic site.

I've made a very simple sample app that connects to a cassandra db using the following classes:

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

And in my Controller I'm using it like this:

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

Any examples, information will be very helpful.

Thanks.

like image 870
Martin Avatar asked May 14 '14 09:05

Martin


People also ask

Does Cassandra have a REST API?

CassandraRestfulAPI project exposes the cassandra data tables and nodes with the help of Restful API's. The project follows the standard Restful API rules.


1 Answers

I think you're on the right track. Here is what I've done in the past:

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

With my connection details isolated in my CassandraDAO class, I then write individual DAOs for each keyspace or area of functionality. These DAOs then inherit the CassandraDAO class.

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

There isn't a whole lot of official DataStax code out there for C#. I adapted this from what I learned by taking the Cassandra Java Development class on DataStax Academy. Obviously, I wasn't doing MVC in this example, but I hope it helps.

like image 107
Aaron Avatar answered Oct 08 '22 13:10

Aaron