Where Condition

ColumnFamily

x.ColumnFamily == "columnfamily1"

Key

x.Key == "key1"
x.Key.In("key1", "key2", "key3")
x.Key >= 1  or  x.Key.GreaterThanOrEqual(1)
x.Key <= 1  or  x.Key.LessThanOrEqual(1)
x.Key >= 1 && x.Key <= 5  or  x.Key.Between(1, 5)

Retrieved CassandraEntity count is equal Key count.
Example, you specify Key.In("1", "2", "3"), three CassandraEntity is returned.

Column, SuperColumn

x.Column == "column1"
x.Column.In(1, 2, 3)
x.Column >= 1  or  x.Column.GreaterThanOrEqual(1)
x.Column <= 1  or  x.Column.LessThanOrEqual(1)
x.Column >= 1 && x.Column <= 5  or  x.Column.Between(1, 5)

Index

x.Index["ColumnName"] == "column1"
x.Index["ColumnName"] >= 1
x.Index["ColumnName"] <= 1
x.Index["ColumnName"] > 1
x.Index["ColumnName"] < 1





Logic of Select Method Call Type


Index["column"] == "column1"  ->  get_indexed_slices

Key == "key1"  ->  retrieve type is Column or SuperColumn  ->  get
Key == "key1"  ->  retrieve type is ColumnList or SuperColumnList  ->  get_slice

Key.In("key1", "key2")  ->  multiget_slice

Other  ->  get_range_slices




Matrix

