Month: August 2016

Experience with Azure Big Data Analyzing

Introduction

This experiment was done in order to have first understanding on how to deal with big data on cloud. So this is my first time experience with lots of new technologies such as Hadoop, Hive etc. Before the experiment I have tried few examples with Hadoop and other related technologies, and yet found that this would be a better way to go.

Use case

This experiment is based on the dataset with annual salaries for different persons. In this case this is about California, USA in 2015. The data set contained names, job titles, basic salary, additional benefits and total salary. So in this, I’ll concentrate on averages of job titles and their averaged total salaries.

Please note that this is an experiment which is solely depends on the dataset. Actual survey reports may contain values different from final results.

Prerequisites

Before moving to the rest of the article, you are expected to have better understanding on followings:

  • Your understanding on Microsoft Azure platform is necessary. You can create a free azure account and try to play around with portal. Also you need to understand their concepts such as Resources, Resource Groups etc.
  • You need to have some sort of understanding on big data concepts such as what is big data, Hadoop DFS, Hadoop Eco system, Hive, SQL etc.
  • Better to have done some tutorials in Azure documentation. Specially “Analyze flight delay data”, which laid the foundation for this article.
  • Understanding on HTML, PHP, JS with AJAX is required for understand visualization part.
  • Using tools like Putty, WinSCP if you are in Windows, else commands related to scp and ssh

Planning Deployment

Deployment required for this article is can be shown as follows.

Azure Deployment

Please note that I’ll shutdown HD Insight cluster once it completed its job, else you’ll lose your credits!

The diagram shows different steps that needs in this experiment.

  • First you need a dataset to examine.
  • Then you have to transfer it to Hadoop cluster, and to Hadoop Distributed File System (HDFS) from there.
  • Thereafter you will run required Hive queries to extract the essence of the dataset. After processing we’ll move those data to a SQL database for ease of accessing.
  • Finally you need to develop a PHP application which runs on an App server node to visualize results.

Preparing dataset

You can download San Francisco annual income dataset from following location:

http://transparentcalifornia.com/salaries/2015/san-francisco/

After downloading, you need to open that using excel. I have observed that several job titles contains comma in text. So use find & replace to replace commas in it with hyphen character. Since this is a CSV file, those commas will negatively affect our analysis.

Put the CSV inside a zip folder to minimize data transfer.

Setting up Azure resources

Now you need to create Azure resources required. So let’s start with HD Insight. In this case we need to create a Hadoop-Linux cluster, with 2 worker nodes. Following guide will help to create such cluster quickly.

https://azure.microsoft.com/en-in/documentation/articles/hdinsight-hadoop-linux-tutorial-get-started/

Also for further information about cluster types and concepts, you may look at following link:

https://azure.microsoft.com/en-in/documentation/articles/hdinsight-hadoop-provision-linux-clusters/

Unfortunately Azure still hasn’t way to deactivate HD Insight cluster when idle. You need to manually delete it, else you’ll be charged for idle hours too.

Thereafter you need to create SQL database. Following tutorial will help on that:

https://azure.microsoft.com/en-us/documentation/articles/sql-database-get-started/#create-a-new-azure-sql-database (Create a new Azure SQL database section)

Finally you need to create an empty App Service. For further information about App Service you may refer following:

https://azure.microsoft.com/en-us/documentation/articles/app-service-web-overview/

This App Service will contain PHP runtime which will be needed at the last part of the article.

A best practice would be, when creating above resources would be to allocate all the resources to a single resource group, which makes it easy to manage.

Also make sure to give strong passwords and remember those.

Executing process

First you need to transfer zip file created to Hadoop nodes’s file system. You can do it by using scp command or any GUI tool which does scp.

As the host, you need to mention “CLUSTERNAME-ssh.azurehdinsight.net”. Along with that, you need to provide ssh credentials.


scp FILENAME.zip USERNAME@CLUSTERNAME-ssh.azurehdinsight.net:

