我有一个名为find_distance的Hive UDF,它计算一对lat-long坐标之间的坐标距离。 我还有一个表格,其中包含城市名称列表及其各自的纬度坐标。 所以目前如果我需要找到两个城市之间的距离,比如丹佛和圣何塞,我需要进行自我加入:
Select find_Distance(cityA.latitude, cityA.longitude, cityB.latitude, cityB.longitude) from
(select latitude, longitude from city_data.map_info where city = 'Denver') cityA
join
(select latitude, longitude from city_data.map_info where city = 'San Jose') cityB;
我如何构建一个只接受城市名称作为参数的视图?所以实际上我可以使用
SELECT distance from city_distance where cityA = 'Denver' and cityB = 'San Jose'
答案 0 :(得分:1)
试试这个视图:
CREATE VIEW city_distance AS
SELECT
cityA.city as city_from,
cityA.city as city_to,
find_Distance(cityA.latitude, cityA.longitude, cityB.latitude, cityB.longitude) as distance
FROM
(SELECT city, latitude, longitude FROM city_data.map_info) cityA
CROSS JOIN
(SELECT city, latitude, longitude FROM city_data.map_info) cityB;