pig tutorial - apache pig tutorial - Splitting input into substrings in PIG (Hadoop) ? - pig latin - apache pig - pig hadoop

What is substring ?

  • A substring of a string is a string that occurs "in" . For example, "the best of" is a substring of "It was the best of times".
  • This is not to be confused with subsequence, which is a generalization of substring. For example, "Itwastimes" is a subsequence of "It was the best of times", but not a substring.

What is hadoop ?

  • Hadoop is an open source, Java-based programming framework that supports the processing and storage of extremely large data sets in a distributed computing environment
  • On Hadoop system using Apache Pig you can write very simple code that will split file on the fly. we will have the flexibility to control flow of data and do manipulations (if any) and split file.

Now we see how to split file into individual files using Pig Script. Here is our sample file


000001010K1DIBB  7RHUN  2100000AE            J82V  2269167AD         2002-03-079999-12-31+000000000100000    22004-02-28-
000002010K1DIBB  7RHUN  2100000AE            J82V  2269167AD         2002-03-072004-07-30+000000000100000    32004-02-28-
0000030108VV9IH  AKB7L  3300000AE            XMV9  2269167AD         2002-03-059999-12-31+000000000100000    22004-02-28-
0000040108VV9IH  AKB7L  3300000SE            XMV9  2269167AD         2002-03-052004-07-30+000000000100000    32004-02-28-
000005010ULY674  5XWJR  0100000SE            XMV9  2269167AD         2002-03-059999-12-31+000000000100000    22004-02-28-
000006010ULY674  5XWJR  0100000AE            XMV9  2269167AD         2002-03-052004-07-30+000000000100000    32004-02-28-
000007010QT0X36  RJPWK  5500000AE            J82V  2269167AD         2002-10-229999-12-31+000000000100000    22004-02-28-
000008010QT0X36  RJPWK  5500000AE            J82V  2269167AD         2002-10-222004-07-30+000000000100000    32004-02-28-
000009010S8LIKA  07L1X  4400000BE            J82V  2269167AD         2002-03-079999-12-31+000000000100000    22004-02-28-
000010010S8LIKA  07L1X  4400000BE            J82V  2269167AD         2002-03-072004-07-30+000000000100000    32004-02-28-
000011010QAS7G3  CO46Q  8500000BE            RI12  2269167AD         2002-03-059999-12-31+000000000100000    22004-02-28-
000012010QAS7G3  CO46Q  8500000BE            RI12  2269167AD         2002-03-052004-07-30+000000000100000    32004-02-28-
  • In the sample file if we see there are lot of headers and trailers and some data between them. Our requirement is to split each set of data with HEADER, TRAILER and DETAIL DATA into individual files. For our sample it should generate 6 different files.
  • We will split file using key values in the file. Here we use positions 11-15 (5 characters) in DETAIL DATA and positions 8-12 in HEADER and TRAILER data. We must maintain the consistency of the data as Header row, Details rows and Trailer row.
  • This makes the complete structure of file and keeps all data together.

Now we write Pig Script to split the file -

REGISTER /home/jars/pig/piggybank.jar;

A  = LOAD  '/path/to/input/file/TEST.DEV.ENV.SAMPLE.FILE'
     USING PigStorage('\t') AS (line:chararray);

B  = FILTER A BY SUBSTRING(line, 1, 7) != 'HEADER' AND SUBSTRING(line, 0, 7) != '9TRAILR';

C  = FILTER A BY SUBSTRING(line, 1, 7) == 'HEADER' OR SUBSTRING(line, 0, 7) == '9TRAILR';

-- Extract data based on key value from Header, Details and Trailer rows 
D  = GROUP B BY SUBSTRING($0, 10, 15);
E  = GROUP C BY SUBSTRING($0, 7, 12);

F  = UNION D, E;


SPLIT G INTO H IF SIZE($0) > 0, X IF SIZE($0) <= 0;

J  = ORDER H BY $1;

STORE J INTO '/path/to/output/directory'
        -- Stores using \t as the input separator 
        USING org.apache.pig.piggybank.storage.MultiStorage('/path/to/output/directory', '0');
  • File is split on the value we taken as key value. Here our key value from all rows (Header, Details and Trailer) is 5 characters specified using function substring() at transformtions ‘D’ and ‘E’.

Now we will see output directory for files -

pig split string example

Learn Apache pig - Apache pig tutorial - Pig split string example - Apache pig examples - Apache pig programs

pig substring

Learn Apache pig - Apache pig tutorial - Pig Substring - Apache pig examples - Apache pig programs

Split in Pig

Learn Apache pig - Apache pig tutorial - Split in pig - Apache pig examples - Apache pig programs

Related Searches to Splitting input into substrings in PIG (Hadoop)

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.