Then you need to access that node using SSH. In windows you can use Putty tool, others may use terminal.


ssh USERNAME@CLUSTERNAME-ssh.azurehdinsight.net

Unzip the file uploaded


unzip FILENAME.zip

Next, you have to move the csv file to Hadoop File System. Use following commands to create a new directory in HDFS and move the csv file


hdfs dfs -mkdir –p /sf/salary/data

hdfs dfs -put FILENAME.csv /sf/salary/data

Now, you need to create Hive query and execute. You can execute Hive query using a file or in an interactive manner. We’ll do in both manner, but first using the file.

Create the following “salaries.hql” using nano

nano salaries.hql

and add the following queries:


DROP TABLE salaries_raw;
-- Creates an external table over the csv file
CREATE EXTERNAL TABLE salaries_raw (
 EMPLOYEE_NAME string,
 JOB_TITLE string,
 BASE_PAY float,
 OVERTIME_PAY float,
 OTHER_PAY float,
 BENEFITS float,
 TOTAL_PAY float,
 TOTAL_PAY_N_BENEFITS float,
 YEAR string,
 NOTES string,
 AGENCY string,
 STATUS string)
-- The following lines describe the format and location of the file
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
LOCATION '/sf/salary/data';

-- Drop the salaries table if it exists
DROP TABLE salaries;
-- Create the salaries table and populate it with data
-- pulled in from the CSV file (via the external table defined previously)
CREATE TABLE salaries AS
SELECT
 JOB_TITLE AS job_title,
 BASE_PAY AS base_pay,
 TOTAL_PAY AS total_pay,
 TOTAL_PAY_N_BENEFITS AS total_pay_n_benefits
FROM salaries_raw;

You can also locally create “salaries.hql” and upload via SCP.

The queries are self-explanatory, but to make it easy, each query  ends with a semicolon. Table “salaries_raw” is creating to directly extract values in CSV. So first query has one-to-one mapping with csv data. Data to the table is taken from where we stored csv file. Thereafter “salaries” table is created using “salaries_raw” table. The “salaries” table filters values of base_pay, total_pay and total_pay_n_benifits columns only. Those columns are selected, because only those would necessary for the next level.

To execute the HIVE query, use the following command


beeline -u 'jdbc:hive2://localhost:10001/;transportMode=http' -n admin -f salaries.hql

Next part of the Hive query we’ll going to do with interactive manner. You can open interactive shell with command:


beeline -u 'jdbc:hive2://localhost:10001/;transportMode=http' -n admin

and enter following commands:


INSERT OVERWRITE DIRECTORY '/sf/salary/output'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
SELECT
job_title,
avg(base_pay),
avg(total_pay),
avg(total_pay_n_benefits)
FROM salaries
WHERE base_pay IS NOT NULL AND total_pay IS NOT NULL AND total_pay_n_benefits IS NOT NULL
GROUP BY job_title;

The above Hive query will output the result to “/sf/salary/output” folder. It’ll group the job title and get the average values of base_pay, total_pay and total_pay_n_benifits columns.

Use “!quit” command to exit from interactive shell.

At this stage, we have successfully completed extracting essence of the dataset. Next, we need to make it ready for presentation.

For presentation, we’re going to copy output data to the SQL database created.

To create a table and do other interactions with SQL database, we need to install FreeTDS to Hadoop node. Use following command to install and verify the connectivity.


sudo apt-get --assume-yes install freetds-dev freetds-bin

TDSVER=8.0 tsql -H <serverName>.database.windows.net -U <adminLogin> -P <adminPassword> -p 1433 -D <databaseName>

Once you execute the last command, you’ll be directed to another interactive shell where you can interact with the database you created, when creating SQL node.

Use following commands to create a table to put the output we got


CREATE TABLE [dbo].[salaries](
[job_title] [nvarchar](50) NOT NULL,
[base_pay] float,
[total_pay] float,
[total_pay_n_benefits] float,
CONSTRAINT [PK_delays] PRIMARY KEY CLUSTERED
([job_title] ASC))
GO

