Monday, November 17, 2014

cassandra vs rdbms data model

If you are coming from a relational database world like me then the initial terminology used in cassandra will be a bit confusing.
I'll state some keywords which are used in cassandra and try to draw parallels of them to the relational world.

Note: These parallels are just for ease of understanding and actually does not equal to each other at all.

Cassandra keyword
RDBMS keyword
Keyspace
Database
Column family
Table
Column
Table Column
Row
Table row

The outermost element of Cassandra is a cluster which can be one or more nodes set up as a ring which appears as a single instance to the end user.

A cluster usually contains one or more keyspaces (Databases).

A keyspace contains one or more column families  (Tables)

A column family contains one or more rows. (Slightly different from a Table which would contain columns )

A row contains one or more columns.

The biggest difference is that the number and type of columns in rows can  differ from one another thus making the Cassandra column family much more flexible compared with the rigid table structure in the rdbms world.

In other words each row in  a column family can have its own schema. This is an extremely useful option for storing dynamic data

Sunday, August 3, 2014

Cannot read remote side has closed Thrift Cassandra C#

I was seeing this error ("Cannot read remote side has closed") while trying to get Thrift API to work with Cassandra using Csharp. For the life of me I couldn't figure out what was wrong as even though the error is saying something like "Hey I think your Thrift server is down". That was not the case because I was connecting to My Cassandra server using other client libraries that was using Thrift under the hood. Eventually I figured out the error, the offending line was transport.Open(); When I replaced it with cassandraClient.InputProtocol.Transport.Open(); The error went away.

Here is the full snippet,

            TSocket socket = null;

            TTransport transport = null;

        

            socket = new TSocket("localhost", 9160);

          

          

            transport = new TFramedTransport(socket);

            TProtocol protocol = new TBinaryProtocol(transport);

            CassandraClient cassandraClient = new CassandraClient(protocol);

            cassandraClient.InputProtocol.Transport.Open();



            string s = cassandraClient.describe_cluster_name();

            List<ksdef> keyspaces = cassandraClient.describe_keyspaces();





Hope this will help another poor soul.

Thursday, June 26, 2014

SQL GROUP BY Example

The GROUP BY clause causes data in a table (or any data source) to be divided into groups based on the expressions (or conditions) given in the GROUP BY clause. Seems confusing? Probably.. Let's see what it means,

Say you have Column1, Column 2..etc on your table.

Now you say GROUP BY Column1 , what this does is it divides your table's data based on Column1. So all data which has  Column1='Apple' will be in one group, then all data which has  Column1='Orange' will be in another group.

Again if you had GROUP BY Column1, Column2 then your data will be divided into groups of Column1 + Column2 combinations . If Column2 had values of Green and Red in different rows then one group would be based on Apple+Red another group Apple+Green. So you get the point.

So in a nutshell,

GROUP BY creates groups for output rows, according to unique combination of values specified in the GROUP BY clause

But why would you need to divide them into groups? So we can operate on those groups.

I'll use AdventureWorks database as an example.

Download it here,
https://msftdbprodsamples.codeplex.com/releases/view/93587


SELECT SalesPersonID, YEAR(OrderDate) AS OrderYear,
COUNT(CustomerID) AS All_Customers
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

The above query will return all unique combinations of SalesPersonID  + YEAR(OrderDate) and display all customers each of those combinations.

In other words this means you are seeing all customers for a particular Sales Person in a given year.

Point to note:

SELECT DISTINCT SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader

and

SELECT  SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

Will give you the same results.

Saturday, May 24, 2014

ASP.Net Webforms Inline tags and code blocks

There are many elements inside a webforms (.aspx) page. To somebody beginning asp.net webforms these may seem quite similar and be confusing at first. (Well it was for me when I started learning asp.net webforms)
So hopefully this might help another soul.

Elements of a asp.net page (Inline tags and code blocks) can be listed as follows,

1. Directives

These come in many flavors like Page directives,
 ex : <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Trace="true" %>

or  Import directives,
ex : <%@ namespace="System.Drawing" %>

There are many more directives other than these two.

2. ASP.Net expressions

