SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Hive and HiveQL
What is Hive?
• Apache Hive is a data warehouse system for Hadoop.
• Hive is not a relational database, it only maintains metadata information about
your Big Data stored on HDFS.
• Hive allows to treat your Big Data as tables and perform SQL-like operations on
the data using a scripting language called HiveQL.
• Hive is not a database, but it uses a database (called the metastore) to store the
tables that you define. Hive uses Derby by default.
• A Hive table consists of a schema stored in the metastore and data stored on
HDFS.
• Hive converts HiveQL commands into MapReduce jobs.
Hive Architecture Contd..
Step 1: Issuing Commands
Using the Hive CLI, a Web interface, or a Hive JDBC/ODBC client, a Hive
query is submitted to the HiveServer.
Step 2: Hive Query Plan
The Hive query is compiled, optimized and planned as a MapReduce job.
Step 3: MapReduce Job Executes
The corresponding MapReduce job is executed on the Hadoop cluster.
Comparison with Traditional Database
Hive data types
Arithmetic Operators
Mathematical functions
Aggregate functions
Other built-in functions
Managed Tables
• When a table is created in Hive, by default Hive will manage the data, which
means that Hive moves the data into its warehouse directory.
• When data is loaded into a managed table, it is moved into Hive’s warehouse
directory.
CREATE TABLE managed_table (dummy STRING);
LOAD DATA INPATH '/user/tom/data.txt' INTO table managed_table;
• It will move the file hdfs://user/tom/data.txt into Hive’s warehouse directory for
the managed_table table, which is hdfs://user/hive/warehouse/managed_table
• If the table is later dropped, then the table, including its metadata and its data, is
deleted.
External Tables
• When a External table is created, it tells Hive to refer to the data that is at an
existing location outside the warehouse directory and it is not managed by Hive.
• The location of the external data is specified at table creation time
CREATE EXTERNAL TABLE external_table (dummy STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' LINES TERMINATED BY 'n'
LOCATION '/user/tom/external_table_location/file.txt';
• Creation and deletion of the data can be controlled.
• Hive tables can be organized into
buckets, which imposes extra
structure on the table and how the
underlying files are stored.
Bucketing has two key benefits:
• More efficient queries: especially
when performing joins on the
same bucketed columns.
• More efficient sampling: because
the data is already split up into
smaller pieces.
Storage Formats
• There are two dimensions that govern table storage in Hive
• Row format : The row format dictates how rows, and the fields in a particular row, are
stored. The row format is defined by a SerDe.
• File format : The file format dictates the container format for fields in a row.
The default storage format: Delimited text
• When a table is created with no ROW FORMAT or STORED AS clauses, the default format is
delimited text, with a row per line.
• The default row delimiter is not a tab character, but the Control-A character.
• The default collection item delimiter is a Control-B character, used to delimit items in an ARRAY
or STRUCT, or key-value pairs in a MAP.
• The default map key delimiter is a Control-C character, used to delimit the key and value in a
MAP.
• Rows in a table are delimited by a newline character.
Storage Formats Contd..
CREATE TABLE XYZ;
is identical to the more explicit:
CREATE TABLE ...
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '001'
COLLECTION ITEMS TERMINATED BY '002'
MAP KEYS TERMINATED BY '003'
LINES TERMINATED BY 'n'
STORED AS TEXTFILE;
Importing Data
INSERT OVERWRITE TABLE
INSERT OVERWRITE TABLE target
SELECT col1, col2
FROM source;
• Forpartitionedtables,youcanspecifythepartitiontoinsertintobysupplyingaPARTITION
clause:
INSERT OVERWRITE TABLE target
PARTITION (dt='2010-01-01')
SELECT col1, col2
FROM source;
Importing Data Contd..
Multitable insert
FROM records2
INSERT OVERWRITE TABLE stations_by_year
SELECT year, COUNT(DISTINCT station)
GROUP BY year
INSERT OVERWRITE TABLE records_by_year
SELECT year, COUNT(1)
GROUP BY year
INSERT OVERWRITE TABLE good_records_by_year
SELECT year, COUNT(1)
WHERE temperature != 9999
AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9)
GROUP BY year;
Importing Data Contd..
CREATE TABLE...AS SELECT
CREATE TABLE target
AS
SELECT col1, col2
FROM source;
• A CTAS operation is atomic, so if the SELECT query fails for some
reason, then the table is not created.
Altering Tables
• ALTER TABLE source RENAME TO target;
• ALTER TABLE target ADD COLUMNS (col3 STRING);
Dropping Tables
DROP TABLE table_name;
• The DROP TABLE statement deletes the data and metadata for a table. In the
case of external tables, only the metadata is deleted—the data is left
untouched.
Querying data(Sorting and Aggregating)
• Sorting data in Hive can be achieved by use of a standard ORDER BY clause.
ORDER BY produces a result that is totally sorted, so sets the number of reducers
to one.
• SORT BY produces a sorted file per reducer.
• DISTRIBUTE BY clause used to control which reducer a particular row goes to.
• Inner joins
Querying data(Joins)
• Left Outer Join
• Right Outer Join
• Full Outer Join
• Left Semi Join
Subqueries
• A subquery is a SELECT statement that is embedded in another SQL statement.
Hive has limited support for subqueries, only permitting a subquery in the FROM
clause of a SELECT statement.
SELECT station, year, AVG(max_temperature)
FROM (
SELECT station, year, MAX(temperature) AS max_temperature
FROM records2
WHERE temperature != 9999
AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9)
GROUP BY station, year
) mt
GROUP BY station, year;
Views
• A view is a sort of “virtual table” that is defined by a SELECT statement.
CREATE VIEW max_temperatures (station, year, max_temperature) AS
SELECT station, year, MAX(temperature)
FROM valid_records
GROUP BY station, year;
• With the views in place, we can now use them by running a query:
SELECT station, year, AVG(max_temperature)
FROM max_temperatures
GROUP BY station, year;
Hive Join Strategies
Hive and HiveQL - Module6
Hive and HiveQL - Module6
Hive and HiveQL - Module6

