Pivot a multiple rows into one row with multiple columns (like reshape cast in R)

后端 未结 1 857
鱼传尺愫
鱼传尺愫 2021-01-29 01:48

I have a table that contains the data that for each product, they have certain attributes, say (limited to A,B,C for now). and I have the value corresponding to each attribute.

相关标签:
1条回答
  • 2021-01-29 02:32

    I'm not aware of a pivot function in Hive but this is possible. You will need to collect attribute and value to a map, which can be done using this UDAF

    Example:

    ADD JAR /root/path/to/jar/brickhouse-0.7.0.jar;
    CREATE TEMPORARY FUNCTION collect AS 'brickhouse.udf.collect.CollectUDAF';
    
    SELECT product
        , attr_map['A'] AS A
        , attr_map['B'] AS B
        , attr_map['C'] AS C
    FROM (
        SELECT product
            , collect(attribute, value) AS attr_map
        FROM test
        GROUP BY product
        ) x
    

    The caveat here is that if you have a lot of attributes, this can be quite a bit of repetitive code.

    0 讨论(0)
提交回复
热议问题