hive join 主要包括join(内连接)、left join(左连接)、right join(右连接)、full join(全连接)、left semi join(左半连接)、map side join(map端连接)六种用法,其中inner join 和 join等价,left outer join 和 left join等价,right outer join 和 right join 等价,full outer join 和 full join等价。 正确选择合适的join 类型在开发过程中可以提升效率。在Hive中,JOIN操作用于将两个或多个表中的数据连接在一起,以便进行联合查询和分析。在连接查询的时候,如果不指定,那么默认使用的是inner join在Hive中,除了支持上述比较常用的join以外,还支持left semi join。当a left semi join b的时候,表示获取a表中的数据哪些在b表中出现过
在hdfs上创建两个目录orders和products
order.txt
2001 20240710 3 21
2002 20240710 4 1001
2003 20240710 3 401
2004 20240711 2 14
2005 20240711 2 45
2006 20240823 4 10
2007 20240823 2 31
2008 20240823 3 23
2009 20240912 2 10
2010 20240912 2 21
2011 20240914 3 17
2012 20240914 3 19
product.txt
1 one 4999
2 two 6999
3 three 6999
4 four 7999
use demo;
create external table products (productid int, name string, price double) row format delimited fields terminated by ' ' location '/userorders/products';
create external table orders (orderid int, orderdate string,productid int, nm int) row format delimited fields terminated by ' ' location '/userorders/orders';
右连接-以右表为准:
select * from orders o right join products p on o.productid = p.productid;
左连接-以左表为准
select * from orders o left join products p on o.productid = p.productid;
内连接 - 获取两个表都有的数据:
select * from orders o inner join products p on o.productid = p.productid;
1:查询被卖出去过的商品 - 获取商品表中的哪些数据在订单表中出现过
select * from products p left semi join orders o on p.productid = o.productid;
2:获取每天卖了多少钱:
select o.orderdate, sum(o.nm*p.price) from orders o inner join products p on o.productid = p.productid group by o.orderdate;