【问题标题】:Joining partitioned tables in Hive在 Hive 中加入分区表
【发布时间】:2017-01-28 01:28:29
【问题描述】:

假设我有两个分区表,分别是 customeritems,它们都由 countrystate 列分区。

鉴于我想检索特定国家和州的数据,这是加入这些表格内容的正确方法吗?

select 
  customer.id, 
  customer.name, 
  items.name, 
  items.value
from
  customers
  join items
  on customers.id == items.customer_id
  and customers.country == 'USA'
  and customers.state == 'TX'
  and items.country == 'USA'
  and items.state == 'TX'

或者这些条件应该放在 WHERE 子句中吗?

and customers.country == 'USA'
and customers.state == 'TX'
and items.country == 'USA'
and items.state == 'TX'

【问题讨论】:

  • 这些条件应该放在 WHERE 子句中。

标签: hive hiveql


【解决方案1】:

对于简单查询,Hive 将在 reduce 阶段之前推送谓词,因此在这种情况下,将条件放在“on”或“where”子句上的性能相同。但是,如果您编写其他查询来比较表之间的字段 (table1.a

【讨论】:

    【解决方案2】:

    我们可以加入分区表,分区只是文件夹结构,分区是指根据特定列的值将表划分为相关部分的方式,例如:日期、状态等。 例如,我有如下分区

    show partitions table_name1 
    year=2016/month=12/day=1/part=10
    
    show partitions table_name2 
    year=2016/month=12/day=1/part=1
    

    现在我们可以通过以下方式加入表格

    select i.col1, c.col1
    FROM (SELECT * FROM table_name1 WHERE year=2016 AND month=12 AND day=1) i
    JOIN (SELECT * FROM table_name2 WHERE year=2016 AND month=12 AND day=1) c
    ON i.col2= c.col2
    AND i.col3= c.col3
    AND i.col3= c.col3
    GROUP BY c.col1
    

    SELECT i.col1, c.col1
    FROM table_name1
    JOIN table_name2
    ON i.col2= c.col2
    AND i.col3= c.col3
    AND i.col3= c.col3
    AND i.year=2016 AND i.month=12 AND i.day=1
    AND c.year=2016 AND c.month=12 AND c.day=1
    GROUP BY c.col1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-13
      • 2012-10-20
      • 2018-01-03
      • 2014-12-05
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2016-12-13
      相关资源
      最近更新 更多