How to pivot Spark DataFrame?

后端 未结 10 2139
闹比i
闹比i 2020-11-21 06:43

I am starting to use Spark DataFrames and I need to be able to pivot the data to create multiple columns out of 1 column with multiple rows. There is built in functionality

10条回答
  •  暖寄归人
    2020-11-21 07:19

    There are plenty of examples of pivot operation on dataset/dataframe, but I could not find many using SQL. Here is an example that worked for me.

    create or replace temporary view faang 
    as SELECT stock.date AS `Date`,
        stock.adj_close AS `Price`,
        stock.symbol as `Symbol` 
    FROM stock  
    WHERE (stock.symbol rlike '^(FB|AAPL|GOOG|AMZN)$') and year(date) > 2010;
    
    
    SELECT * from faang 
    
    PIVOT (max(price) for symbol in ('AAPL', 'FB', 'GOOG', 'AMZN')) order by date; 
    
    

提交回复
热议问题