Column SuperColumn ColumnList SuperColumnList
- - - bad bad OK OK
- - Column=="c1" bad bad bad bad
- - Column.In("c1", "c2") bad bad OK OK
- - Column.Between("c1", "c2") bad bad OK OK
- SuperColumn=="sc1" - bad Error OK Error
- SuperColumn=="sc1" Column=="c1" bad Error bad Error
- SuperColumn=="sc1" Column.In("c1", "c2") bad Error OK Error
- SuperColumn=="sc1" Column.Between("c1", "c2") bad Error OK Error
- SuperColumn.In("sc1", "sc2") - bad bad OK OK
- SuperColumn.In("sc1", "sc2") Column=="c1" Error Error Error Error
- SuperColumn.In("sc1", "sc2") Column.In("c1", "c2") bad bad bad bad
- SuperColumn.In("sc1", "sc2") Column.Between("c1", "c2") Error Error Error Error
- SuperColumn.Between("sc1", "sc2") - bad bad OK OK
- SuperColumn.Between("sc1", "sc2") Column=="c1" bad bad bad bad
- SuperColumn.Between("sc1", "sc2") Column.In("c1", "c2") Error Error Error Error
- SuperColumn.Between("sc1", "sc2") Column.Between("c1", "c2") bad bad bad bad
Key=="k1" - - Error Error OK OK
Key=="k1" - Column=="c1" OK Error bad bad
Key=="k1" - Column.In("c1", "c2") Error Error OK OK
Key=="k1" - Column.Between("c1", "c2") Error Error OK OK
Key=="k1" SuperColumn=="sc1" - Error OK OK Error
Key=="k1" SuperColumn=="sc1" Column=="c1" OK Error bad Error
Key=="k1" SuperColumn=="sc1" Column.In("c1", "c2") Error Error OK Error
Key=="k1" SuperColumn=="sc1" Column.Between("c1", "c2") Error Error OK Error
Key=="k1" SuperColumn.In("sc1", "sc2") - Error Error OK OK
Key=="k1" SuperColumn.In("sc1", "sc2") Column=="c1" Error Error Error Error
Key=="k1" SuperColumn.In("sc1", "sc2") Column.In("c1", "c2") Error Error bad bad
Key=="k1" SuperColumn.In("sc1", "sc2") Column.Between("c1", "c2") Error Error Error Error
Key=="k1" SuperColumn.Between("sc1", "sc2") - Error Error OK OK
Key=="k1" SuperColumn.Between("sc1", "sc2") Column=="c1" Error Error bad bad
Key=="k1" SuperColumn.Between("sc1", "sc2") Column.In("c1", "c2") Error Error Error Error
Key=="k1" SuperColumn.Between("sc1", "sc2") Column.Between("c1", "c2") Error Error bad bad
Key.In("k1", "k2") - - bad bad OK OK
Key.In("k1", "k2") - Column=="c1" bad bad bad bad
Key.In("k1", "k2") - Column.In("c1", "c2") bad bad OK OK
Key.In("k1", "k2") - Column.Between("c1", "c2") bad bad OK OK
Key.In("k1", "k2") SuperColumn=="sc1" - bad Error OK Error
Key.In("k1", "k2") SuperColumn=="sc1" Column=="c1" bad Error bad Error
Key.In("k1", "k2") SuperColumn=="sc1" Column.In("c1", "c2") bad Error OK Error
Key.In("k1", "k2") SuperColumn=="sc1" Column.Between("c1", "c2") bad Error OK Error
Key.In("k1", "k2") SuperColumn.In("sc1", "sc2") - bad bad OK OK
Key.In("k1", "k2") SuperColumn.In("sc1", "sc2") Column=="c1" Error Error Error Error
Key.In("k1", "k2") SuperColumn.In("sc1", "sc2") Column.In("c1", "c2") bad bad bad bad
Key.In("k1", "k2") SuperColumn.In("sc1", "sc2") Column.Between("c1", "c2") Error Error Error Error
Key.In("k1", "k2") SuperColumn.Between("sc1", "sc2") - bad bad OK OK
Key.In("k1", "k2") SuperColumn.Between("sc1", "sc2") Column=="c1" bad bad bad bad
Key.In("k1", "k2") SuperColumn.Between("sc1", "sc2") Column.In("c1", "c2") Error Error Error Error
Key.In("k1", "k2") SuperColumn.Between("sc1", "sc2") Column.Between("c1", "c2") bad bad bad bad
Key.Between("k1", "k2") - - bad bad OK OK
Key.Between("k1", "k2") - Column=="c1" bad bad bad bad
Key.Between("k1", "k2") - Column.In("c1", "c2") bad bad OK OK
Key.Between("k1", "k2") - Column.Between("c1", "c2") bad bad OK OK
Key.Between("k1", "k2") SuperColumn=="sc1" - bad Error OK Error
Key.Between("k1", "k2") SuperColumn=="sc1" Column=="c1" bad Error bad Error
Key.Between("k1", "k2") SuperColumn=="sc1" Column.In("c1", "c2") bad Error OK Error
Key.Between("k1", "k2") SuperColumn=="sc1" Column.Between("c1", "c2") bad Error OK Error
Key.Between("k1", "k2") SuperColumn.In("sc1", "sc2") - bad bad OK OK
Key.Between("k1", "k2") SuperColumn.In("sc1", "sc2") Column=="c1" Error Error Error Error
Key.Between("k1", "k2") SuperColumn.In("sc1", "sc2") Column.In("c1", "c2") bad bad bad bad
Key.Between("k1", "k2") SuperColumn.In("sc1", "sc2") Column.Between("c1", "c2") Error Error Error Error
Key.Between("k1", "k2") SuperColumn.Between("sc1", "sc2") - bad bad OK OK
Key.Between("k1", "k2") SuperColumn.Between("sc1", "sc2") Column=="c1" bad bad bad bad
Key.Between("k1", "k2") SuperColumn.Between("sc1", "sc2") Column.In("c1", "c2") Error Error Error Error
Key.Between("k1", "k2") SuperColumn.Between("sc1", "sc2") Column.Between("c1", "c2") bad bad bad bad
Index["Column1"] == "c1" - - bad bad OK OK
Index["Column1"] == "c1" - Column=="c1" bad bad bad bad
Index["Column1"] == "c1" - Column.In("c1", "c2") bad bad OK OK
Index["Column1"] == "c1" - Column.Between("c1", "c2") bad bad OK OK
Index["Column1"] == "c1" SuperColumn=="sc1" - bad Error OK Error
Index["Column1"] == "c1" SuperColumn=="sc1" Column=="c1" bad Error bad Error
Index["Column1"] == "c1" SuperColumn=="sc1" Column.In("c1", "c2") bad Error OK Error
Index["Column1"] == "c1" SuperColumn=="sc1" Column.Between("c1", "c2") bad Error OK Error
Index["Column1"] == "c1" SuperColumn.In("sc1", "sc2") - bad bad OK OK
Index["Column1"] == "c1" SuperColumn.In("sc1", "sc2") Column=="c1" Error Error Error Error
Index["Column1"] == "c1" SuperColumn.In("sc1", "sc2") Column.In("c1", "c2") bad bad bad bad
Index["Column1"] == "c1" SuperColumn.In("sc1", "sc2") Column.Between("c1", "c2") Error Error Error Error
Index["Column1"] == "c1" SuperColumn.Between("sc1", "sc2") - bad bad OK OK
Index["Column1"] == "c1" SuperColumn.Between("sc1", "sc2") Column=="c1" bad bad bad bad
Index["Column1"] == "c1" SuperColumn.Between("sc1", "sc2") Column.In("c1", "c2") Error Error Error Error
Index["Column1"] == "c1" SuperColumn.Between("sc1", "sc2") Column.Between("c1", "c2") bad bad bad bad

OK : You get good result.
Bad : Not occur Exception. But you get unexpected result.
Error : Thrown Exception.

# GreaterThanOrEqual and LessThanEqual are same as Between.




Special Condition

