BTech-VII Sem Big Data (702D) Unit-III Notes
BTech-VII Sem Big Data (702D) Unit-III Notes
Apache: Apache HTTP Server is an open source cross-platform web server. It is fast and
secure enough to be used to run major websites. As a Web server, Apache is responsible
for accepting directory (HTTP) requests from Internet users and sending them their desired
information in the form of files and Web pages. Apache is a web server software that is
responsible for accepting HTTP requests from visitors and sending them back the requested
information in the form of web pages. Or in simpler terms, it allows visitors to view content on
your website.
Sqoop: It is used to import and export data to and from between HDFS
and RDBMS.
Pig: It is a procedural language platform used to develop a script for
MapReduce operations.
Hive: It is a platform used to develop SQL type scripts to do MapReduce
operations.
What is Hive
The Hive Query Language (HiveQL) is a query language for Hive to
process and analyze structured data in a Metastore
Hive is not
A relational database
A design for OnLine Transaction Processing (OLTP)
A language for real-time queries and row-level updates
Note: Hive works on the server-side while Pig works on the client-side of your clusters. Hive
can access raw data while Pig Latin scripts cannot.
Features of Hive
It stores schema in a database and processed data into HDFS.
It is designed for OLAP.
It provides SQL type language for querying called HiveQL or HQL.
It is familiar, fast, scalable, and extensible.
Architecture of Hive
The following component diagram depicts the architecture of Hive:
Hive is a data warehouse infrastructure software that can create interaction between
User Interface user and HDFS. The user interfaces that Hive supports are Hive Web UI, Hive command
line, and Hive HD Insight (In Windows server).
Hive chooses respective database servers to store the schema or Metadata of tables,
Meta Store
databases, columns in a table, their data types, and HDFS mapping.
HiveQL is similar to SQL for querying on schema info on the Metastore. It is one of the
HiveQL Process Engine replacements of traditional approach for MapReduce program. Instead of writing
MapReduce program in Java, we can write a query for MapReduce job and process it.
The conjunction part of HiveQL process Engine and MapReduce is Hive Execution
Execution Engine Engine. Execution engine processes the query and generates results as same as
MapReduce results. It uses the flavor of MapReduce.
Hadoop distributed file system or HBASE are the data storage techniques to store data
HDFS or HBASE
into file system.
Working of Hive
The following diagram depicts the workflow between Hive and Hadoop.
The following table defines how Hive interacts with Hadoop framework:
Execute Query
1 The Hive interface such as Command Line or Web UI sends query to Driver (any database
driver such as JDBC, ODBC, etc.) to execute.
Get Plan
2 The driver takes the help of query compiler that parses the query to check the syntax and
query plan or the requirement of query.
Get Metadata
3
The compiler sends metadata request to Metastore (any database).
Send Metadata
4
Metastore sends metadata as a response to the compiler.
Send Plan
5 The compiler checks the requirement and resends the plan to the driver. Up to here, the
parsing and compiling of a query is complete.
Execute Plan
6
The driver sends the execute plan to the execution engine.
Execute Job
Internally, the process of execution job is a MapReduce job. The execution engine sends
7
the job to JobTracker, which is in Name node and it assigns this job to TaskTracker, which
is in Data node. Here, the query executes MapReduce job.
Metadata Ops
7.1 Meanwhile in execution, the execution engine can execute metadata operations with
Metastore.
Fetch Result
8
The execution engine receives the results from Data nodes.
Send Results
9
The execution engine sends those resultant values to the driver.
Send Results
10
The driver sends the results to Hive Interfaces.
Integer Types
Decimal Type
Date/Time Types
TIMESTAMP
DATES
The Date value is used to specify a particular year, month and day, in the form YYYY--
MM--DD. However, it didn't provide the time of the day. The range of Date type lies
between 0000--01--01 to 9999--12--31.
String Types
STRING
The string is a sequence of characters. It values can be enclosed within single quotes
(') or double quotes (").
Varchar
The varchar is a variable length type whose range lies between 1 and 65535, which
specifies that the maximum number of characters allowed in the character string.
CHAR
Complex Type
To interact with tables, databases, and queries, Hive provides a SQL like environment
through hadoop hiveql. To execute various types of data processing and querying, we can
have different types of Clauses for improved communication with various nodes outside the
ecosystem. HIVE also has JDBC connectivity.
If a database with the name "mydatabase" doesn't already exist, this statement creates one.
The database is only created if it doesn't already exist, thanks to the IF NOT EXISTS
condition.
USE mydatabase;
By switching to the "mydatabase" database using this line, further activities can be carried
out in that database.
);
The "employees" table is created with this statement. It has three columns: "id" (integer),
"name" (string), and "age" (integer). The table is only generated if it doesn't already exist
thanks to the IF NOT EXISTS condition.
This hadoop hiveql command creates a new external table called "ext_employees." External
tables point to data that is kept in a location independent of Hive, preserving the original
location of the data. The HDFS path where the data is located is specified by the LOCATION
clause.
The LOAD DATA statement inserts data into the designated table from an HDFS path. The
"employees" table receives a specific row of data when the INSERT INTO TABLE query is
executed.
This hadoop hiveql command retrieves all records from the "employees" table.
Only those records from the "employees" table that have a "age" greater than 25 are chosen
by this.
These hadoop hiveql queries count the number of employees and determine the average age
using aggregation operations on the "employees" table.
In order to extract employee names and their related departments, this query connects the
"employees" and "departments" databases based on the "department_id" field.
FROM employees e
The "department_id" column is used to link the "employees" and "departments" databases in
order to access employee names and their related departments.
This query counts the number of employees in each department and organises employees by
department.
1. Data Filtering: You can use the WHERE clause to filter rows based on specific
conditions.
The "marks" table's field must be greater than 60 in order for this query to return all items
with that value.
2. Sorting Data: You can use the ORDER BY clause to order the result set according to one
or more columns.
1. Data Transformations: HiveQL provides a number of built-in functions for changing the
data in your query.
This hadoop hiveql query pulls the "name" column from the "employees" table and uses the
UPPER function to change the names to uppercase.
[Link]: Using functions like COUNT, SUM, AVG, and others, aggregates let you
condense data.
Using the AVG function, this query determines the average age of every employee in the
"employees" table.
3. Grouping and Aggregating: To group data into categories, the GROUP BY clause is used
with aggregate functions.
The COUNT function is used in this query to count the number of employees in each
department and group the employees by the "department" column.
Example: Calculate the typical age of your staff members that are over 35.
This hadoop hiveql query determines the average age of the filtered subset of employees by
first excluding those over the age of 35.
1. Joins: With the use of joins, you can merge rows from various tables based on a shared
column. The INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN are
examples of common join types.
Example: As an illustration, retrieve the employees and the corresponding departments from
an inner join.
Based on the "department_id" column, this query combines information from the
"employees" and "departments" tables to retrieve employee names and their related
departments.
2. Subqueries: A subquery is a query that is nested inside another query. The SELECT,
WHERE, and FROM clauses can all use them.
Example: Determine the typical age of employees in each department using a subquery in
the SELECT statement.
SELECT department, (
SELECT AVG(age)
FROM employees e
The average age of employees for each department in the "departments" dataset is determined
by this query using a subquery.
3. Correlated Subqueries: An inner query that depends on results from the outer query is
referred to as a correlated subquery.
Example: Find employees whose ages are higher than the department's average, for instance.
To locate employees whose ages are higher than the mean ages of employees in the same
department, this query uses a correlated subquery.
Conclusion
HiveQL (Hive Query Language) is used to communicate with Apache Hive, a
Hadoop data warehouse and SQL-like query language.
Hive provides a SQL-like environment through hadoop hiveql for interacting with
tables, databases, and queries.
Hive offers features like creating databases, tables.
HiveQL allows data retrieval, filtering, transformation, and analysis. Common
querying operations include SELECT, WHERE, ORDER BY, and JOIN.
HiveQL provides built-in functions for data transformations and aggregation.
Aggregations use functions like COUNT, SUM, AVG, etc., to condense data.
1. Structured Query Language (SQL): SQL is a domain-specific language used
in programming and designed for managing data held in a relational database
management system also known as RDBMS. It is also useful in handling structured
data, i.e., data incorporating relations among entities and variables. SQL is a
standard language for storing, manipulating, and retrieving data in databases.
2. Hive Query Language (HiveQL): HiveQL is a query language for Hive to
analyze and process structured data in a Meta-store. It is a mixture of SQL-92,
MySQL, and Oracle’s SQL. It is very much similar to SQL and highly scalable. It
reuses familiar concepts from the relational database world, such as tables, rows,
columns and schema, to ease learning. Hive supports four file formats those are
TEXT FILE, SEQUENCE FILE, ORC and RC FILE (Record Columnar File).
Differences between SQL and HiveQL is as follows:
On the basis
of SQL HiveQL
Update-
UPDATE, DELETE UPDATE, DELETE
commands in
INSERT, INSERT,
table structure
Hundreds of built-in
Functions Hundreds of built-in functions
functions
Multitable
Not supported Supported
inserts in table
On the basis
of SQL HiveQL
Create
table…as Not supported Supported
Select
Introduction to Pig:
To analyze data using Apache Pig, programmers need to write scripts using
Pig Latin language. All these scripts are internally converted to Map and
Reduce tasks. Apache Pig has a component known as Pig Engine that
accepts the Pig Latin scripts as input and converts those scripts into
MapReduce jobs
Programmers can use Pig to write data transformations without knowing Java. Pig
uses both structured and unstructured data as input to perform analytics and
uses HDFS to store the results.
Features of Pig
Apache Pig comes with the following features −
Rich set of operators − It provides many operators to perform
operations like join, sort, filer, etc.
Ease of programming − Pig Latin is similar to SQL and it is easy to write
a Pig script if you are good at SQL.
Optimization opportunities − The tasks in Apache Pig optimize their
execution automatically, so the programmers need to focus only on
semantics of the language.
Extensibility − Using the existing operators, users can develop their
own functions to read, process, and write data.
UDF’s − Pig provides the facility to create User-defined Functions in
other programming languages such as Java and invoke or embed
them in Pig Scripts.
Handles all kinds of data − Apache Pig analyzes all kinds of data, both
structured as well as unstructured. It stores the results in HDFS.
Pig SQL
The data model in Apache Pig is nested The data model used in SQL is
relational. flat relational.
, Anatomy of Pig:
The language used to analyze data in Hadoop using Pig is known as Pig Latin. It is a highlevel data
processing language which provides a rich set of data types and operators to perform various
operations on the data.
To perform a particular task Programmers using Pig, programmers need to write a Pig script
using the Pig Latin language, and execute them using any of the execution mechanisms
(Grunt Shell, UDFs, Embedded). After execution, these scripts will go through a series of
transformations applied by the Pig Framework, to produce the desired output.
Internally, Apache Pig converts these scripts into a series of MapReduce jobs, and thus, it
makes the programmer’s job easy. The architecture of Apache Pig is shown below.
The compiler compiles the optimized logical plan into a series of MapReduce jobs.
Execution engine
Finally the MapReduce jobs are submitted to Hadoop in a sorted order. Finally, these
MapReduce jobs are executed on Hadoop producing the desired results.
Pig Latin Data Model
The data model of Pig Latin is fully nested and it allows complex non-atomic datatypes such
as map and tuple. Given below is the diagrammatical representation of Pig Latin’s data
model.
Atom
Any single value in Pig Latin, irrespective of their data, type is known as an Atom. It is stored
as string and can be used as string and number. int, long, float, double, chararray, and
bytearray are the atomic values of Pig. A piece of data or a simple atomic value is known as
a field.
Example − ‘raja’ or ‘30’
Tuple
A record that is formed by an ordered set of fields is known as a tuple, the fields can be of
any type. A tuple is similar to a row in a table of RDBMS.
Example − (Raja, 30)
Bag
A bag is an unordered set of tuples. In other words, a collection of tuples (non-unique) is
known as a bag. Each tuple can have any number of fields (flexible schema). A bag is
represented by ‘{}’. It is similar to a table in RDBMS, but unlike a table in RDBMS, it is not
necessary that every tuple contain the same number of fields or that the fields in the same
position (column) have the same type.
ETL Processing:
INTRODUCTION:
1. ETL stands for Extract, Transform, Load and it is a process used in data warehousing to
extract data from various sources, transform it into a format suitable for loading into a
data warehouse, and then load it into the warehouse. The process of ETL can be broken
down into the following three stages:
2. Extract: The first stage in the ETL process is to extract data from various sources such as
transactional systems, spreadsheets, and flat files. This step involves reading data from
the source systems and storing it in a staging area.
3. Transform: In this stage, the extracted data is transformed into a format that is suitable
for loading into the data warehouse. This may involve cleaning and validating the data,
converting data types, combining data from multiple sources, and creating new data
fields.
4. Load: After the data is transformed, it is loaded into the data warehouse. This step
involves creating the physical data structures and loading the data into the warehouse.
5. The ETL process is an iterative process that is repeated as new data is added to the
warehouse. The process is important because it ensures that the data in the data
warehouse is accurate, complete, and up-to-date. It also helps to ensure that the data is
in the format required for data mining and reporting.
Additionally, there are many different ETL tools and technologies available, such as
Informatica, Talend, DataStage, and others, that can automate and simplify the ETL process.
ETL is a process in Data Warehousing and it stands for Extract, Transform and Load. It is a
process in which an ETL tool extracts the data from various data source systems, transforms
it in the staging area, and then finally, loads it into the Data Warehouse system.
Let us understand each step of the ETL process in-depth:
1. Extraction:
The first step of the ETL process is extraction. In this step, data from various source
systems is extracted which can be in various formats like relational databases, No SQL,
XML, and flat files into the staging area. It is important to extract the data from various
source systems and store it into the staging area first and not directly into the data
warehouse because the extracted data is in various formats and can be corrupted also.
Hence loading it directly into the data warehouse may damage it and rollback will be much
more difficult. Therefore, this is one of the most important steps of ETL process.
2. Transformation:
The second step of the ETL process is transformation. In this step, a set of rules or
functions are applied on the extracted data to convert it into a single standard format. It
may involve following processes/tasks:
Filtering – loading only certain attributes into the data warehouse.
Cleaning – filling up the NULL values with some default values, mapping U.S.A, United
States, and America into USA, etc.
Joining – joining multiple attributes into one.
Splitting – splitting a single attribute into multiple attributes.
Sorting – sorting tuples on the basis of some attribute (generally key-attribute).
3. Loading:
The third and final step of the ETL process is loading. In this step, the transformed data is
finally loaded into the data warehouse. Sometimes the data is updated by loading into the
data warehouse very frequently and sometimes it is done after longer but regular
intervals. The rate and period of loading solely depends on the requirements and varies
from system to system.
ETL process can also use the pipelining concept i.e. as soon as some data is extracted, it can
transformed and during that period some new data can be extracted. And while the
transformed data is being loaded into the data warehouse, the already extracted data can be
transformed. The block diagram of the pipelining of ETL process is shown below:
ETL Tools: Most commonly used ETL tools are Hevo, Sybase, Oracle Warehouse builder,
CloverETL, and MarkLogic.
Data Warehouses: Most commonly used Data Warehouses are Snowflake, Redshift,
BigQuery, and Firebolt.
ADVANTAGES OR DISADVANTAGES:
1. Improved data quality: ETL process ensures that the data in the data warehouse is
accurate, complete, and up-to-date.
2. Better data integration: ETL process helps to integrate data from multiple sources and
systems, making it more accessible and usable.
3. Increased data security: ETL process can help to improve data security by controlling
access to the data warehouse and ensuring that only authorized users can access the data.
4. Improved scalability: ETL process can help to improve scalability by providing a way to
manage and analyze large amounts of data.
5. Increased automation: ETL tools and technologies can automate and simplify the ETL
process, reducing the time and effort required to load and update data in the warehouse.
1. High cost: ETL process can be expensive to implement and maintain, especially for
organizations with limited resources.
2. Complexity: ETL process can be complex and difficult to implement, especially for
organizations that lack the necessary expertise or resources.
3. Limited flexibility: ETL process can be limited in terms of flexibility, as it may not be able
to handle unstructured data or real-time data streams.
4. Limited scalability: ETL process can be limited in terms of scalability, as it may not be able
to handle very large amounts of data.
5. Data privacy concerns: ETL process can raise concerns about data privacy, as large
amounts of data are collected, stored, and analyzed.
Overall, ETL process is an essential process in data warehousing that helps to ensure that the
data in the data warehouse is accurate, complete, and up-to-date. However, it also comes
with its own set of challenges and limitations, and organizations need to carefully consider
the costs and benefits before implementing them.
Primitive
Complex
Complex data types consist of a bit of logical and complicated data type. The following are
the complex data type −
Key − An element of finding an element, the key must be unique and must be
charrarray.
Value − Any data can be stored in a value, and each key has particular data related to
it. The map is built using a bracket and hash between key and values. Cas to separate
pairs of over one key value. Here # is used to distinguish key and value.
Null Values − Valuable value is missing or unknown, and any data may apply. The pig
handles an empty value similar to SQL. Pig detects blank values when data is missing,
or an error occurs during data processing. Also, null can be used as a value proposition
of your choice.
Note − Pig allows for the reproduction of complex data structures. For example, you can easily
place a tuple on the interior side of a tuple, bag, and map.
Complex Types
Null Values
Values for all the above data types can be NULL. Apache Pig treats null
values in a similar way as SQL does.
CASE f2 % 2
CASE
WHEN 0 THEN
WHEN
Case − The case operator is equivalent to nested bincond 'even'
THEN
operator. WHEN 1 THEN
ELSE
'odd'
END
END
Equal − Checks if the values of two operands are equal or not; (a = b) is not
==
if yes, then the condition becomes true. true
Less than − Checks if the value of the left operand is less than
< the value of the right operand. If yes, then the condition (a < b) is true.
becomes true.
Operator Description
Filtering
Sorting
Diagnostic Operators
Running Pig:
Local Mode
o It executes in a single JVM and is used for development experimenting and
prototyping.
o Here, files are installed and run using localhost.
o The local mode works on a local file system. The input and output data stored
in the local file system.
1. $ pig-x local
MapReduce Mode
o The MapReduce mode is also known as Hadoop Mode.
o It is the default mode.
o In this Pig renders Pig Latin into MapReduce jobs and executes them on the
cluster.
o It can be executed against semi-distributed or fully distributed Hadoop
installation.
o Here, the input and output data are present on HDFS.
1. $ pig
Or,
1. $ pig -x mapreduce
o Interactive Mode - In this mode, the Pig is executed in the Grunt shell. To
invoke Grunt shell, run the pig command. Once the Grunt mode executes, we
can provide Pig Latin statements and command interactively at the command
line.
o Batch Mode - In this mode, we can run a script file having a .pig extension.
These files contain Pig Latin commands.
o Embedded Mode - In this mode, we can define our own functions. These
functions can be called as UDF (User Defined Functions). Here, we use
programming languages like Java and Python.
Note: Pig Engine has two type of execution environment i.e. a local execution environment in
a single JVM (used when dataset is small in size)and distributed execution environment in a
Hadoop Cluster.
In this mode, a user can interactively run Apache Pig using the Grunt shell. Users
can submit commands and get a result there only.
Let us see the below example. We are running the below statements in interactive
mode and getting output there only.
Command:
grunt> employee = LOAD 'hdfs://localhost:9000/pigdata/[Link]' USING PigSto
rage(',')
as (empid:int,empname:chararray,company:chararray);
Let us see the below example. We have created a script file with the name
“emp_script.pig” and placed it at the HDFS location, now we are calling that file
using the batch mode command.
Command:
$pig -x mapreduce hdfs:///pigdata/emp_script.pig
Pig Commands
Given below in the table are some frequently used Pig Commands.
Command Function
Type Examples
EVAL functions AVG, COUNT, COUNT_STAR, SUM, TOKENIZE, MAX,
MIN, SIZE etc
LOAD or STORE Pigstorage(), Textloader, HbaseStorage, JsonLoader, JsonStorage
functions etc
Math functions ABS, COS, SIN, TAN, CEIL, FLOOR, ROUND, RANDOM etc
String functions TRIM, RTRIM, SUBSTRING, LOWER, UPPER etc
DateTime function GetDay, GetHour, GetYear, ToUnixTime, ToString etc
Eval functions:
AVG(col): computes the average of the numerical values in a single column of a bag
CONCAT(string expression1, string expression2) : Concatenates two expressions of
identical type
COUNT(DataBag bag): Computes the number of elements in a bag excluding null
values
COUNT STAR (DataBag bag1, DataBag bag 2): Computes the number of elements in a
bag including null values.
DIFF(DataBag bag1, DataBag bag2): It is used to compare two bags, if any element in
one bag is not present in the other bag are returned in a bag
IsEmpty(DataBag bag), IsEmpty(Map map): It is used to check if the bag or map is
empty
Max(col): Computes the maximum of the numeric values or character in a single column
bag
MIN(col): Computes the minimum of the numeric values or character in a single column
bag
DEFINE pluck pluckTuple(expression1): It allows the user to specify a string prefix, and
filters the columns which begins with that prefix
SIZE(expression): Computes the number of elements based on any pig data
SUBSTRACT(DataBag bag1, DataBag bag2): It returns the bag which does not contain
bag1 element in bag2
SUM: Computes the sum of the values in a single-column bag
TOKENIZE(String expression[,‘field delimiter’): It splits the string and outputs a bag of
words
PigStorage ():
Syntax: PigStorage(field_delimiter)
A = LOAD ‘Employee’ USING PigStorage(‘\t’) AS (name: chararray, age:int, gpa:
float);
Loads and stores data as structured text file
TextLoader():
BinStorage():
Handling compression:
It loads and stores compressed data in Pig
JsonLoader, JsonStorage:
Pig dump:
Math functions:
ABS:
Syntax: ABS(expression)
It returns the absolute value of an expression
COS:
Syntax: COS(expression)
It Returns the trigonometric cosine of an expression.
SIN:
CEIL:
Syntax: CEIL(expression)
It is used to return the value of an expression rounded up to the nearest integer
TAN:
Syntax: TAN(expression)
It is used to return the trigonometric tangent of an angle.
ROUND:
Syntax: ROUND(expression)
It returns the value of an expression rounded to an integer (if the result type is float)
or long (if the result type is double)
RANDOM:
Synatx: RANDOM ()
It returns a pseudo random number (type double) greater than or equal to 0.0 and
less than 1.0
Floor:
Syntax: FLOOR(expression)
Returns the value of an expression rounded down to the nearest integer.
CBRT:
Synatx: CBRT(expression)
It returns the cube root of an expression
EXP:
Syntax: EXP(expression)
Returns Euler’s number e raised to the power of x.
String Functions:
INDEXOF:
TRIM:
Syntax: TRIM(expression)
It returns a copy of the string with leading and trailing whitespaces removed
SUBSTRING:
UCFIRST:
Syntax: UCFIRST(expression)
It will return a string with the first character changed to the upper case
LOWER:
Syntax: LOWER(expression)
Converts all characters in a string to lowercase
UPPER:
Synatx: UPPER(expression)
Converts all characters in a string to the uppercase
*********End**********