Concatenating Rows in Apache Hive

The CONCAT_WS function in Apache Hive incorporates two or more strings into a single string with the specified delimiter.

Example:
SELECT fname, lname, CONCAT_WS(‘ ‘, fname, lname) FROM firstlastnames;

concat_ws2

However, this function can be used to combine row values into a single string. Using CONCAT WS along with COLLECT SET we can join the row values into a single string. This functionality would be equivalent to GROUP CONCAT in MySQL and Apache Impala, STRING AGG in Microsoft SQL Server and LISTAGG in Oracle SQL.

Let’s see how we can do that.

Here is my test-data.

salesdata_for_concat

SELECT Product, CONCAT_WS(‘,’, COLLECT_SET(CAST(sales AS STRING)))
FROM SalesData
GROUP BY Product

concat_ws_1
concat_ws_2

Hope you liked this post.

Please click on the follow button to receive notifications on latest posts.

.

4 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s