Skip to content

Instantly share code, notes, and snippets.

@suisho
Created November 14, 2012 04:36
Show Gist options
  • Save suisho/4070280 to your computer and use it in GitHub Desktop.
Save suisho/4070280 to your computer and use it in GitHub Desktop.
Detect browser from user agent in hadoop pig UDF
package pigudf;
import java.io.IOException;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
/**
* Usage:
* log = LOAD logfile.txt AS (time:int, path:chararray, ua:chararray)
* log = FOREACH log GENERATE time, path, ua, pigudf(ua) AS browser;
*/
public class Browser extends EvalFunc<String> {
@Override
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0) {
return "None";
}
String uaString = (String) input.get(0);
if (uaString.matches(".*IEMobile.*")) {
return "IEMobile";
} else if (uaString.matches(".*MSIE 6.*")) {
return "IE 6";
} else if (uaString.matches(".*MSIE 7.*")) {
return "IE 7";
} else if (uaString.matches(".*MSIE 8.*")) {
return "IE 8";
} else if (uaString.matches(".*MSIE 9.*")) {
return "IE 9";
} else if (uaString.matches(".*Firefox.*")) {
return "Firefox";
} else if (uaString.matches(".*Chrome.*")) {
return "Chrome";
} else if (uaString.matches(".*Safari.*")) {
return "Safari";
}
return "Unknown";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment