Cassandra. How to create column family using FluentCassandra API?

945 views Asked by At

I'm trying to create column family with this code

 

    public void CreateAColumnFamily()
     {
      using (var db = new CassandraContext(keyspace:"Earth", 
        server: Server))
      {
        CassandraColumnFamily cf = db.GetColumnFamily("People");
        if (cf == null)
        {
          db.ExecuteNonQuery(@"CREATE COLUMNFAMILY People (
            KEY ascii PRIMARY KEY,
            FirstName text,
            LastName text,
            Age int,
            Title text
    );");
        }
        cf = db.GetColumnFamily("People");
      }
    }

But nothing to happening. If i just to execute command create column family People in cassandra-cli - column family is creating. What i do wrong?

1

There are 1 answers

0
Lyuben Todorov On

Your code assumes that they column family 'People' already exists, and it shouldn't if you are trying to create it.

db.GetColumnFamily("People"); // means we are getting an existing CF

To create a column family:

db.TryCreateColumnFamily(new CassandraColumnFamilySchema
{
    FamilyName = "Tags",
    KeyValueType = CassandraType.AsciiType,
    ColumnNameType = CassandraType.Int32Type,
    DefaultColumnValueType = CassandraType.UTF8Type
});

Source: Example from fluent cassandra github.

Also as a side note. I struggled with finding documentation for fluent cassandra and it looks like you did too. If you are learning Cassandra try out the datastax C# driver... lots of documentation and a bigger community behind it.