Weitere ähnliche Inhalte

Was ist angesagt?

Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Simplilearn
 
Introduction to Apache Hive(Big Data, Final Seminar)
Introduction to Apache Hive(Big Data, Final Seminar)Introduction to Apache Hive(Big Data, Final Seminar)
Introduction to Apache Hive(Big Data, Final Seminar)
Takrim Ul Islam Laskar
 
No sql distilled-distilled
No sql distilled-distilledNo sql distilled-distilled
No sql distilled-distilled
rICh morrow
 

Was ist angesagt? (20)

Hadoop YARN
Hadoop YARNHadoop YARN
Hadoop YARN
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
 
Hadoop Ecosystem
Hadoop EcosystemHadoop Ecosystem
Hadoop Ecosystem
 
Introduction to Apache Hive(Big Data, Final Seminar)
Introduction to Apache Hive(Big Data, Final Seminar)Introduction to Apache Hive(Big Data, Final Seminar)
Introduction to Apache Hive(Big Data, Final Seminar)
 
Key-Value NoSQL Database
Key-Value NoSQL DatabaseKey-Value NoSQL Database
Key-Value NoSQL Database
 
Map reduce prashant
Map reduce prashantMap reduce prashant
Map reduce prashant
 
Unit 3
Unit 3Unit 3
Unit 3
 
Introduction To HBase
Introduction To HBaseIntroduction To HBase
Introduction To HBase
 
No sql distilled-distilled
No sql distilled-distilledNo sql distilled-distilled
No sql distilled-distilled
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Apache Hadoop
Apache HadoopApache Hadoop
Apache Hadoop
 
Introduction to HBase
Introduction to HBaseIntroduction to HBase
Introduction to HBase
 
Data models in NoSQL
Data models in NoSQLData models in NoSQL
Data models in NoSQL
 
Intro to HBase
Intro to HBaseIntro to HBase
Intro to HBase
 
HTML CSS Basics
HTML CSS BasicsHTML CSS Basics
HTML CSS Basics
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Caching
CachingCaching
Caching
 
Map Reduce
Map ReduceMap Reduce
Map Reduce
 
Web Development using HTML & CSS
Web Development using HTML & CSSWeb Development using HTML & CSS
Web Development using HTML & CSS
 

Ähnlich wie Hive and HiveQL - Module6

Clase 11 manejo tablas modificada
Clase 11 manejo tablas   modificadaClase 11 manejo tablas   modificada
Clase 11 manejo tablas modificada
Titiushko Jazz
 
Clase 11 manejo tablas modificada
Clase 11 manejo tablas   modificadaClase 11 manejo tablas   modificada
Clase 11 manejo tablas modificada
Titiushko Jazz
 
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptxhive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
OmarBen27
 

Ähnlich wie Hive and HiveQL - Module6 (20)

03 hive query language (hql)
03 hive query language (hql)03 hive query language (hql)
03 hive query language (hql)
 
Ten tools for ten big data areas 04_Apache Hive
Ten tools for ten big data areas 04_Apache HiveTen tools for ten big data areas 04_Apache Hive
Ten tools for ten big data areas 04_Apache Hive
 
Hive_An Brief Introduction to HIVE_BIGDATAANALYTICS
Hive_An Brief Introduction to HIVE_BIGDATAANALYTICSHive_An Brief Introduction to HIVE_BIGDATAANALYTICS
Hive_An Brief Introduction to HIVE_BIGDATAANALYTICS
 
