public String evaluate(String urlStr, String partToExtract) {
    if (urlStr == null || partToExtract == null) {
      return null;
    }

    if (lastUrlStr == null || !urlStr.equals(lastUrlStr)) {
      try {
        url = new URL(urlStr);
      } catch (Exception e) {
        return null;
      }
    }
    lastUrlStr = urlStr;

    if (partToExtract.equals("HOST")) {
      return url.getHost();
    }
    if (partToExtract.equals("PATH")) {
      return url.getPath();
    }
    if (partToExtract.equals("QUERY")) {
      return url.getQuery();
    }
    if (partToExtract.equals("REF")) {
      return url.getRef();
    }
    if (partToExtract.equals("PROTOCOL")) {
      return url.getProtocol();
    }
    if (partToExtract.equals("FILE")) {
      return url.getFile();
    }
    if (partToExtract.equals("AUTHORITY")) {
      return url.getAuthority();
    }
    if (partToExtract.equals("USERINFO")) {
      return url.getUserInfo();
    }

    return null;
  }
UDF to extract specfic parts from URL For example,
parse_url('http://facebook.com/path/p1.php?query=1', 'HOST') will return
'facebook.com' For example,
parse_url('http://facebook.com/path/p1.php?query=1', 'PATH') will return
'/path/p1.php' parse_url('http://facebook.com/path/p1.php?query=1', 'QUERY')
will return 'query=1'
parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'REF') will return
'Ref' parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'PROTOCOL')
will return 'http' Possible values are
HOST,PATH,QUERY,REF,PROTOCOL,AUTHORITY,FILE,USERINFO Also you can get a value
of particular key in QUERY, using syntax QUERY:&lt;KEY_NAME&gt; eg: QUERY:k1.
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFParseUrl.java