Search This Blog

Showing posts with label Apache Hive. Show all posts
Showing posts with label Apache Hive. Show all posts

Connecting to Hive database with dynamic service discovery and without dynamic service discovery

We will learn how to use dynamic service discovery in Hive and what are the advantages of dynamic service discovery feature.

First we will see what are the issues faced if we do not use dynamic service discovery in Hive .


1) 

Hive provides two prompts to run hive queries. They are hive prompt and beeline prompt. Hive prompt is deprecated , we need to use beeline prompt.

The below picture show how to connect to beeline prompt.



2) 

Before running Hive queries , We need to establish  a connection to Hive data base. We can establish a connection with dynamic  service discovery and with out dynamic service discovery.

We will see how to connect to database without dynamic service discovery.


We need to know below things to establish a connection to Hive data base .

HiveServer 2 host name : Host name or IP address where hiveserver2  is running. Multiple nodes will be having hiveserver2 instances running. Please use one hostname or IP address.

Database name : Which database in Hive you want to connect.  We will see how to connect to default database.

Port number : Hiveserver2 port number. default value is 10000.

User name : User name for database. We are using hive here.

Password  : Password for database. We are using hive here.


The below picture shows how to connect to hive server2 without dynamic service discovery.

Connection string used : jdbc:hive2://master1:10000/default

jdbc:hive2:// is fixed for all conection strings.

User name and password used : hive and hive



3) 

We will have multiple hiveserver2 instances running on the cluster. Assume that we have connected to database using hiveserver2 running on master1 host,  If hiveserver 2 on master1 is down, Hive queries would fail.

Other problem is we are also increasing the load on one hiveserver2 instance by hard coding it.


In the below picture , first query was successful but second query failed as hiveserver2 went down on master1.





4) 

Hive provides advanced feature called dynamic service discovery to address the above problems.

In dynamic service discovery , Rather than using hiveserver2 host name directlry,We will use zookeeper to connect to hive database.

Zookeeper will always resolve to active hiveserver2 so that your queries never fail.

We need below things to use dynamic service discovery .

Host names and port numbers  where your zookeeper is running . We also call it as Zookeeper ensemble.
We can easily get this value from property hive.zookeeper.quorum in Hive.

zookeeper's default port number is 2181. You can get zookeeper host names from Zookeeper configuration files also.


Specify service discovery mode using serviceDiscoveryMode=zooKeeper .


Specify zookeeper namespace as hivesever2. This is the value of hive.server2.zookeeper.namespace property in Hive.


We are using below connection string .

jdbc:hive2://datanode1:2181,master1:2181,master2:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2


The picture below shows how to connect to Hive database using dynamic service discovery.



5)

Many people have typos in dynamic service discovery connection string , Easiest way to get correct connection string to take it from Ambari GUI.

In Ambari -------->  Click on Hive  --------------------> Click Summary ----------------> Click Left arrow icon to copy connection string.




We can directly paste connection string in the terminal once copied from Ambari to avoid typos.

6)

Benefits of Dynamic Service Discovery.

High Availability  :

Dynamic service discovery will always point user queries to active hiveserver2 so that hive queries would never fail.


Load balancing  :

Dynamic service discovery changes hiveserver2 to every new user query in round rpobin fashion so that all hiveserver2 instances would get the same load.

It is always recommended to use dynamic service discovery in production to avoid hiveserver2 crashes.

Let me know if any questions you have.

Checking versions of Hadoop eco system tools


We need to know versions of hadoop technologies while we are trouble shooting any Hadoop issues. This article talks how to check versions of  Hadoop ecosystem technologies.

1) Hadoop version

Hadoop version can be found using below command.

hadoop version




2) Hive version

Hive version can be found using below command.

hive version



3) Pig version

Pig version can be found using below command.

pig version




4) Sqoop version

Sqoop version can be found using below command.

sqoop version



5) Tez version 

Tez version can be found using below rpm command.

rpm -qa|grep -i tez



6) Zookeeper version

Zookeeper version can be found using below rpm command.

rpm -qa|grep -i zookeeper




7) Hortonworks Data Platform  (HDP) version 

HDP version can be found using below command.

hdp-select versions



8) Knox version

Apache Knox version can be found using below rpm command.

rpm -qa|grep -i knox



9) Ranger version

Apache Ranger version can be found using below rpm command.

rpm -qa|grep -i ranger

We can also check version file for ranger version.



10)Checking all  versions from Ambari Web UI

Goto ---> Admin ---> Service accounts and versions



Let me know if you want to know particular Hadoop technology version.


How to create Hive table for Parquet data format file ?

In this article we will learn How to create Hive table for parquet file format data. We  need to use stored as Parquet  to create a hive table for Parquet file  format data.


1) Create  hive table without location.

We can create hive table for Parquet data without location.  And we can load data into that table later.

Command :

create table employee_parquet(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' stored as Parquet ;




2) Load data into hive table .

We can use regular insert query to load data into parquet file format table. Data will be converted into parquet file format implicitely while loading the data.

 insert into table employee_parquet select * from employee;



3) Create hive table with location

We can  also create hive table for parquet file data  with location. Specified location should have parquet file format data.

Command :

create table employee_parquet(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' 
stored as parquet location '/data/in/employee_parquet' ;



How to create hive table for RC file format ?


In this article we will learn How to create Hive table for RC file format data. We  need to use stored as RCFILE to create a hive table for RCFILE format data.


1) Create  hive table without location.

We can create hive table for RCFILE data without location.  And we can load data into that table later.

Command :

create table employee_rc(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' stored as RCFILE ;



2) Load data into hive table .

We can use regular insert query to load data into RC file format table. Data will be converted into RC file format implicitely while loading the data.

 insert into table employee_rc select * from employee;



3) Create hive table with location

We can  also create hive table for RC file data  with location. Specified location should have RC file format data.

Command :

create table employee_rc(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' 
stored as RCFILE location '/data/in/employee_rc' ;


How to create Hive table for sequece file format data ?

In this article we will learn How to create Hive table for sequence file format data. We  need to use stored as SequenceFile to create a hive table for sequence file format data.


1) Create  hive table without location.

We can create hive table for sequence file data without location.  And we can load data into that table later.

Command :

create table employee_seq(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' stored as SequenceFile ;



2) Load data into hive table .

We can use normal insert query to load data into sequence file format table. Data will be converted into sequence file format while loading the data.

 insert into table employee_seq select * from employee;



3) Create hive table with location

We can  also create hive table for sequence file data  with location. Specified location should have sequence file format data.

Command :

create table employee_seq(name string,salary int,deptno int,DOJ date)  row format delimited fields terminated by ',' 
stored as SequenceFile location '/data/in/employee_seq' ;


Working with databases in Apache Hive

In this article We will learn how to work on databases in Apache Hive. We will learn how to create, drop, change and use database in Apache Hive.

1) Check existing databases.

We check existing databases in Hive using show databases command. Apache Hive comes with a database called default.

Command :

show databases;


2) Creating a new database;

We can create a new database in Apache Hive using create command.

Command syntax:

create database [if not exist] {database-name};




3) Switching databases

By default , Queries will be run on default databases. If we want to run a query on different database, We have to change the current database.

We use use command to change the current database in Apache Hive.

Command :

use test;

The picture below changes current database to test database and creates a new table called dummy in test database.


4) Drop database

We can drop databases using drop command. We need to drop all tables in the database before droping the database. Otherwise we get database is not empty, one or more tables exist  error.

Command :

drop database test;

The picture below shows droping databse called test after deleting it's table dummy.

5) Using database name in a hive query

If we do not specify any database name in hive query, Hive runs it on current database.

We have to prefix database name to table name if we want to run the hive query on a particular database.

The query below runs on test database.

Select count(*) from test.dummy;





Using where clause in Apache Hive query

In this article, We will learn where  clause in Apache Hive.
Where clause is used to filter the column data that satisfies given condition. We will learn how to use where clause in different ways in the following steps.

1) Create table called employee in Apache Hive using this article.

2)  Check the data in Hive table employee.

Select * from employee;




3) Where clause in Hive  supports many operators.

The query below uses equal operator and filters the data if deptno column matches  to 10.

 select * from employee where deptno=10;



4) Where clause in Hive also allows funtions.

The query below selects data from employee table if year of joining is 2014. year is the hive function that is performed on doj column.

 select * from employee where year(doj)=2014;



5) Where clause also allows like operator to filter the data using a pattern.

The query below selects the data from employee table if name starts with 'B' using like operator.

select * from employee where name like 'B%'



6) Using where clause with in operator.

We define set of values using in operator. IN operator allows us to compare  a column data against set of values.

The query below selects the data from employee table if deptno equals to 10 or 20.

select * from employee where deptno in (10,20);




7) Subquery in Where  clause is also allowed.



Please check here to learn more about subqueries in where clause.

We have learned how to use where clause :

1) Plain where clause

