-1

My column has first name and last name separated by SPACE. I want to use pig function to split into 2 different columns. I think of STRSPLIT function but I don't know how to use it. Could anyone help me on this simple question?

Dhyaan
  • 1
  • 1
  • 2

1 Answers1

1

You can try something like this, sample code below
here what i am doing is
1.Reading each line as single column
2.Apply the STRSPLIT function using space as delimiter
3.Store the firstname and lastname into two different columns

input.txt
Pearson Charles
James  Michael
Smith Linda

PigScript:
A = LOAD 'input.txt' AS line;
B = FOREACH A GENERATE FLATTEN(STRSPLIT(line,'\\s+',2)) AS (firstname:chararray,lastname:chararray);
C = FOREACH B GENERATE firstname,lastname;
DUMP C;

Output:
(Pearson,Charles)
(James,Michael)
(Smith,Linda)

Check more info from this link
http://pig.apache.org/docs/r0.13.0/func.html#strsplit

Sivasakthi Jayaraman
  • 4,724
  • 3
  • 17
  • 27
  • Sivasakthi Jayaraman one question in my legacy code I see STRSPLIT(line,'_',1), it always returns same as input line. Not sure why this is being used..Any thoughts on this – vinayak_narune Jun 09 '21 at 09:31