Apache Pig string Interception in combat small example

Source: Internet
Author: User

A small example of how to record a pig string interception:

The requirement is as follows to extract the value of column 2nd (after the colon) from the following string:

Java code

1 2 3 4a:ab#c#da:c#c#da:dd#c#da:zz#c#d



If it is in Java, the method may have many kinds, such as substring, or split several times, and so on in pig, you can use the substring built-in functions to complete, but it is recommended to use the following way, this method is more flexible, suitable for most scenes of data extraction, The 2 functions that need to be used are as follows:

(1) regex_extract (' Primitive string ', ' regular ', return index of type int)

(2) Strsplit (' Primitive string ', ' regular ', limit number of returns)


Pig script notation:

Java code

A = Laod '/tmp/data ' as (Data:chararray)//Note that the logic is to first get the data after the colon, then split into a tuple, and then through the (subscript access Element) to get the data we need. b = foreach a Generate Strsplit (regex_extract (Data, ' (. *):(. *) ', 2), ' # ', 5). $0;dump b;


Apache Pig string Interception in combat small example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.