2) Using fuction in where clause

3) Using like operator in where clause to search column data with a pattern.

4) Using like operator with in operator

5) Using subquery in where clause





Using order by clause in Apache Hive.

Order by clause in Apache Hive is used to arrange data of column(s) in desired order. We can arrange the column(s) data either in ascending order or descending order. Ascending order is the default order.

You can practise below queries to understand order-by clause practically.

1) Create a table called employee using this article.

2) Run select query and check the output.

Query:

select * from employee;



3) Sort the output data by deptno column and compare second step output to understand how data is sorted.

The following query sorts deptno column data in ascending order.

Query :

select * from employee order by deptno;




By default output will be in ascending order.

4) Use desc keyword to sort in descending order.

The following query sorts deptno column data in descending order.

select * from employee order by deptno desc.



5) We can use multiple columns in order by clause.

The following query sorts first deptno column data and salary column data later.

Query:

select * from employee order by deptno,salary;


Compare the above output with 3 step output to understand how multi column order by clause works.

After sorting first column, Order by picks every uniq value in first column i.e. deptno and sorts second column data i.e. salary data against deptno uniq value. In the above step , Only for deptno value 15 ,  it sorts salary values 325000,350000 in ascending order  as rest of the data for deptno was already in ascending order.


Usage of subqueries in Apache Hive queries

In this article, we will learn how and where to use subqueries in Apache Hive. Sub-queries in Hive queries are allowed in from clause ad where clause.

The following steps explain you in detail about subqueries in Hive.



1) Create a department table as explained in this article.

2)  In From clause.

The following query uses subquery in from clause.

select name,newsalary from (select name,salary*1.1 as newsalary from employee) emp_new;




Subquery columns can be accessed in main query. In the above query, We have accessed newsalary column in main query from subquery.


Union and Union all are also allowed in the subquery of from clause.


select name,newsalary from (
select name,salary*1.1 as newsalary from employee
union
select name,salary from employee
) emp_new;





2)  In where clause

Subquery  in the where clause is used to define list of elements.

The following query defines list of deptno elements from department table. and the main query retrieves names from employee table whose department number available in department table.

select name from employee where deptno in (select deptno from department) ;




We can use both in or not in in where clause.

In or not in subqueries should have only one column in subquery.

If subquery has morethan one column, Hive throws an error SubQuery can contain only 1 item in Select List.



Sub queries need to be only right side , Left side sub-queries not allowed.

The folowing query is not valid as subquery defined left side.

select name from employee where  (select deptno from department)d in (10,20);






We can use exists or not exists in where clause to compare main query data with subquery data.

The query below compares deptno in employee table to deptno in department table and retrieves matched rows.

select name from employee e where exists (select name from department d where e.deptno=d.deptno) ;



Hope it is clear now for you, Please comment your questions if you get any.

Happy Hadooping !!!!!!

How to create a new Hive table using select query output

In this article we will learn how to create a new table in Hive using other query output.

We use create table as select statement to create a new table from select query output data.

Syntax is :

create table [table-name] as [select-query]

Practise below steps to unnderstand this feature better.

1) Create a hive table called employee using this article.

2) Run a select query to get deptno wise employee count on employee table.

select deptno,count(name) from employee group by deptno



If you want to persist above query output  in a hive table , You have to use create table as select (CTAS) query.

3) Create a new table called deptnowiseempcount using above select query output.

create table deptnowiseempcount as
select deptno,count(name) from employee group by deptno;




Table deptnowiseempcount is a managed table.

CTAS has some restrictions like below.


  • New table can not be a external table.

  • New table can not be partitioned table.

  • New table table can not be a bucketed table.


4) The query below fails as the query is trying to create external table using CTAS statement.

 create external table deptnowiseempcount as select deptno,count(name) from employee group by deptno;



 Query fails throwing an error  CREATE-TABLE-AS-SELECT cannot create external table.





Creating a new Hive table with same schema of anothe Hive table.

In this article we will learn how to create a table with same schema of another table.

We use like operator in Apache Hive to copy schema from another table.

Syntax is : create table [new-table-name] like [old-table-name];

Practise below steps to understand better.

1) Create a new table called employee in Hive using this article.

2) Check the schema of Hive tables employee using describe command.

describe employee;



3) Create a new table called employee_india with same column names and data types of employee table.

create table employee_india like employee;



4) Now check column names and their data types of table employee_india.

describe employee_india;


We can copy schema not only from existing table, but also from existing existing view. Systax is also same , we just have to replace old-tablename with view name.