Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f8b1df

Browse files
author
Jonathan Chang
committed
Initial migration from facebook-hive-udfs
1 parent af1b9bd commit 2f8b1df

File tree

10 files changed

+572
-0
lines changed

10 files changed

+572
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.airbnb</groupId>
7+
<artifactId>facebook-udfs</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<name>Archetype - facebook-udfs</name>
10+
<url>http://maven.apache.org</url>
11+
12+
<repositories>
13+
<repository>
14+
<id>Cloudera</id>
15+
<name>Cloudera Maven Repo</name>
16+
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
17+
</repository>
18+
</repositories>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.hive</groupId>
22+
<artifactId>hive-exec</artifactId>
23+
<version>0.10.0-cdh4.3.0</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.hadoop</groupId>
28+
<artifactId>hadoop-core</artifactId>
29+
<version>2.0.0-mr1-cdh4.3.0</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.hadoop</groupId>
34+
<artifactId>hadoop-common</artifactId>
35+
<version>2.0.0-cdh4.3.0</version>
36+
<scope>provided</scope>
37+
</dependency>
38+
</dependencies>
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-dependency-plugin</artifactId>
44+
<dependencies>
45+
<!-- Workaround for MDEP-138 -->
46+
<dependency>
47+
<groupId>org.codehaus.plexus</groupId>
48+
<artifactId>plexus-utils</artifactId>
49+
<version>1.5.1</version>
50+
<scope>runtime</scope>
51+
</dependency>
52+
</dependencies>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-compiler-plugin</artifactId>
57+
<version>2.5.1</version>
58+
<configuration>
59+
<source>1.6</source>
60+
<target>1.6</target>
61+
<encoding>UTF-8</encoding>
62+
<maxmem>1024m</maxmem>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-assembly-plugin</artifactId>
68+
<version>2.2-beta-2</version>
69+
<configuration>
70+
<descriptorRefs>
71+
<descriptorRef>jar-with-dependencies</descriptorRef>
72+
</descriptorRefs>
73+
<appendAssemblyId>false</appendAssemblyId>
74+
<finalName>${project.artifactId}-all</finalName>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
81+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.facebook.hive.udf.string;
20+
21+
import org.apache.hadoop.hive.ql.exec.UDF;
22+
import org.apache.hadoop.hive.ql.exec.Description;
23+
24+
import com.facebook.hive.udf.tests.HiveUnitTests;
25+
import com.facebook.hive.udf.tests.HiveUnitTest;
26+
27+
/**
28+
* Returns true when the haystack string (first argument) ends with the
29+
* needle string (second argument). If either argument is NULL then NULL is
30+
* returned.
31+
*
32+
* @author jonchang
33+
*/
34+
@Description(name = "ends_with",
35+
value = "_FUNC_(haystack, needle) - Return whether " +
36+
"haystack ends with needle.")
37+
@HiveUnitTests(tests = {
38+
@HiveUnitTest(query = "SELECT FB_ENDS_WITH('wham!', 'am!'), "+
39+
"FB_STARTS_WITH('wham!', 'am') FROM dim_one_row",
40+
result = "true\tfalse")
41+
})
42+
public class UDFEndsWith extends UDF {
43+
public Boolean evaluate(String haystack, String needle) {
44+
if (haystack == null || needle == null) {
45+
return null;
46+
}
47+
return haystack.endsWith(needle);
48+
}
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.facebook.hive.udf.string;
20+
21+
import org.apache.hadoop.hive.ql.exec.UDF;
22+
import org.apache.hadoop.hive.ql.exec.Description;
23+
24+
import com.facebook.hive.udf.tests.HiveUnitTests;
25+
import com.facebook.hive.udf.tests.HiveUnitTest;
26+
27+
/**
28+
* Returns the index where the needle string (first argument) occurs in the
29+
* haystack string (second argument). The index begins at 0. If the string
30+
* is not found then -1 is returned. If either argument is NULL then NULL
31+
* is returned.
32+
*
33+
* @author jonchang
34+
*/
35+
@Description(name = "find_in_string",
36+
value = "_FUNC_(needle, haystack) - Return the index at which " +
37+
"needle appears in haystack.")
38+
@HiveUnitTests(tests = {
39+
@HiveUnitTest(
40+
query = "SELECT FB_FIND_IN_STRING('face', 'facebook'), " +
41+
"FB_FIND_IN_STRING('k00b', 'facebook'), " +
42+
"FB_FIND_IN_STRING('book', 'facebook') FROM dim_one_row",
43+
result = "0\t-1\t4")
44+
})
45+
public class UDFFindInString extends UDF {
46+
public Integer evaluate(String needle, String haystack) {
47+
if (haystack == null || needle == null) {
48+
return null;
49+
}
50+
return haystack.indexOf(needle);
51+
}
52+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.facebook.hive.udf.string;
20+
21+
import org.apache.hadoop.hive.ql.exec.UDF;
22+
import org.apache.hadoop.hive.ql.exec.Description;
23+
24+
import com.facebook.hive.udf.tests.HiveUnitTests;
25+
import com.facebook.hive.udf.tests.HiveUnitTest;
26+
27+
/**
28+
* Returns the string s (first argument) with the characters
29+
* in the string chars (second argument) trimmed off the left
30+
* side.
31+
*
32+
* @author atfiore
33+
*/
34+
@Description(name = "udfltrim",
35+
value = "_FUNC_(s, chars) - Return the string s " +
36+
"with the characters in chars trimmed off " +
37+
"the left side.")
38+
@HiveUnitTests(tests = {
39+
@HiveUnitTest(query = "SELECT FB_LTRIM(\"aabbccXaabbccddeeee\", \"abcde\") " +
40+
"from dim_one_row;",
41+
result = "Xaabbccddeeee"),
42+
@HiveUnitTest(query = "SELECT FB_LTRIM(\"aabbccddee\", \"xy\") " +
43+
"from dim_one_row;",
44+
result = "aabbccddee"),
45+
@HiveUnitTest(query = "SELECT FB_LTRIM(\"\", \"abcde\") " +
46+
"from dim_one_row;",
47+
result = ""),
48+
@HiveUnitTest(query = "SELECT FB_LTRIM(\"a\", \"a\") " +
49+
"from dim_one_row;",
50+
result = ""),
51+
@HiveUnitTest(query = "SELECT FB_LTRIM(\"aabbccddee\", \"abcde\") " +
52+
"from dim_one_row;",
53+
result = "")
54+
})
55+
56+
public class UDFLtrim extends UDF {
57+
public String evaluate(String s, String chars) {
58+
if (s == null) {
59+
return null;
60+
}
61+
if (chars == null) {
62+
return s;
63+
}
64+
int i = 0;
65+
boolean done = false;
66+
while (i < s.length() && !done) {
67+
if (chars.indexOf(s.charAt(i)) == -1) {
68+
done = true;
69+
} else {
70+
i++;
71+
}
72+
}
73+
int j = s.length() - 1;
74+
75+
if (i > j) {
76+
return new String();
77+
}
78+
79+
// substring(begin, end) takes chars from s[begin] to s[end-1]
80+
return s.substring(i, j + 1);
81+
}
82+
83+
public String evaluate(String s) {
84+
return this.evaluate(s, " \n\t");
85+
}
86+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.facebook.hive.udf.string;
20+
21+
import org.apache.hadoop.hive.ql.exec.UDF;
22+
import org.apache.hadoop.hive.ql.exec.Description;
23+
24+
import com.facebook.hive.udf.tests.HiveUnitTests;
25+
import com.facebook.hive.udf.tests.HiveUnitTest;
26+
27+
/**
28+
* Returns the string s (first argument) with the characters
29+
* in the string chars (second argument) trimmed off the right
30+
* side.
31+
*
32+
* @author atfiore
33+
*/
34+
@Description(name = "udfrtrim",
35+
value = "_FUNC_(s, chars) - Return the string s " +
36+
"with the characters in chars trimmed off " +
37+
"the right.")
38+
@HiveUnitTests(tests = {
39+
@HiveUnitTest(query = "SELECT FB_RTRIM(\"aaXaabbccddeeee\", \"abcde\") " +
40+
"from dim_one_row;",
41+
result = "aaX"),
42+
@HiveUnitTest(query = "SELECT FB_RTRIM(\"aabbccddee\", \"xy\") " +
43+
"from dim_one_row;",
44+
result = "aabbccddee"),
45+
@HiveUnitTest(query = "SELECT FB_RTRIM(\"\", \"abcde\") " +
46+
"from dim_one_row;",
47+
result = ""),
48+
@HiveUnitTest(query = "SELECT FB_RTRIM(\"a\", \"a\") " +
49+
"from dim_one_row;",
50+
result = ""),
51+
@HiveUnitTest(query = "SELECT FB_RTRIM(\"aabbccddee\", \"abcde\") " +
52+
"from dim_one_row;",
53+
result = "")
54+
})
55+
56+
public class UDFRtrim extends UDF {
57+
public String evaluate(String s, String chars) {
58+
if (s == null) {
59+
return null;
60+
}
61+
if (chars == null) {
62+
return s;
63+
}
64+
int i = 0;
65+
66+
int j = s.length() - 1;
67+
boolean done = false;
68+
while (j >= i && !done) {
69+
if (chars.indexOf(s.charAt(j)) == -1) {
70+
done = true;
71+
} else {
72+
j--;
73+
}
74+
}
75+
76+
if (j < i) {
77+
return new String();
78+
}
79+
80+
// substring(begin, end) takes chars from s[begin] to s[end-1]
81+
return s.substring(i, j + 1);
82+
}
83+
84+
public String evaluate(String s) {
85+
return this.evaluate(s, " \n\t");
86+
}
87+
}

0 commit comments

Comments
 (0)