This is expression builder syntax, typically used to get values from web.config
ex: <%$ ConnectionStrings:AdventureWorks2008 %>  

3. User Controls

These are references to user created controls which are similar to web controls that enhances reusability 
ex: <PH:MainMenu Title="Menu Control" id="mainmenu" runat="server" />

4. Render blocks

Used for outputting elements like variables
ex:  If you have a variable named age then,
<%= age %>
will output the value of age

5. Code blocks

These are similar to javascript tags just that these run on the server,
<script runat="Server" language="c#" > ....some server side code here .... </script>  

6. Server Controls

These are the controls we drag and drop from the designer (Labels, Buttons etc)
ex: <asp:Label id="myLabel" runat="Server" />

7. Data binding controls

Used for biniding expressions such as Eval or Bind, these are typically used with data controls like Gridview
ex : <%# Eval("DBFieldName") %>

   

Tuesday, March 25, 2014

DataContract vs Serializable WCF

When applied to a class Serializable attribute serializes everything inside of it including private fields thus the developer has no control as what to expose.

When DataContract attribute is applied to a class it will serialize only the elements decorated with DataMember attribute thus the developer will have more control.

ServiceContract Name property WCF

This property can give your contract a name which is different from the actual interface name. So this way if you decide to change the interface name of a already running WCF service your client wont break.

Ex:

 [ServiceContract(Name="IGoodbyeService")]

    public interface IHelloService

    {

        [OperationContract]

        string GetMessage(string name);

    }


Now the contract name would be IGoodbyeService rather than  IHelloService.

To verify in the WSDL generated check the porttype property it should be IGoodbyeService

Note: Same can be done for OperationContract as well 

Monday, March 24, 2014

Basic Elements of WCF Appconfig

All config items should be inside ,

<system.serviceModel>

This is the WCF namespace provided by Microsoft.

Inside the <system.serviceModel> element you have the <services> element. This is where you configure all your services.
For each service you have a <service> element inside <services> element.
A particular service can be exposed using more than one endpoint. (Ex: http , net.tcp etc)
So for each endpoint in a service you configure a <endpoint> element.
An endpoint element will have three essential things,

  1. address
  2. binding
  3. contract  
Here is a  sample config (not complete) to get an idea.

The service name is HelloService (class and namespace both are HelloService) and has two endpoints (http and net.tcp) for the service.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

Saturday, March 22, 2014

system.web vs system.webserver

I have not worked in IIS6 and mostly I have worked on IIS 7+ and this came up when I was following a very old article (2003) ASP.Net article.

Anyway system.web is IIS6 and classic mode.
system.webserver is IIS7+  and integrated mode.

One scenario you might come across is when adding httpmodules to your ASP.Net project. If you want backward compatibility then you should add the module section to both system.web section (which is there by default for all ASP.Net projects ) and to system.webserver.

<system.web>
    <httpModules>
    </httpModules>
</system.web>

And

<system.webServer>
    <modules>
    </modules>
</system.webServer>


Sunday, March 9, 2014

SQL JOIN Example

Joins can be quite perplexing initially hopefully this simple example will help. 

Joins are used to combine data from multiple tables (usually two tables)

There are three types of joins,

1. INNER
2. OUTER - FULL, LEFT, RIGHT
3. CROSS


For all examples I'll be using the below given tables and use the Name field as the condition.

                                                                         Table1
Id1
Name1
1
Padmika
2
Dissanayake
3
Sukitha
4
Pahan


                                                                        Table2
Id2
Name2
1
Fernando
2
Malaka
3
Padmika
4
Sukitha


1. INNER JOIN

Returns only the set of records that match the Name field in both Table 1 and Table2

SELECT * FROM Table1
INNER JOIN Table2
ON Table1.Name1 = Table2.Name2
Result,

Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
3
Sukitha
4
Sukitha

* This gets executed in the following manner, first get the first record from Table 1 get its name field compare it to Table 2 first record name field if it’s a match return it, now compare with Table2 second record name field…  etc.

You will see that only the records that have a common name is returned
This is similar to the intersection of two sets (Table1 and Table2) based on Name.  