Count Condition

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	// get_count
	var entitys = from x in context.ColumnList
      		      where x.Key == "k1" &&
		            x.ColumnFamily == "Product"
		      select x;
				   
	int count = entitys.CountColumn();
	Console.WriteLine(count);   // display column count


	// multiget_count
	var entitys = from x in context.ColumnList
      		      where x.Key.In("k1", "k2") &&
		            x.ColumnFamily == "Product"
		      select x;
				   
	Dictionary<string, int> dic = entitys.CountColumn<string>();
	foreach(var key in dic.Keys)
	{
		Console.WriteLine(key + " = " + dic[key]);   // display column count
	}
}

Reverse Condition

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var entitys = from x in context.ColumnList
      		      where x.Key.In("k1", "k2") &&
		            x.ColumnFamily == "Shop" &&
		            x.Column >= 1L &&
		            x.Column <= 3L 
		      select x;
				   
	foreach(CassandraEntity<List<Column>> e in entitys.ReverseColumn())
	{
		foreach(Column c in e.Data)
		{
			Console.WriteLine(e.Key + " " + c.Name.ToInt64());
		}
	}
}

/* result
k1 3
k1 2
k1 1
k2 3
k2 2
k2 1
*/

Take Condition

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var entitys = from x in context.ColumnList
      		      where x.Key.In("k1", "k2") &&
		            x.ColumnFamily == "Shop" &&
		            x.Column >= 1L &&
		            x.Column <= 5L 
		      select x;
				   
	foreach(CassandraEntity<List<Column>> e in entitys.TakeColumn(2))
	{
		foreach(Column c in e.Data)
		{
			Console.WriteLine(e.Key + " " + c.Name.ToInt64());
		}
	}
}

/* result
k1 1
k1 2
k2 1
k2 2
*/

// If you don't call TakeColumn, default take amount is 100.

First Condition

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var entitys = from x in context.ColumnList
      		      where x.Key.In("k1", "k2") &&
		            x.ColumnFamily == "Shop" &&
		            x.Column >= 1L &&
		            x.Column <= 5L 
		      select x;
				   
	var first = entitys.FirstColumn();
	var firstOrDefault = entitys.FirstOrDefaultColumn();

	foreach(CassandraEntity<Column> e in first)
	{
		Console.WriteLine(e.Key + " " + e.Data.Name.ToInt64());
		// k1 1
		// k2 1
	}
	
	foreach(CassandraEntity<Column> e in firstOrDefault)
	{
		Console.WriteLine(e.Key + " " + e.Data.Name.ToInt64());
		// k1 1
		// k2 1
	}
}

Last Condition

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var entitys = from x in context.ColumnList
      		      where x.Key.In("k1", "k2") &&
		            x.ColumnFamily == "Shop" &&
		            x.Column >= 1L &&
		            x.Column <= 5L 
		      select x;
				   
	var last = entitys.LastColumn();
	var lastOrDefault = entitys.LastOrDefaultColumn();
	
	foreach(CassandraEntity<Column> e in last)
	{
		Console.WriteLine(e.Key + " " + e.Data.Name.ToInt64());
		// k1 5
		// k2 5
	}
	
	foreach(CassandraEntity<Column> e in lastOrDefault)
	{
		Console.WriteLine(e.Key + " " + e.Data.Name.ToInt64());
		// k1 5
		// k2 5
	}
}





Use CQL

You can write string query and execute it.

using(var context = new CassandraContext("localhost", 9160, "Keyspace1"))
{
	// insert
	string insertCql = "insert into ColumnFamily1 (KEY, name1, name2) values ('key1', 'value1', 'value2')"; 
	CqlResult insertResult = context.ExecuteCqlQuery(insertCql);
	
	// get count
	string countCql = "select count(*) from ColumnFamily1 where KEY = 'key1'";
	CqlResult countResult = context.ExecuteCqlQuery(countCql);
	Console.WriteLine(countResult.Num);	// 2
	
	// get data
	string retrieveCql = "select * from ColumnFamily1 where KEY = 'key1'";
	CqlResult retrieveResult = context.ExecuteCqlQuery(retrieveCql);
	
	var dictionary = retrieveResult.ToFlatDictionary<string, string>();
	dictionary.ToList().ForEach(kv => Console.WriteLine(kv.Key + " = " + kv.Value));
	// name1 = value1
	// name2 = value2
}





Other Sample

Query as Column

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var entitys = from x in context.Column
      		      where x.Key == "1" &&
		            x.ColumnFamily == "Product" &&
		            x.Column == "Name"
		      select x;
				   
	foreach(CassandraEntity<Column> e in entitys)
	{
		Console.WriteLine(e.Data.Name.ToInt32());
		Console.WriteLine(e.Data.Value.ToUTF8());
	}
}

Query as Object

using(var context = new CassandraContext("localhost", 9160, "KeySpace1"))
{
	var products = from x in context.ColumnList
		       where x.Key == "1" &&
			     x.ColumnFamily == "Product"
		       select x.ToObject<Product>();
				   
	foreach(Product p in products)
	{
		Console.WriteLine(p.ToString());
	}
}

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

Comments

No comments yet.