利用Hive中concat_ws函数将多行记录合并成一行

  1. 创建测试表

     CREATE TABLE IF NOT EXISTS TEST.USERS (
         id string,
         k1 string,
         k2 string,
         k3 string
     ) COMMENT '用户表' 
     ROW FORMAT delimited fields terminated BY '\t' 
     STORED AS TEXTFILE;
    
  2. 插入测试数据

     INSERT OVERWRITE TABLE test.users VALUES
     ('1','a1','a2','a3'),
     ('1','a11','a22','a33'),
     ('1','a111','a222','a333'),
     ('2','b1','b2','b3'),
     ('2','b11','b22','b33');
    

    查看
    利用Hive中concat_ws函数将多行记录合并成一行_第1张图片

  3. 将多行数据转为一行

     SELECT id,concat('[',concat_ws( ',',
                             collect_set(
                                         concat(
                                                '{',
                                                '"k1":"',k1,
                                                '","k2":"',k2,
                                                '","k3":"',k3,
                                                '"}'
                                                )
                                         )
                             ),
                   ']'
                   )
     FROM test.users
     GROUP BY id;
    
```sql
SELECT id,
       concat(
              '[{',
              concat_ws(
                         '},{',
                         collect_set(
                                     concat(
                                            '"k1":"',k1,
                                             '","k2":"',k2,
                                             '","k3":"',k3,
                                             '"' 
                                            )
                                      )
                        ),
                '}]'
               )
FROM test.users
GROUP BY id

你可能感兴趣的:(hive)