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 class generated by thrift api are added some extension methods.
And List<Column>, List<SuperColumn> class are added, too.

Column Extension Methods
  • SetName (object name)
  • SetValue (object value)
  • SetTimeStamp (long unixTime)
  • SetNameValue (object name, object value)
  • SetNameValueTimestamp (object name, object value, long unixTime)

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

SuperColumn Extension Methods
  • SetName (object name)
  • SetColumn (Column column)
  • SetColumn (object name, object value)
  • SetColumn (object name, object value, long unixTime)
  • 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)
  • AddColumnList (IEnumerable<Column> columnList)
  • AddColumnList (object obj) // obj fields are converted to columns

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

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.



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




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.GreaterThanOrEqual("c1")
  • Column.LessThanOrEqual("c1")
  • 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();
}

Last edited Jun 10, 2010 at 6:06 PM by sabro, version 4

Comments

No comments yet.