Finding efficiently all relevant sub ranges for bigdata tables in Hive/ Spark

放肆的年华 提交于 2019-12-08 11:42:51

问题


Following this question, I would like to ask. I have 2 tables: The first table - MajorRange

row  | From   |  To     | Group ....
-----|--------|---------|---------
1    | 1200   |   1500  | A
2    | 2200   |   2700  | B
3    | 1700   |   1900  | C
4    | 2100   |   2150  | D
...

The second table - SubRange

row  | From   |  To     | Group ....
-----|--------|---------|---------
1    | 1208   |   1300  | E
2    | 1400   |   1600  | F
3    | 1700   |   2100  | G
4    | 2100   |   2500  | H
... 

The output table should be the all the SubRange groups who has overlap over the MajorRange groups. In the following example the result table is:

 row | Major  |  Sub | 
-----|--------|------|-
1    | A      |   E  | 
2    | A      |   F  |
3    | B      |   H  |
4    | C      |   G  |
5    | D      |   H  |

In case there is no overlapping between the ranges the Major will not appear. Both tables are big data tables.How can I do it using Hive/ Spark in most efficient way?


回答1:


With spark, maybe a non equi join like this?

val join_expr = major_range("From") < sub_range("To") && major_range("To") > sub_range("From")

(major_range.join(sub_range, join_expr)
 .select(
    monotonically_increasing_id().as("row"), 
    major_range("Group").as("Major"), 
    sub_range("Group").as("Sub")
  )
).show

+---+-----+---+
|row|Major|Sub|
+---+-----+---+
|  0|    A|  E|
|  1|    A|  F|
|  2|    B|  H|
|  3|    C|  G|
|  4|    D|  H|
+---+-----+---+


来源:https://stackoverflow.com/questions/46183563/finding-efficiently-all-relevant-sub-ranges-for-bigdata-tables-in-hive-spark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!