2. OUTER JOIN

2.1 FULL OUTER JOIN

Returns all records in Table 1 and Table 2, with matching records from both sides where available. If there is no match, the missing side will contain null.

SELECT * FROM Table1
FULL OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2

Result,
Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
2
Dissanayake
null
Null
3
Sukitha
4
Sukitha
4
Pahan
null
Null
null
Null
1
Fernando
null
Null
2
Malaka

This is similar to the union of two sets (Table1 and Table2) based on Name.

2.2 LEFT OUTER JOIN

 Returns the complete set of records from Table 1 (Left table), with the matching records for Name field (where available) in Table 2. If there is no match, the right side will contain null.

SELECT * FROM Table1
LEFT OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2

Results,

Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
2
Dissanayake
null
null
3
Sukitha
4
Sukitha
4
Pahan
null
null



Special case of the above result would be to select records that do not have any matching records from the right table (Table2)

To do this just add a ‘WHERE Table2.Id2 IS NULL’ clause to the above query.

Ex: You have a Customer table and an Order table (Has a CustomerID column) and want to find out which customers have not placed any orders  

2.3 RIGHT OUTER JOIN

Similar to the above but all the right table (Table2) records will return rather than the left table ones.

SELECT * FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2


Results,

Id1
Name1
Id2
Name2
null
null
1
Fernando
null
null
2
Malaka
1
Padmika
3
Padmika
3
Sukitha
4
Sukitha


Note : When inspected carefully you will see that in this example,
OUTER JOIN result = LEFT JOIN result + RIGHT JOIN result – INNER JOIN result

Saturday, March 8, 2014

SQL COUNT Example

COUNT returns the number of rows in a select statement (Also note that it doesn't consider null values),

Note : I'll be using the AdventureWorks2008R2 sample database which can be downloaded from here,

http://msftdbprodsamples.codeplex.com/releases/view/59211


Example 1,

/* By using the * we return the number of records in the Sales.Customer table */
select

count(*)

from Sales.Customer;



Example 2,

/* By using StoreID inside the COUNT() function we return the number of store id's in the Sales.Customer table */

select

count(StoreID)

from Sales.Customer;


Example 3,
  /* By using "DISTINCT StoreID" inside the COUNT() function we return the unique number of store id's in the Sales.Customer table */

select

count(distinct StoreID)

from Sales.Customer;
Note: From a performance point of view it is better to use 1 instead of * inside the COUNT function.

Saturday, March 1, 2014

DDL vs DML in SQL Server

DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Examples:

  • CREATE - to create objects in the database
  • ALTER - alters the structure of the database
  • DROP - delete objects from the database

DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Examples:


  • SELECT - retrieve data
  • INSERT - add data
  • UPDATE - modify data
  • DELETE - remove data
Note : This is not limited to SQL Server

Friday, February 28, 2014

SQL Server Data Types

Here are the SQL server data types for quick reference,

  1. Exact numerics – (bigint, bit, decimal, int, money, numeric, smallint)
  2. Approximate numerics (float, real)
  3. Date and time (date, datetime2, datetime, datetimeoffset, time)
  4. Character strings (char, varchar, text)
  5. Unicode character strings (nchar, ntext, nvarchar)
  6. Binary strings (binary, varbinary, image)
  7. Other data types (cursor, timestamp, uniqueidentifier, table)
  8. Large valued data types (varchar(max), nvarchar(max))
  9. Large object data types (text, ntext, image, xml)

Sunday, February 16, 2014

ASP.NET ViewState example

ViewState is a client side state management mechanism. It is not the only state management mechanism. Some other examples would be,

For client side state management,
·         Hidden Field
·         Cookies
·         Control State

For server side,
·         Session
·         Application Object
·         Caching
·         Database

ViewState should be used to persist only small amounts of data as having lots of data in ViewState will degrade performance.

Also by default ViewState does not encrypt data but only encodes them to Base64. So generally sensitive data should not be kept there.

However you can encrypt ViewState by setting  ViewStateEncryptionMode="Always" as a page level directive,

<%@Page ViewStateEncryptionMode="Always" %>

 or for all pages by setting in web.config,

<configuration>
   <system.web>
      <pages ViewStateEncryptionMode="Always" />
   </system.web>
</configuration>

There is a lot of confusion about ViewState and Form input preservation on postbacks.
An example would be, how a textbox persists data even when View State is set to off.
This is because many ASP.Net controls (TextBox, ListBox, RadioButtonList etc) have implemented the IPostBackDataHandler interface and calls the LoadPostData(), which repopulates them.

Here is a small example of using ViewState persist state,
  1. Create a ASP.Net web application 
  2. Add a Web Form named ViewStateExample
  3. Paste the below given code to the apsx and apsx.cs files respectively
  4. Make sure to rename of the namespace LearnASPWebForms to your namespace


  
ViewStateExample.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewStateExample.aspx.cs" Inherits="LearnASPWebForms.ViewStateExample" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>ViewStateExample</title>

</head>

<body>

    <form id="testform" runat="server">

        <asp:Label runat="server" Text="Enter a Value" />

        <asp:TextBox runat="server" id="FieldToBePreserved" /><br />

        <asp:Button runat="server" id="SubmitForm" onclick="SubmitForm_Click" text="Submit and add to ViewState" />

        <asp:Button runat="server" id="SubmitDontSave" text="Submit but dont save to ViewState" OnClick="SubmitDontSave_Click"  />

        <br /><br />

        Value from ViewState: <asp:Label runat="server" id="ViewStateValue" BorderStyle="Dotted" />

    </form> 

</body>

</html>



CodeBehind ViewStateExample.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LearnASPWebForms
{
    public partial class ViewStateExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["Field"] != null)
                ViewStateValue.Text = ViewState["Field"].ToString();
            else
                ViewStateValue.Text = "Nothing in ViewState";
        }

        protected void SubmitForm_Click(object sender, EventArgs e)
        {
            ViewState["NameOfUser"] = FieldToBePreserved.Text;
            ViewStateValue.Text = FieldToBePreserved.Text;
        }

        protected void SubmitDontSave_Click(object sender, EventArgs e)
        {
            //This does nothing
        }

    }
}

