Creating a table:-
1.
create table t1 (id int, name
string, age int)row format delimited fields terminated by '\,';
2.
create table t2 (id int, name
string, age int)row format delimited fields terminated by '\,';
3.
create table t3(id int, name
string, age int, designation string, off_location string, salary double)row
format delimited fields terminated by '\,';
Loading data
into tables t1, t2 and t3:-
1.
load data local inpath
'/home/training/Desktop/t1.txt' into table t1;
2.
load data local inpath
'/home/training/Desktop/t2.txt' into table t2;
3.
load data local inpath
‘/home/training/Desktop/t3.txt’ into table t3;
Retrieving
all the rows from the tables:-
select * from t2;
select * from t3;
Simple join:-
Hive supports
only equality joins because it is primarily difficult to express non-equality
joins in map-reduce.
Syntax: select table_name_1.ColumnValue, table_name_2.ColumnValue from
table_name_1 JOIN table_name_2 on
(table_name_1.ColumnValue=table_name_2.ColumnValue)
select t1.id,t1.name,t1.age,t2.name,t2.age from t1 JOIN t2 ON(t1.id=t2.id);
select t1.id,t1.name,t2.age from t1 JOIN t2 ON(t1.id=t2.id);
Select t3.id,t3.designation,t2.name,t1.age from t3 join t2 on(t3.id=t2.id) join t1 on(t3.id=t1.id);
Inner join:
Select t2.name, t3.designation, t3.off_location from t2 JOIN t3 where t2.id=t3.id AND t2.name=t3.name;
Outer Join:
The table in an Outer Join that must return all rows. For left outer joins this is the Left table, for right outer joins it is the Right table, and for full outer joins both tables are Preserved Row tables.
Left outer join:-
Select t3.off_location, t3.id, t1.id from t3 left
outer join t1 on t3.id=t1.id;
Right outer join:-
Select t3.off_location, t3.id, t1.id from t3 right
outer join t1 on t3.id=t1.id;
Full Outer Join
Select t3.off_location, t3.id, t1.id from t3 full
outer join t1 on t3.id=t1.id;
No comments:
Post a Comment