Advance Hive, NoSQL Database (HBase) - Module 7
Advance Hive, NoSQL Database (HBase) - Module 7Advance Hive, NoSQL Database (HBase) - Module 7
Advance Hive, NoSQL Database (HBase) - Module 7
 
Apache hive
Apache hiveApache hive
Apache hive
 
Apache Hive, data segmentation and bucketing
Apache Hive, data segmentation and bucketingApache Hive, data segmentation and bucketing
Apache Hive, data segmentation and bucketing
 
Apache Hive
Apache HiveApache Hive
Apache Hive
 
Apache Hive
Apache HiveApache Hive
Apache Hive
 
Clase 11 manejo tablas modificada
Clase 11 manejo tablas   modificadaClase 11 manejo tablas   modificada
Clase 11 manejo tablas modificada
 
Clase 11 manejo tablas modificada
Clase 11 manejo tablas   modificadaClase 11 manejo tablas   modificada
Clase 11 manejo tablas modificada
 
6.hive
6.hive6.hive
6.hive
 
Hive @ Bucharest Java User Group
Hive @ Bucharest Java User GroupHive @ Bucharest Java User Group
Hive @ Bucharest Java User Group
 
Introduction to Apache HBase, MapR Tables and Security
Introduction to Apache HBase, MapR Tables and SecurityIntroduction to Apache HBase, MapR Tables and Security
Introduction to Apache HBase, MapR Tables and Security
 
01 hbase
01 hbase01 hbase
01 hbase
 
Apache Drill talk ApacheCon 2018
Apache Drill talk ApacheCon 2018Apache Drill talk ApacheCon 2018
Apache Drill talk ApacheCon 2018
 
Amazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and Optimization
 
Юра Гуляев. Oracle tables
Юра Гуляев. Oracle tablesЮра Гуляев. Oracle tables
Юра Гуляев. Oracle tables
 
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptxhive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
hive_slidesjhsdjhsasdfksnfjisnvosjnv-2.pptx
 
