How to query with FluentCassandra

906 Views Asked by At

I'm new to Cassandra and to FluentCassandra (C#.NET) too. I have heavy concepts of RDBMS (relational databases), like Microsoft SQL.

Well... i was able to do some simple code, but, that is far of what i do when consulting with ADO.NET in RDBMS.

What i did:

var context = new CassandraContext(keyspace: "keyspace", host: "ipAddress"))

var columnFamily = context.GetColumnFamily("Test");

var record = records.Get("joao").FirstOrDefault();

foreach (FluentColumn colunm in record.Columns)
{
    Console.WriteLine("{0}", colunm.ColumnValue);
}

Well, the row i have the key "joao" is a CompositeColumn, but the result went to me as a linear sequence os columns, like you can see at the foreach stantement.

Well, now you know all this, i can expose my doubts:

How can i apply filters like say something like:

select Name, Birthday, Age, MiddleName, LastnName,
from Test
where Key = 'joao'
and Age > 18

I dont know how to do it using FluentCassandra. Can i use the .Get() method to do that? If yes, how? Remember i have Composite columns, i dont know too how to work with them in FluentCassandra...

If i was using ADO.NET for example i just wad do something like:

SqlConnection connection = new SqlConnection("connectrion string");
string query = "select Name, Birthday, Age, MiddleName, LastnName, from Test where Key = 'joao' and Age > 18";
SqlDataAdapter adapter = new SqlDataAdpter(connection, query);
Dataset users = new datSet();
adapter.Fill(users);

After this i had all the information in "users" DataSet object.

I really would like to perform the same oprations on Cassandra database using FluentCassandra, but i'm a little (well, much) lost. I spent a lot of time searching for this, but no success, the documentation and examples are poor...

Does anyone can help, please?

Thanks.


Well, i did:

var context = new CassandraContext(keyspace: "KeySpace", host: "IpAddress");

var results = context.ExecuteQuery("select * from Test");

And i have the return, but what is returned is just the KEY column, the others columns doesn't.

I tried others ways to do the select but all that have the same result: just the rows with only the KEY of the row.

Any idea?

Thanks.

2

There are 2 best solutions below

0
On

Is it possible to read just a selected number of columns with one query from a column family unless they're a in a consecutive range within the sorted column set ?

0
On

executeQuery will return you a list of IcqlRow entities. From that you should be able to loop through the columns in each row.

Here's the method I use to convert a CqlRow to a known object:

var rows = db.ExecuteQuery(cql).ToList();
var entities = rows.Select(row => ToEntity(row)).ToList();

function DataEntity ToEntity(ICqlRow row)
{
    var obj = new DataEntity();
    foreach (var col in row.Columns)
    {
        var info = data.GetType().GetProperty(col.ColumnName.GetValue<string>());
        if (col.ColumnValue != null)
            info.SetValue(data, col.ColumnValue.GetValue(info.PropertyType), null);
    }
    return obj;
}

If all your getting is the Key column and nothing else that is weird. Have you confirmed that the column family does actually contain the columns you are expecting? What do you get when you query the CF using Cassandra CLI?