Use “exit” command to exit from SQL interactive session.

To move the data from HDFS to SQL database, we are going to use Sqoop. Following Sqoop command with put output data in HDFS to SQL database.


sqoop export --connect 'jdbc:sqlserver://<serverName>.database.windows.net:1433;database=<databaseName>' --username <adminLogin> --password <adminPassword> --table ' salaries' --export-dir 'wasbs:/// sf/salary/output' --fields-terminated-by '\t' -m 1

Once the task is successfully completed, you can again log in to SQL interaction session and execute following to view table results:


SELECT * FROM salaries

GO

Finally you need to use FTP to connect with App Services node, and FTP the following PHP files (including js foolder).

https://github.com/Buddhima/AzureSFSalaryApp

You need to change SQL server host and credentials in “dbconnect.php” file. I’ll leave the rest of code in PHP file as a self-explanatory for you. If you successfully created the app, you should see something similar to following:

azure_web_app

 

Conclusion

In this article I have shown you how I did my first experiment with Azure big data analysis. Along the path I had to cover several other related technologies such as Spark, Azure Stream Analysis. So there are pros and cons with using those technologies. In such cases like analyzing annual income, it’s generally accepted to use Hadoop along with Hive. But if you want to do more frequent activities, you may look in to alternatives.

References

  1. Project sourcecode – https://github.com/Buddhima/AzureSFSalaryApp
  2. Get started with Hadoop in Windows – https://azure.microsoft.com/en-us/documentation/articles/hdinsight-hadoop-tutorial-get-started-windows
  3. Analyze Flight Delays with HD Insight – https://azure.microsoft.com/en-us/documentation/articles/hdinsight-analyze-flight-delay-data-linux
  4. Use Hive and HiveQL with Hadoop in HDInsight – https://azure.microsoft.com/en-us/documentation/articles/hdinsight-use-hive/
Advertisements

Architectural Constraints of REST

Introduction

The term REST, (Representational State Transfer) was introduced in way back in year 2000, by Roy Fielding in his doctoral dissertation at UC Irvine[1]. Though it is about 16 years ago, still it’s importance is more or less the same. REST introduces an architectural style for web applications.

REST is tightly coupled, and embraces the potential of HTTP. Because of that it has become more popular in web application domain. REST see a web application as a virtual state-machine where states are different web pages; progressing user from one state to another through links; and going to another state.

rest-state-machine
REST web application with state transitions

To call be a web application RESTful, it needs to comply certain constraints. Following those constraints also helps web application to have desirable non-functional properties as well.

Architectural Constraints

Decoupled client-server interaction

There should be a uniform interface to separate client and server, which helps to achieve separation of concerns. Client has no need to store data, such that client is portable. On the other hand, server does not worry about keeping user state, so server-side can be scaled. In RESTful architecture server and clients should be able to use different technologies to develop separately.

Stateless

Going further with client-server architecture, the server should not keep client context between requests. Each request from client should include all the information necessary to serve that request. Session state is held at client side. Once session data is received, the service can transfer that to another service.

Each state of application contains the links for client to choose the next state transition.

Layered

The client may connect to intermediate layers, which might not aware before-hand. So those layers can enforce security, do load balancing, provide caching etc.

Cacheable

Response from server can be cached at client side and at intermediaries, as defined by HTTP response. Cache-Control Header plays a key role in that behavior [2].

Extensible through code on demand

This is an optional requirement. Server can send executable code to client side to customize the functionality of the client. Client-side scripts such as JavaScript can be taken as an example.

Uniform Interface

This a basic requirement of a RESTful service and helps to make decoupled client-server interaction.

  • Identification of resources

Server can use requests to identify resources separately (eg: URI), and returns the representation of that resource to client (as HTML, XML or JSON).

  • Manipulate the resource through its representation

