Zach Moshe
Zach Moshe

Reputation: 2980

How to extract keys from map?

How do I extract all keys from a map field?
I have a bag of tuples where one of the fields is a map that contains HTTP headers (and their values). I want to create a set of all possible keys (in my dataset) for a HTTP header and count how many times I've seen them.

Ideally, something like:

A = LOAD ...
B = FOREACH A GENERATE KEYS(http_headers)
C = GROUP FLATTEN(B) BY $0
D = FOREACH C GENERATE group, COUNT($0)

(didn't test it but it illustrates the idea..)

How do I do something like this? If I can extract a bag of keys from a map it would actually solve it. I just couldn't find any function like this in piglatin's documentation.

Upvotes: 1

Views: 1629

Answers (1)

o-90
o-90

Reputation: 17593

Yes there is a command in Pig to accomplish this.

Example:

/*  data  */
[a#1,b#2,c#3]
[green#sam,eggs#I,ham#am]

A = load 'data' as (M:[]);
B = foreach A generate KEYSET($0);
dump B

Output:

({(b),(c),(a)})
({(ham),(eggs),(green)})

Upvotes: 2

Related Questions