Hive concat_ws function using the multiple rows are combined into one line

  1. Create a test table

     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 test data

     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');
    

    View

  3. The multiple rows of data into a row

     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

Guess you like

Origin blog.csdn.net/oZuoLuo123/article/details/90229346