A Quick introduction to Hadoop Hive on Azure and Querying Hive using LINQ in C#

ANOOP MADHUSUDANAN

Vote on HN

imageEarlier, in a couple of posts related to Hadoop on Azure - Analyzing some ‘Big Data’ using C# and Extracting Top 500 MSDN Links from Stack Overflow – I showed how to use C# Map Reduce Jobs with Hadoop Streaming to do some meaningful analytics.

Now, a preview version of the .NET SDK for Hadoop is available, making it easier to work with Hadoop from .NET – with more types for supporting Map Reduce Jobs, For creating LINQ to Hive queries etc.  You can experiment with Hadoop and C# either by creating a cluster in http://hadooponazure.com or you can obtain Hadoop in your machine by installing Microsoft HDInsight using WebPI.

In case you are new to Hadoop on Azure, I suggest you read the introductory concepts here before you start. This post is just a quick example that shows how to use LINQ to Hive.

Hive is a data warehouse system for Hadoop that facilitates easy data summarization, ad-hoc queries, and the analysis of large datasets stored in Hadoop compatible file systems

Installing the libraries

To start with, you can fire up Visual Studio, Create a console project, and install Microsoft.Hadoop.Hive libraries via Nuget.

install-package Microsoft.Hadoop.Hive -pre 

Also, head over to http://hadoopoonazure.com and create a new cluster. And you are now set.

Creating the typed wrappers

To access Hive, you need to create a strongly typed wrapper – as of now, you need to roll this out your own, as there is no automated generation support. When you provision a Hadoop cluster, the Hive will be pre populated with a sample table (hivesampletable), and I’m using the same for the below example for brevity.  You can connect to the Hive via ODBC and see the hive tables in Excel.

So, let us go ahead and create a hive connection (much like an EF data context) and a typed representation for a row in the table. HiveConnection and HiveTable types are in the Microsoft.Hadoop.Hive namespace.

    
    //Our concrete hive connection
    
    public class SampleHiveConnection : HiveConnection
    {
        public SampleHiveConnection(string hostName, int port) 
            : base(hostName, port, null, null) { }

        public SampleHiveConnection(string hostName, int port, 
                            string username, string password) 
            : base(hostName, port, username, password) { }

        public HiveTable<DeviceInfo> DeviceInfoTable
        {
            get
            {
                return this.GetTable<DeviceInfo>("hivesampletable");
            }
        }
    }

    //A typed row. Property names based on field names hivesampletable
    
    public class DeviceInfo : HiveRow
    {
        public string DevicePlatform { get; set; }
        public string DeviceMake { get; set; }
        public int ClientId { get; set; }
    }

Querying the Hive using LINQ

Now,  you may perform LINQ queries against your Hive context, thanks to the Hadoop SDK we installed via Nuget. Just make sure to substitute the connection string, username and password with your own.

class Program
    {
        static void Main(string[] args)
        {


            //Create a hive connection
            //I've my cluster in https://www.hadooponazure.com
            var hive = new SampleHiveConnection(
                    "saintcluster.cloudapp.net", //your connection string
                    10000,                       //port                    
                    "user",                      //your username
                    "yourpass");                 //your password


            //Get the results
            //Make sure you goto the dashboard and turn on the ODBC port
            var res = from d in hive.DeviceInfoTable
                      where d.ClientId < 100
                      select d;

            //Dump it to the console if you like
            var list = res.ToList();     

        }
    }
That is cool. Your LINQ query will be submitted to the Azure cluster via the ODBC driver, and will be compiled and executed in the Hive.
© 2012. All Rights Reserved. Amazedsaint.com