Regist Operation

Build Entity

First, you should build CassandraEntity instance. You can ignore Column and SuperColumn field becouse of them only used for Query.

CassandraEntity has useful method. SetColumnFamily and SetKey speak for itself. SetData methods set data for store. You give object to SetData method, The object is converted to column list that column name set field name and column value set field value by reflection. Then data convert byte array rightly. UTF8 encoding for string, Big Endian for numeric type, Big Endian ticks for DateTime and TimeSpan, ToByteArray for Guid(TimeUUID), And binary serialize for other object. You want to build SuperColumn entity, then you can use SetData(object obj, object columnName) overload. Obj parameter convert column list and columnName parameter set SuperColumn's name. In Addition you can use SetDataAndColumnFamily methods. Then ColumnFamily field set type name of obj parameter automatically.

CassandraEntity Extension Methods
  • SetColumnFamily (string columnFamily)
  • SetKey (string key)
  • SetData (T data)
  • SetData (object obj)
  • SetData (object obj, object columnName)
  • SetDataAndColumnFamily (object obj)
  • SetDataAndColumnFamily (object obj, object columnName)

Column and SuperColumn and CounterColumn and CounterSuperColumn class generated by thrift api are added some extension methods.
And List<Column>, List<SuperColumn>, List<CounterColumn>, List<CounterSuperColumn> class are added, too.

Column Extension Methods
  • SetName (object name)
  • SetValue (object value)
  • SetTimeStamp (long unixTime)
  • SetTtl (int ttl)
  • SetNameValue (object name, object value)
  • SetNameValueTimestamp (object name, object value, long unixTime)
  • SetNameValueTtl (object name, object value, int ttl)
  • SetNameValueTimestampTtl (object name, object value, long unixTime, int ttl)

List<Column> Extension Methods
  • Add (object name, object value)
  • Add (object name, object value, long unixTime)
  • Add (object name, object value, int ttl)
  • Add (object name, object value, long unixTime, int ttl)
  • SetTtl (int ttl)

SuperColumn Extension Methods
  • SetName (object name)
  • SetColumn (Column column)
  • SetColumn (object name, object value)
  • SetColumn (object name, object value, long unixTime)
  • SetColumn (object name, object value, int ttl)
  • SetColumn (object name, object value, long unixTime, int ttl)
  • SetColumnList (IEnumerable<Column> columnList)
  • SetColumnList (object obj) // obj fields are converted to columns
  • AddColumn (Column column)
  • AddColumn (object name, object value)
  • AddColumn (object name, object value, long unixTime)
  • AddColumn (object name, object value, int ttl)
  • AddColumn (object name, object value, long unixTime, int ttl)
  • AddColumnList (IEnumerable<Column> columnList)
  • AddColumnList (object obj) // obj fields are converted to columns
  • SetTtl (int ttl)

List<SuperColumn> Extension Methods
  • Add (object name, Column column)
  • Add (object name, object columnName, object columnValue)
  • Add (object name, IEnumerable<Column> columnList)
  • Add (object name, object obj) // obj fields are converted to columns
  • SetTtl (int ttl)

CounterColumn
  • SetName (object name)
  • SetValue (long value)
  • SetNameValue (object name, long value)

List<CounterColumn>
  • Add (object name, long value)

CounterSuperColumn
  • SetName (object name)
  • SetColumn (CounterColumn column)
  • SetColumn (object name, long value)
  • SetColumnList (IEnumerable<CounterColumn> columnList)
  • AddColumn (CounterColumn column)
  • AddColumn (object name, long value)
  • AddColumnList (IEnumerable<CounterColumn> columnList)

List<CounterSuperColumn>
  • Add (object name, CounterColumn column)
  • Add (object name, object columnName, long columnValue)
  • Add (object name, IEnumerable<CounterColumn> columnList)

Operation

	context.Column.InsertOnSubmit(entity);
	context.SubmitChanges();
If you build entity, Let's call InsertOnSubmit method and give the entity. Cassandraemon doesn't regist this timing actually. After, you call SubmitChanges method then regist job is executed. If you have not set column timestamp, then Cassandraemon set now timestamp (Unix Time but milliseconds interval) automatically.

Shortcut Operation

	// old version
	context.Column.InsertOnSubmit(cassandraEntity1);
	context.SuperColumn.InsertOnSubmit(cassandraEntity2);
	context.ColumnList.InsertOnSubmit(cassandraEntity3);
	context.SuperColumnList.InsertOnSubmit(cassandraEntity4);

	// shortcut version
	context.InsertOnSubmit( "ColumnFamily1", "key1", column );
	context.InsertOnSubmit( "ColumnFamily2", "key2", superColumn );
	context.InsertOnSubmit( "ColumnFamily3", "key3", columnList );
	context.InsertOnSubmit( "ColumnFamily4", "key4", superColumnList );

	context.SubmitChanges();
New Insert method overload are added in ver0.8. Now, you don't have to create CassandaEntity object.


Regist Sample

