SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
Rupsee
 

Tendances (20)

Database backup & recovery
Database backup & recoveryDatabase backup & recovery
Database backup & recovery
 
Hadoop Architecture
Hadoop ArchitectureHadoop Architecture
Hadoop Architecture
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
 
6.hive
6.hive6.hive
6.hive
 
Apache hive
Apache hiveApache hive
Apache hive
 
Database backup and recovery basics
Database backup and recovery basicsDatabase backup and recovery basics
Database backup and recovery basics
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recovery
 
Map Reduce
Map ReduceMap Reduce
Map Reduce
 
Lec 7 query processing
Lec 7 query processingLec 7 query processing
Lec 7 query processing
 
Spatial Database
Spatial DatabaseSpatial Database
Spatial Database
 
DBMS an Example
DBMS an ExampleDBMS an Example
DBMS an Example
 
Big Data and Hadoop
Big Data and HadoopBig Data and Hadoop
Big Data and Hadoop
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 
Map reduce in BIG DATA
Map reduce in BIG DATAMap reduce in BIG DATA
Map reduce in BIG DATA
 
Hadoop
HadoopHadoop
Hadoop
 
SQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJSQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJ
 
Database security
Database securityDatabase security
Database security
 
Temporal databases
Temporal databasesTemporal databases
Temporal databases
 
Schemaless Databases
Schemaless DatabasesSchemaless Databases
Schemaless Databases
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
 

Similaire à 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
 
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptxShshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
086ChintanPatel1
 

Similaire à 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, 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
 
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...
 
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptxShshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
Shshsjsjsjs-4 - Copdjsjjsjsjsjakakakaaky.pptx
 
Tunning overview
Tunning overviewTunning overview
Tunning overview
 

Plus de Rohit Agrawal

Plus de 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
 

Dernier

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
 
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
 

Dernier (20)

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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
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
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 

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;