Tuesday, February 4, 2014

Could not load file or assembly EntityFramework, Version=6.0.0.0 and MVC 4

In MVC 4 this mostly happens when you create a MVC 4 project and then for your DAL (Data access layer ) you have a separate class library project and install EF through NuGet. NuGet will give you the latest version which is currently EF 6. When you try to run the project you will see an error similar to the above with a yellow screen of death. The root cause is that MVC 4 has  EF5 referenced. So just open up NuGet and upgrade  EF to version 6 under updates section and you are good to go.

Saturday, February 1, 2014

ViewBag vs ViewData in ASP.NET MVC

Bottom line is that they are essentially the same, ViewBag is just a dynamic wrapper around ViewData. ViewData is actually a dictionary used to pass data from controllers to views. The following example will make this clear,

ViewBag.Name = "Padmika"

is equal to,

ViewData["Name"] = "Padmika"

Saturday, January 25, 2014

Browser definition safari3plus not found : Blackberry redirection not working

One of the clients of the product that I work on had a problem where a redirection to an apps download page was not working on Blackberry 6.0 and Blackberry 7.0 OS  devices and was working fine for BB 5.0 OS. Strangely this was working fine until the client had installed some updates from Microsoft.

When digging in to the issue the root cause was that there is a custom browser definition for BB 6.0 and BB 7.0 browsers which had a different user agent string compared against BB 5.0 and this browser definition file was having a parent of  "safari3to4". However the machine had an extra browser definition for "safari3plus" under,

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Browsers\safari.browser and
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Browsers\safari.browser

This was causing the custom browser definition not getting picked up as the parent ID of it was safari3to4.
The fix is (rather than removing safri3plus definition of the above mentioned files ) to rename the parent Id of the custom browser definition file under App_Browsers in the IIS deployed folder, this doesn't even require an IIS reset or an app pool  recycle.

Microsoft changes these browser definition files from time to time and  developers who use Request.Browser property to do re-directions should be aware of this fact.