Transcription

Apache Hivei

Apache HiveAbout the TutorialHive is a data warehouse infrastructure tool to process structured data in Hadoop.It resides on top of Hadoop to summarize Big Data, and makes querying andanalyzing easy.This is a brief tutorial that provides an introduction on how to use Apache HiveHiveQL with Hadoop Distributed File System. This tutorial can be your first steptowards becoming a successful Hadoop Developer with Hive.AudienceThis tutorial is prepared for professionals aspiring to make a career in Big DataAnalytics using Hadoop Framework. ETL developers and professionals who are intoanalytics in general may as well use this tutorial to good effect.PrerequisitesBefore proceeding with this tutorial, you need a basic knowledge of Core Java,Database concepts of SQL, Hadoop File system, and any of Linux operating systemflavors.Disclaimer & Copyright Copyright 2014 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of TutorialsPoint (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy,distribute or republish any contents or a part of contents of this e-book in anymanner without written consent of the publisher. We strive to update the contentsof our website and tutorials as timely and as precisely as possible, however, thecontents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. providesno guarantee regarding the accuracy, timeliness or completeness of our websiteor its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at [email protected]

Apache HiveTable of ContentsAbout the Tutorial ········ iAudience ······················ iPrerequisites ················ iDisclaimer & Copyright · iTable of Contents ········ ii1. INTRODUCTION ················ 1Hadoop ······················· 1What is Hive? ·············· 2Features of Hive ·········· 2Architecture of Hive ···· 2Working of Hive ·········· 42. HIVE INSTALLATION ·········· 6Step 1: Verifying JAVA Installation ······· 6Step 2: Verifying Hadoop Installation ··· 8Step 3: Downloading Hive ·················· 15Step 4: Installing Hive ························· 15Step 5: Configuring Hive ····················· 16Step 6: Downloading and Installing Apache Derby ······ 17Step 7: Configuring Metastore of Hive ························· 19Step 8: Verifying Hive Installation ······ 203. HIVE DATA ·············· 22Column Types ············ 22Literals ······················ 24Null Value ·················· 24Complex Types ·········· 24ii

Apache Hive4. CREATE DATABASE ········· 25Create Database Statement ··············· 255. DROP DATABASE ············ 28Drop Database Statement ·················· 286. CREATE TABLE ················ 30Create Table · 30Load Data ····· 337. ALTER ····················· 36Alter Table Statement ························ 36Rename To Statement ····················· 36Change Statement ····· 38Add Columns 40Replace Statement ···· 418. DROP TABLE ··················· 44Drop Table Statement ························ 449. PARTITIONING ················ 47Adding a Partition ····· 48Renaming a Partition · 48Dropping a Partition ·· 4810. BUILT-IN OPERATORS ····· 50Relational Operators · 50Arithmetic Operators ························· 52Logical Operators ······ 53Complex Operators ··· 5411. BUILT-IN FUNCTIONS ······ 55iii

Apache HiveBuilt-In Functions ······ 55Aggregate Functions ·· 5712. VIEWS AND INDEXES ······ 59Creating a View ········· 59Example ···················· 59Dropping a View ········ 60Creating an Index ······ 60Example ···················· 61Dropping an Index ····· 6113. HIVEQL SELECT WHERE · 62Syntax ······················· 62Example ···················· 6214. HIVEQL SELECT ORDER ······················· 66Syntax ······················· 66Example ···················· 6615. HIVEQL GROUP BY ·········· 70Syntax ······················· 70Example ···················· 7016. HIVEQL JOINS ················· 74Syntax ······················· 74Example ···················· 74JOIN · 75LEFT OUTER JOIN ······· 76RIGHT OUTER JOIN ···· 76FULL OUTER JOIN ······ 77iv

1. INTRODUCTIONApache HiveThe term ‘Big Data’ is used for collections of large datasets that include huge volume, highvelocity, and a variety of data that is increasing day by day. Using traditional datamanagement systems, it is difficult to process Big Data. Therefore, the Apache SoftwareFoundation introduced a framework called Hadoop to solve Big Data management andprocessing challenges.HadoopHadoop is an open-source framework to store and process Big Data in a distributedenvironment. It contains two modules, one is MapReduce and another is Hadoop DistributedFile System (HDFS). MapReduce: It is a parallel programming model for processing large amounts ofstructured, semi-structured, and unstructured data on large clusters of commodityhardware.1. HDFS: Hadoop Distributed File System is a part of Hadoop framework, used to storeand process the datasets. It provides a fault-tolerant file system to run on commodityhardware.The Hadoop ecosystem contains different sub-projects (tools) such as Sqoop, Pig, and Hivethat are used to help Hadoop modules. Sqoop: It is used to import and export data to and fro between HDFS and RDBMS. Pig: It is a procedural language platform used to develop a script for MapReduceoperations. Hive: It is a platform used to develop SQL type scripts to do MapReduce operations.Note: There are various ways to execute MapReduce operations: The traditional approach using Java MapReduce program for structured, semistructured, and unstructured data. The scripting approach for MapReduce to process structured and semi structured datausing Pig. The Hive Query Language (HiveQL or HQL) for MapReduce to process structured datausing Hive.What is Hive?Hive is a data warehouse infrastructure tool to process structured data in Hadoop. It resideson top of Hadoop to summarize Big Data, and makes querying and analyzing easy.5

Apache HiveInitially Hive was developed by Facebook, later the Apache Software Foundation took it upand developed it further as an open source under the name Apache Hive. It is used bydifferent companies. For example, Amazon uses it in Amazon Elastic MapReduce.Hive is not A relational database A design for OnLine Transaction Processing (OLTP) A language for real-time queries and row-level updatesFeatures of HiveHere are the 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 HiveThe following component diagram depicts the architecture of Hive:This component diagram contains different units. The following table describes each unit:Unit NameOperation6

Apache HiveUser InterfaceHive is a data warehouse infrastructure software that can createinteraction between user and HDFS. The user interfaces that Hivesupports are Hive Web UI, Hive command line, and Hive HD Insight(In Windows server).Meta StoreHive chooses respective database servers to store the schema orMetadata of tables, databases, columns in a table, their datatypes, and HDFS mapping.HiveQL ProcessEngineHiveQL is similar to SQL for querying on schema info on theMetastore. It is one of the replacements of traditional approach forMapReduce program. Instead of writing MapReduce program inJava, we can write a query for MapReduce job and process it.Execution EngineThe conjunction part of HiveQL process Engine and MapReduce isHive Execution Engine. Execution engine processes the query andgenerates results as same as MapReduce results. It uses the flavorof MapReduce.HDFS or HBASEHadoop distributed file system or HBASE are the data storagetechniques to store data into file system.Working of HiveThe following diagram depicts the workflow between Hive and Hadoop.7

Apache HiveThe following table defines how Hive interacts with Hadoop framework:Step No.1OperationExecute QueryThe Hive interface such as Command Line or Web UI sends query to Driver(any database driver such as JDBC, ODBC, etc.) to execute.2Get PlanThe driver takes the help of query compiler that parses the query to checkthe syntax and query plan or the requirement of query.3Get MetadataThe compiler sends metadata request to Metastore (any database).4Send MetadataMetastore sends metadata as a response to the compiler.5Send Plan8

Apache HiveThe compiler checks the requirement and resends the plan to the driver.Up to here, the parsing and compiling of a query is complete.6Execute PlanThe driver sends the execute plan to the execution engine.7Execute JobInternally, the process of execution job is a MapReduce job. The executionengine sends the job to JobTracker, which is in Name node and it assignsthis job to TaskTracker, which is in Data node. Here, the query executesMapReduce job.7.1Metadata OpsMeanwhile in execution, the execution engine can execute metadataoperations with Metastore.8Fetch ResultThe execution engine receives the results from Data nodes.9Send ResultsThe execution engine sends those resultant values to the driver.10Send ResultsThe driver sends the results to Hive Interfaces.9

2. HIVE INSTALLATIONApache HiveAll Hadoop sub-projects such as Hive, Pig, and HBase support Linux operating system.Therefore, you need to install any Linux flavored OS. The following simple steps are executedfor Hive installation:Step 1: Verifying JAVA InstallationJava must be installed on your system before installing Hive. Let us verify java installationusing the following command: java –versionIf Java is already installed on your system, you get to see the following response:java version "1.7.0 71"Java(TM) SE Runtime Environment (build 1.7.0 71-b13)Java HotSpot(TM) Client VM (build 25.0-b02, mixed mode)If java is not installed in your system, then follow the steps given below for installing java.Installing JavaStep I:Download java (JDK latest version - X64.tar.gz) by visiting the following downloads/jdk7-downloads-1880260.html.Then jdk-7u71-linux-x64.tar.gz will be downloaded onto your system.Step II:Generally, you will find the downloaded java file in the Downloads folder. Verify it and extractthe jdk-7u71-linux-x64.gz file using the following commands. cd Downloads/ lsjdk-7u71-linux-x64.gz10

Apache Hive tar zxf jdk-7u71-linux-x64.gz lsjdk1.7.0 71jdk-7u71-linux-x64.gzStep III:To make java available to all the users, you have to move it to the location “/usr/local/”. Openroot, and type the following commands. supassword:# mv jdk1.7.0 71 /usr/local/# exitStep IV:For setting up PATH and JAVA HOME variables, add the following commands to /.bashrcfile.export JAVA HOME /usr/local/jdk1.7.0 71export PATH PATH: JAVA HOME/binNow apply all the changes into the current running system. source /.bashrcStep V:Use the following commands to configure java alternatives:# alternatives --install /usr/bin/java java usr/local/java/bin/java 2# alternatives --install /usr/bin/javac javac usr/local/java/bin/javac 2# alternatives --install /usr/bin/jar jar usr/local/java/bin/jar 2# alternatives --set java usr/local/java/bin/java11

Apache Hive# alternatives --set javac usr/local/java/bin/javac# alternatives --set jar usr/local/java/bin/jar12

Apache HiveEnd of ebook previewIf you liked what you saw Buy it from our store @ https://store.tutorialspoint.com13

Apache Hive i About the Tutorial Hive is a data warehouse infrastructure tool to process structured data in Hadoop. It resides on top of Hadoop to summarize Big Data, and makes querying and analyzing easy. This is a brief tutorial that provides an introduction on how to use Apache Hive HiveQL with Hadoop Distributed File System.File Size: 780KBPage Count: 14Explore furtherHive Cheat Sheet - Download in PDF & JPG Format - Intellipaatintellipaat.comHive Tutorial for Beginners: Learn with Examples in 3 Dayswww.guru99.comHive - Introductionwww.tutorialspoint.comHive Performance - 10 Best Practices for Apache Hive Qubolewww.qubole.comTop Hive Commands with Examples in HQL Edureka blogwww.edureka.coRecommended to you b