The representation send by the server is enough for a client to manipulate (update, delete) that resource.

  • Self-descriptive messages

Information included in a message contains adequate information on how to manipulate that message.

  • Hypermedia as the engine of application state (HATEOAS)

“Hypermedia, an extension of the term hypertext, is a nonlinear medium of information which includes graphics, audio, video, plain text and hyperlinks” [3]

So clients can only make transitions, through actions that are dynamically identified within hypermedia by the server. There are no standard format to represent links in a hypermedia, but there are few popular ones [4].

Architectural Properties

The constraints helps you to achieve following properties;

  • Performance
  • Scalability
  • Simplicity of interfaces
  • Modifiability
  • Visibility
  • Portability
  • Reliability

Web service APIs that comply with REST architectural constraints are called RESTful APIs [5].

References

[1] http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

[2] http://www.tutorialspoint.com/restful/restful_caching.htm

[3] https://en.wikipedia.org/wiki/Hypermedia

[4] http://sookocheff.com/post/api/on-choosing-a-hypermedia-format/

[5] http://restfulapi.net/

CAP Theorem

This theorem is presented as a conjecture by Prof. Eric Brewer at PODC (Principles of Distributed Computing) 2000 keynote talk. The theorem states that a distributed computer system to obtain all following 3 conditions is difficult.

  1. Consistency
  • All nodes should see the same data at the same time
  1. Availability
  • Every request receives response whether it is succeed or failed
  1. Partition Tolerance
  • System continue to work despite partitioning of the system

cap_venn

In 2002, Seth Gilbert and Nancy Lynch were able to prove the theorem.

Proving the CAP theorem

To prove the theorem, let’s assume that theorem is false. Which means there’s a system such that Consistency, Availability and Partition-Tolerance are satisfied.

CAP Theorem - Writing

So let’s say that system has 2 nodes which are N1 and N2.

Also there’s a value v0 in both N1 and N2 nodes, which is interested by the client.

Since system can tolerate partitioning, a user can get the same service from N1 and N2, and according to our assumption the link between N1 and N2 can be broken.

When the link is broken, client updates v0 value in N1 to v1.

Now client expect to get new ‘v’ value from N2, but still returns v0, because the link between N1 & N2 is broken.

Since client still getting v0 from N2, which implies system’s consistency has broken.

Therefore our assumption is wrong and CAP theorem is proven!

So now there can be systems which satisfies either 2 of those 3.

But what about a system which satisfies just Consistency and Availability not Partition-Tolerance?

Let’s get back to our previous system.

So the system supposed to be Consistent and Available.

Again, the link between N1 & N2 breaks.

Client sends v2 to update v values in N1 node.

What should the system do?

  1. Accept the request and update ‘v’ value in N1 – leads to inconsistent system
  2. Reject the client’s requests – leads to unavailable system

So that says there’s a rare chance of having a system which satisfies Consistency and Availability.

What is available out there is systems with,

AP – relax consistency, but not inconsistent

CP – sacrificed availability, but not unavailable.

So real world systems with AP and CP can offer a degree of consistency, availability and partition-tolerance.

 

PACELC

This was developed to give more complete description of the space of potential tradeoffs for a distributed system

If there’s Partition (P)

  • System trade-off Availability (A) and Consistency (C)

Else (E)

  • System trade-off Latency (L) and Consistency (C)

 

Availability and Latency are arguably the same thing:

Unavailable → extreme high latency

PACELC Theorem - Writing
Example Systems:

PA/EL Systems – Dynamo, Cassendra, Riak

PC/EC Systems – BigTable, HBase, VoltDB/H-Store

PA/EC Systems – MongoDB

PC/EL Systems – Yahoo! PNUTS

 

References

[1] https://en.wikipedia.org/wiki/CAP_theorem

[2] http://mwhittaker.github.io/2014/08/16/illustrated-proof-cap-theorem/

[3] https://www.youtube.com/watch?v=hUd_9FENShA