Multiple rows to columns in hive

0 votes
country-category male_cnt female_cnt total_cnt
US north 100 80 180
US south 100 80 180
US east 100 80 180
US west 100 80 180
IN north 100 80 180
IN south 100 80 180
IN east 100 80 180
IN west 100 80 180
MX north 100 80 180
MX south 100 80 180
MX east 100 80 180
JP south 100 80 180
JP west 100 80 180

I need to convert the 4 rows for each country into a single row output. when the category is north/south I need to pick the male_cnt and when the category east/west I need to pick the female_cnt. If a category is not available for a country the value should null. The order of category is important in the output. It can be any order but for all records, it should remain the same.

Desired output:
US,north,100,south,100,east,80,west,80
IN,north,100,south,100,east,80,west,80
MX,north,100,south,100,east,80,west,
JP,north,,south,100,east,,west,80
US,100,100,80,80
IN,100,100,80,80
MX,100,100,,80,,
JP,,100,,80

Please assist. I used CONCAT_WS and COLLECT_SET so far to achieve the desired output but not getting the right output.

Sep 4, 2020 in Big Data Hadoop by anonymous
• 120 points

edited Sep 4, 2020 by MD 2,883 views

1 answer to this question.

0 votes

Hi,

You need to create your own customize commands according to your use case. So first filter your dataset with the help of the country field and store them in four different data frames. After that use for loop and conditions to search your match. For loop will iterate depends on the length of your data frames. In this way, you have to create your own customized commands.

answered Sep 4, 2020 by MD
• 95,460 points

Related Questions In Big Data Hadoop

0 votes
1 answer

Hadoop Hive: How to convert multiple rows into comma separated values?

You can use the aggregator function collect_set to do ...READ MORE

answered Nov 14, 2018 in Big Data Hadoop by Omkar
• 69,220 points
5,428 views
0 votes
1 answer

Hadoop Hive: How to split a single row into multiple rows?

Try this SELECT ID1, Sub FROM tableName lateral view ...READ MORE

answered Nov 14, 2018 in Big Data Hadoop by Omkar
• 69,220 points
9,358 views
+1 vote
1 answer

How to limit the number of rows per each item in a Hive QL?

SELECT a_id, b, c, count(*) as sumrequests FROM ...READ MORE

answered Dec 1, 2018 in Big Data Hadoop by Omkar
• 69,220 points
27,014 views
0 votes
1 answer

How Impala is fast compared to Hive in terms of query response?

Impala provides faster response as it uses MPP(massively ...READ MORE

answered Mar 21, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
2,221 views
0 votes
1 answer

What is Metastore in Hive?

It stores metadata for Hive tables (like their schema ...READ MORE

answered Dec 20, 2018 in Big Data Hadoop by Frankie
• 9,830 points
3,140 views
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
11,035 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,542 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
108,861 views
0 votes
1 answer

How to add a column in Hive table?

Hi@akhtar, You can use the alter table command ...READ MORE

answered Oct 19, 2020 in Big Data Hadoop by MD
• 95,460 points
1,917 views
0 votes
1 answer

How to show all partitions of a table in Hive?

Hi@akhtar, You can list down all the partitions ...READ MORE

answered Dec 18, 2020 in Big Data Hadoop by MD
• 95,460 points
5,622 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP