1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mobvista.dmp.common;
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hadoop.hive.ql.exec.Description;
/**
* 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:<KEY_NAME> eg: QUERY:k1.
*/
@Description(name = "parse_url",
value = "_FUNC_(url, partToExtract[, key]) - extracts a part from a URL",
extended = "Parts: HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, "
+ "USERINFO\nkey specifies which query to extract\n"
+ "Example:\n"
+ " > SELECT _FUNC_('http://facebook.com/path/p1.php?query=1', "
+ "'HOST') FROM src LIMIT 1;\n"
+ " 'facebook.com'\n"
+ " > SELECT _FUNC_('http://facebook.com/path/p1.php?query=1', "
+ "'QUERY') FROM src LIMIT 1;\n"
+ " 'query=1'\n"
+ " > SELECT _FUNC_('http://facebook.com/path/p1.php?query=1', "
+ "'QUERY', 'query') FROM src LIMIT 1;\n" + " '1'")
public class UrlParser implements Serializable {
private String lastUrlStr = null;
private URL url = null;
private Pattern p = null;
private String lastKey = null;
public UrlParser() {
}
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;
}
public String evaluate(String urlStr, String partToExtract, String key) {
if (!partToExtract.equals("QUERY")) {
return null;
}
String query = this.evaluate(urlStr, partToExtract);
if (query == null) {
return null;
}
if (!key.equals(lastKey)) {
p = Pattern.compile("(&|^)" + key + "=([^&]*)");
}
lastKey = key;
Matcher m = p.matcher(query);
if (m.find()) {
return m.group(2);
}
return null;
}
public static void main(String[] args) {
UrlParser u = new UrlParser();
String a = u.evaluate("http://www.baidu.c/sdfsf/?avc=sds", "PATH", "avc");
System.out.println(a);
System.out.println("http://www.baidu.c/sdfsf/?avc=sds".split("\\?", -1)[0]);
}
}