Insert as Column

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var column = new Column().SetNameValue("name1", "value1");
	
	var entity = new CassandraEntity<Column>()
		        .SetColumnFamily("Product")
		        .SetKey("key1")
		        .SetData(column);
	
	context.Column.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert as SuperColumn

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var superColumn = new SuperColumn()
			.SetName("scName1")
			.AddColumn("name1", "value1")
			.AddColumn("name2", "value2");

	var entity = new CassandraEntity<SuperColumn>()
		        .SetColumnFamily("Product")
		        .SetKey("key1")
		        .SetData(superColumn);
	
	context.SuperColumn.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert as ColumnList

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var columnList = new List<Column>()
			.Add("name1", "value1")
			.Add("name2", "value2");

	var entity = new CassandraEntity<List<Column>>()
		        .SetColumnFamily("Product")
		        .SetKey("key1")
		        .SetData(columnList);
	
	context.ColumnList.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert as SuperColumnList

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var columnList = new List<Column>()
			.Add("name1", "value1")
			.Add("name2", "value2");

	var superColumnList = new List<SuperColumn>()
			.Add("scName1", columnList)
			.Add("scName2", "name3", "value3");

	var entity = new CassandraEntity<List<SuperColumn>>()
		        .SetColumnFamily("Product")
		        .SetKey("key1")
		        .SetData(superColumnList);
	
	context.SuperColumnList.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert SuperColumn from Object

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var product = new Product { ID = 1, Name = "Product1" };
	
	var entity = new CassandraEntity<SuperColumn>()
		        .SetKey(product.ID.ToString())
		        .SetDataAndColumnFamily(product, "scName1");
	
	context.SuperColumn.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert ColumnList from Object

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var product = new Product { ID = 1, Name = "Product1" };
	
	var entity = new CassandraEntity<List<Column>>()
		        .SetKey(product.ID.ToString())
		        .SetDataAndColumnFamily(product);
	
	context.ColumnList.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert SuperColumnList from Object

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var product1 = new Product { ID = 1, Name = "Product1" };
	var product2 = new Product { ID = 2, Name = "Product2" };

	var superColumnList = new List<SuperColumn>()
			.Add("scName1", product1)
			.Add("scName2", product2);

	var entity = new CassandraEntity<List<SuperColumn>>()
		        .SetKey("key1")
		        .SetColumnFamily("Product")
		        .SetData(superColumnList);
	
	context.SuperColumnList.InsertOnSubmit(entity);
	context.SubmitChanges();
}

Insert ColumnList by CQL

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	string insertCql = "insert into ColumnFamily1 (KEY, name1, name2) values ('key1', 'value1', 'value2')"; 
	CqlResult insertResult = context.ExecuteCqlQuery(insertCql);
}



Delete Operation

Operation

    context.Column.DeleteOnSubmit(x => 
	    x.Key == "1" && 
	    x.ColumnFamily == "Product" &&
	    x.Column == "Name".ToCassandraByte());
							  
    context.SubmitChanges();
You want to delete data. You should call DeleteOnSubmit method. Parameter of DeleteOnSubmit method is lambda expression. You can use following condition for lambda expression.

ColumnFamily
  • ColumnFamily == "cf1"

Key
  • Key == "k1"

Column, SuperColumn
  • Column == "c1"
  • Column.In("c1", "c2")
  • Column >= "c1"  or  Column.GreaterThanOrEqual("c1")
  • Column <= "c1"  or  Column.LessThanOrEqual("c1")
  • Column >= "c1" && Column <= "c2"  or  Column.Between("c1", "c2")

Delete Sample

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
    context.Column.DeleteOnSubmit(x => 
	    x.Key == "1" && 
	    x.ColumnFamily == "Product" &&
	    x.Column == "Name".ToCassandraByte());
							  
    context.SubmitChanges();
}




Schema Update Operation

Operation

	CfDef cf = new CfDef();
	cf.Keyspace = "Keyspace1";
	cf.Name = "ColumnFamily1";
	cf.Column_type = "Standard";
	cf.Comparator_type = "LongType";
	
	context.SystemAddColumnFamily(cf);

You want to update schema. You should call schema methods belong to CassandraContext. You don't have to call SubmitChanges method.

class CassandraContext
{
	public string SystemAddKeyspace(KsDef ksDef)
	public string SystemUpdateKeyspace(KsDef ksDef)
	public string SystemDropKeyspace(string keyspace)
	public string SystemAddColumnFamily(CfDef cfDef)
	public string SystemUpdateColumnFamily(CfDef cfDef)
	public string SystemDropColumnFamily(string columnFamily)
	public void Truncate(string columnFamily)
}

Schema Update Sample

using(var context = new CassandraContext("localhost", 9160, "Keyspace1"))
{
	CfDef cf = new CfDef();
	cf.Keyspace = "Keyspace1";
	cf.Name = "ColumnFamily1";
	cf.Column_type = "Super";
	cf.Comparator_type = "LongType";
	cf.Subcomparator_type = "BytesType";
	
	context.SystemAddColumnFamily(cf);
}

Last edited Dec 18, 2011 at 9:44 AM by sabro, version 1

Comments

No comments yet.