Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

[SQL] spark sql is not equivalent join

Shulou Source: shulou.com Published: 2022-06-03 05:33:56 10月02日 Update

Products A table of changes in commodity prices, orders orders for goods, recording each purchase and date

Based on the non-equivalent join in Spark SQL, the matching of orders and products is realized, and the goods in each order are counted corresponding to the current price.

A slowly changing price list

Wang Zai milk, there has been a price change.

Scala > val products = sc.parallelize (Array (("Wang Zai Milk", "2017-01-01", "2018-01-01", 4), | ("Wang Zai Milk", "2018-01-02", "2020-01-01", 5), | ("Wang Laoji", "2017-01-02", "2019-01-01", 5), | ("Weilong spicy Gluten", "2010-01-01") "2020-01-01", 2) |). ToDF ("name", "startDate", "endDate", "price") products: org.apache.spark.sql.DataFrame = [name: string, startDate: string... 2 more fields] scala > products.show () +-+ | name | startDate | endDate | price | +-+ | Wangzai Milk | 2017-01-01 | 2018-01-01 | 4 | Wangzai Milk | 2018-01-02 | 2020-01-01 | 5 | Wang Laoji | 2017-01-02 | 2019-01-01 | 5 | Weilong spicy gluten | 2010 -01-01 | 2020-01-01 | 2 | +-+

Order form (commodity name, order date)

Wang Tsai milk had an order in different price periods.

Scala > val orders = sc.parallelize (Array (| ("2017-06-01", "Wang Zai Milk"), | ("2017-07-01", "Wang Laoji"), | ("2018-03-01", "Wang Zai Milk") |) .toDF ("date", "product") orders: org.apache.spark.sql.DataFrame = [date: string " Product: string] scala > orders.show+-+-+ | date | product | +-+-+ | 2017-06-01 | Wang Zai Milk | | 2017-07-01 | Wang Laoji | | 2018-03-01 | Wang Zai Milk | +-+-+

Calculate the current commodity price of each order through an unequal connection

Check out the prices of the two orders in different periods of time.

Scala > orders.join (products, $"product" = $"name" & & $"date" > = $"startDate" & & $"date"

Tags: Milk price commodity order Wang Lao different commodity price date change slow two price list name time period time period statistics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Docker Linux macOS