2

I need to strip out the third and subsequent values in the 'bracketed' component of the user agent string.

In order to get

Mozilla/4.0 (compatible; MSIE 8.0)

from

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; WinTSI 06.12.2009; .NET CLR 3.0.30729; .NET4.0C)

I successfully use sed command

 sed 's/(\([^;]\+; [^;]\+\)[^)]*)/(\1)/'

I need to get the same result in Apache Pig with a Java regex. Could anybody help me to re-write the above sed regular expression into Java?

Something like:

new = FOREACH userAgent GENERATE FLATTEN(EXTRACT(userAgent, 'JAVA REGEX?') as (term:chararray);

3 Answers 3

2

I don't use Pig, but a look through the docs reveals a REPLACE function which wraps Java's replaceAll() method. Try this:

REPLACE(userAgent, '\(([^;]+; [^;]+)[^)]*\)', '($1)')

That should match the whole parenthesized portion of the UserAgent string and replace its contents with just the first two semicolon-separated terms, just like your sed command does.

8
  • Thank you. But I was not able to get this user_agent = FOREACH logs_filtered5 GENERATE FLATTEN(EXTRACT(userAgent,'\(([^;]+; [^;]+)[^)]*\)')) as (user_agent); working, I always got ERROR 1000: Error during parsing. Lexical error at line 36, column 74. Encountered: "(" (40), after : "\'\\". Maybe this is something specific to PIG.
    – Martin
    Nov 23, 2011 at 11:29
  • Maybe it expects backslashes to be escaped, as in Java string literals. Try '\\(([^;]+; [^;]+)[^)]*\\)'
    – Alan Moore
    Nov 23, 2011 at 11:50
  • I tried that too, but it didn't help ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: Error during parsing. Lexical error at line 36, column 80. Encountered: <EOF> after : "\'\\\\(([^;". Do you think that this can be related to PIG in interactive mode?
    – Martin
    Nov 23, 2011 at 12:00
  • I don't know. I have no experience with Pig, and I can't find any useful examples. I do notice, however, that the code in your first comment doesn't call the REPLACE function. Try replacing your EXTRACT call with what I posted.
    – Alan Moore
    Nov 23, 2011 at 12:26
  • 2
    You can use Pig's ability to interpret unicode to get around this: \u003B maps to a semicolon.
    – SquareCog
    Dec 4, 2011 at 20:00
1

In java if you use the Matcher class you can extract the capturing group. The following appears to do what you want, at least for the test case you provided.

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

    public static void main(String[] args){
        String str = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; WinTSI 06.12.2009; .NET CLR 3.0.30729; .NET4.0C)";
        //str = "aaa";
        Pattern pat = Pattern.compile("(.*\\(.*?;.*?;).*\\)");
        Matcher m = pat.matcher(str);
        System.out.println(m.lookingAt());
        String group = m.group(1) + ")";
        System.out.println(group);
    }
 }

Hmm... I seemed to have answered the wrong question, since you were asking how to do this from 'PIG' not straight JAVA.

1
  • Sorry, I don't know how to use your code in PIG to test it. Thank you anyway.
    – Martin
    Nov 23, 2011 at 11:41
0

As none of two suggested solutions seems to work in PIG I will post workaround which uses sed through stream:

user_agent_mangled = STREAM logs THROUGH `sed 's/(\\([^;]\\+; [^;]\\+\\)[^)]*)/(\\1)/'`;

This works well, however I would still prefer native PIG solution (using EXTRACT or REPLACE function).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.