Apache hive introduction
Apache hive introductionApache hive introduction
Apache hive introduction
 
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
Best Practices and Performance Tuning of U-SQL in Azure Data Lake (SQL Konfer...
 

Mehr von Rohit Agrawal

Mehr von Rohit Agrawal (8)

Apache Oozie Workflow Scheduler - Module 10
Apache Oozie Workflow Scheduler - Module 10Apache Oozie Workflow Scheduler - Module 10
Apache Oozie Workflow Scheduler - Module 10
 
Hadoop 2.0, MRv2 and YARN - Module 9
Hadoop 2.0, MRv2 and YARN - Module 9Hadoop 2.0, MRv2 and YARN - Module 9
Hadoop 2.0, MRv2 and YARN - Module 9
 
Advance HBase and Zookeeper - Module 8
Advance HBase and Zookeeper - Module 8Advance HBase and Zookeeper - Module 8
Advance HBase and Zookeeper - Module 8
 
Pig and Pig Latin - Module 5
Pig and Pig Latin - Module 5Pig and Pig Latin - Module 5
Pig and Pig Latin - Module 5
 
Advance MapReduce Concepts - Module 4
Advance MapReduce Concepts - Module 4Advance MapReduce Concepts - Module 4
Advance MapReduce Concepts - Module 4
 
Hadoop MapReduce framework - Module 3
Hadoop MapReduce framework - Module 3Hadoop MapReduce framework - Module 3
Hadoop MapReduce framework - Module 3
 
Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2Hadoop Cluster Configuration and Data Loading - Module 2
Hadoop Cluster Configuration and Data Loading - Module 2
 
Introduction to Big Data & Hadoop Architecture - Module 1
Introduction to Big Data & Hadoop Architecture - Module 1Introduction to Big Data & Hadoop Architecture - Module 1
Introduction to Big Data & Hadoop Architecture - Module 1
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Hive and HiveQL - Module6

  • 2. What is Hive? • Apache Hive is a data warehouse system for Hadoop. • Hive is not a relational database, it only maintains metadata information about your Big Data stored on HDFS. • Hive allows to treat your Big Data as tables and perform SQL-like operations on the data using a scripting language called HiveQL. • Hive is not a database, but it uses a database (called the metastore) to store the tables that you define. Hive uses Derby by default. • A Hive table consists of a schema stored in the metastore and data stored on HDFS. • Hive converts HiveQL commands into MapReduce jobs.
  • 3.
  • 4. Hive Architecture Contd.. Step 1: Issuing Commands Using the Hive CLI, a Web interface, or a Hive JDBC/ODBC client, a Hive query is submitted to the HiveServer. Step 2: Hive Query Plan The Hive query is compiled, optimized and planned as a MapReduce job. Step 3: MapReduce Job Executes The corresponding MapReduce job is executed on the Hadoop cluster.
  • 11. Managed Tables • When a table is created in Hive, by default Hive will manage the data, which means that Hive moves the data into its warehouse directory. • When data is loaded into a managed table, it is moved into Hive’s warehouse directory. CREATE TABLE managed_table (dummy STRING); LOAD DATA INPATH '/user/tom/data.txt' INTO table managed_table; • It will move the file hdfs://user/tom/data.txt into Hive’s warehouse directory for the managed_table table, which is hdfs://user/hive/warehouse/managed_table • If the table is later dropped, then the table, including its metadata and its data, is deleted.
  • 12. External Tables • When a External table is created, it tells Hive to refer to the data that is at an existing location outside the warehouse directory and it is not managed by Hive. • The location of the external data is specified at table creation time CREATE EXTERNAL TABLE external_table (dummy STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' LINES TERMINATED BY 'n' LOCATION '/user/tom/external_table_location/file.txt'; • Creation and deletion of the data can be controlled.
  • 13.
  • 14. • Hive tables can be organized into buckets, which imposes extra structure on the table and how the underlying files are stored. Bucketing has two key benefits: • More efficient queries: especially when performing joins on the same bucketed columns. • More efficient sampling: because the data is already split up into smaller pieces.
  • 15. Storage Formats • There are two dimensions that govern table storage in Hive • Row format : The row format dictates how rows, and the fields in a particular row, are stored. The row format is defined by a SerDe. • File format : The file format dictates the container format for fields in a row. The default storage format: Delimited text • When a table is created with no ROW FORMAT or STORED AS clauses, the default format is delimited text, with a row per line. • The default row delimiter is not a tab character, but the Control-A character. • The default collection item delimiter is a Control-B character, used to delimit items in an ARRAY or STRUCT, or key-value pairs in a MAP. • The default map key delimiter is a Control-C character, used to delimit the key and value in a MAP. • Rows in a table are delimited by a newline character.
  • 16. Storage Formats Contd.. CREATE TABLE XYZ; is identical to the more explicit: CREATE TABLE ... ROW FORMAT DELIMITED FIELDS TERMINATED BY '001' COLLECTION ITEMS TERMINATED BY '002' MAP KEYS TERMINATED BY '003' LINES TERMINATED BY 'n' STORED AS TEXTFILE;
  • 17. Importing Data INSERT OVERWRITE TABLE INSERT OVERWRITE TABLE target SELECT col1, col2 FROM source; • Forpartitionedtables,youcanspecifythepartitiontoinsertintobysupplyingaPARTITION clause: INSERT OVERWRITE TABLE target PARTITION (dt='2010-01-01') SELECT col1, col2 FROM source;
  • 18. Importing Data Contd.. Multitable insert FROM records2 INSERT OVERWRITE TABLE stations_by_year SELECT year, COUNT(DISTINCT station) GROUP BY year INSERT OVERWRITE TABLE records_by_year SELECT year, COUNT(1) GROUP BY year INSERT OVERWRITE TABLE good_records_by_year SELECT year, COUNT(1) WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY year;
  • 19. Importing Data Contd.. CREATE TABLE...AS SELECT CREATE TABLE target AS SELECT col1, col2 FROM source; • A CTAS operation is atomic, so if the SELECT query fails for some reason, then the table is not created.
  • 20. Altering Tables • ALTER TABLE source RENAME TO target; • ALTER TABLE target ADD COLUMNS (col3 STRING); Dropping Tables DROP TABLE table_name; • The DROP TABLE statement deletes the data and metadata for a table. In the case of external tables, only the metadata is deleted—the data is left untouched.
  • 21. Querying data(Sorting and Aggregating) • Sorting data in Hive can be achieved by use of a standard ORDER BY clause. ORDER BY produces a result that is totally sorted, so sets the number of reducers to one. • SORT BY produces a sorted file per reducer. • DISTRIBUTE BY clause used to control which reducer a particular row goes to.
  • 23. • Left Outer Join • Right Outer Join • Full Outer Join • Left Semi Join
  • 24. Subqueries • A subquery is a SELECT statement that is embedded in another SQL statement. Hive has limited support for subqueries, only permitting a subquery in the FROM clause of a SELECT statement. SELECT station, year, AVG(max_temperature) FROM ( SELECT station, year, MAX(temperature) AS max_temperature FROM records2 WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY station, year ) mt GROUP BY station, year;
  • 25. Views • A view is a sort of “virtual table” that is defined by a SELECT statement. CREATE VIEW max_temperatures (station, year, max_temperature) AS SELECT station, year, MAX(temperature) FROM valid_records GROUP BY station, year; • With the views in place, we can now use them by running a query: SELECT station, year, AVG(max_temperature) FROM max_temperatures GROUP BY station, year;