Author: kamrul
Date: Thu Nov 1 00:40:57 2012
New Revision: 1404434
URL: http://svn.apache.org/viewvc?rev=1404434&view=rev
Log:
OOZIE-1036 Utility class to parse HCat URI (Ryota via Mohammad)
Added:
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/util/HCatURI.java
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/util/TestHCatURI.java
Added:
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/util/HCatURI.java
URL:
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/util/HCatURI.java?rev=1404434&view=auto
==============================================================================
---
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/util/HCatURI.java
(added)
+++
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/util/HCatURI.java
Thu Nov 1 00:40:57 2012
@@ -0,0 +1,180 @@
+/**
+ * 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.
+ */
+package org.apache.oozie.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
+
+public class HCatURI {
+
+ public static final String PREFIX_HCAT =
"oozie.service.MetaAccessorService.hcat";
+ public static final String DEFAULT_SERVER = PREFIX_HCAT + ".server";
+ public static final String DEFAULT_DB = PREFIX_HCAT + ".db";
+ public static final String DEFAULT_TABLE = PREFIX_HCAT + ".table";
+ public static final String PARTITION_SEPARATOR = ";";
+ public static final String PARTITION_KEYVAL_SEPARATOR = "=";
+ public static final String PATH_SEPARATOR = "/";
+
+ private URI uri;
+ private String server;
+ private String db;
+ private String table;
+ private HashMap<String, String> partitions;
+
+ public HCatURI(String s, Configuration conf) throws URISyntaxException {
+ parse(s, conf);
+ }
+
+ public HCatURI(String s) throws URISyntaxException {
+ this(s, null);
+ }
+
+ private void parse(String s, Configuration conf) throws URISyntaxException
{
+
+ uri = new URI(s);
+
+ server = getValidConf(uri.getAuthority(), conf, DEFAULT_SERVER);
+ if (server == null) {
+ throw new URISyntaxException(uri.toString(), "HCat Server Name is
missing");
+ }
+
+ String[] paths = uri.getPath().split(PATH_SEPARATOR, 4);
+
+ if (paths.length != 4) {
+ throw new URISyntaxException(uri.toString(), "DB and Table names
are not specified properly");
+ }
+
+ db = getValidConf(paths[1], conf, DEFAULT_DB);
+ if (db == null) {
+ throw new URISyntaxException(uri.toString(), "DB name is missing");
+ }
+
+ table = getValidConf(paths[2], conf, DEFAULT_TABLE);
+ if (table == null) {
+ throw new URISyntaxException(uri.toString(), "Table name is
missing");
+ }
+
+ partitions = new HashMap<String, String>();
+ String partRaw = uri.getQuery();
+ if (partRaw == null || partRaw.length() == 0) {
+ throw new URISyntaxException(uri.toString(), "Partition name is
missing");
+ }
+
+ String[] parts = partRaw.split(PARTITION_SEPARATOR, -1);
+ for (String part : parts) {
+ if (part == null || part.length() == 0) {
+ continue;
+ }
+ String[] keyVal = part.split(PARTITION_KEYVAL_SEPARATOR, -1);
+ if (keyVal.length != 2) {
+ throw new URISyntaxException(uri.toString(), "Parition key
value pair is not specified properly in ("
+ + part + ")");
+ }
+ partitions.put(keyVal[0], keyVal[1]);
+ }
+ }
+
+ private String getValidConf(String a, Configuration conf, String key) {
+ if (a == null || a.length() == 0) {
+ if (conf != null) {
+ return conf.get(key);
+ }
+ else {
+ return null;
+ }
+ }
+ else {
+ return a;
+ }
+ }
+
+ public String getServer() {
+ return server;
+ }
+
+ public void setServer(String server) {
+ this.server = server;
+ }
+
+ public String getDb() {
+ return db;
+ }
+
+ public void setDb(String db) {
+ this.db = db;
+ }
+
+ public String getTable() {
+ return table;
+ }
+
+ public void setTable(String table) {
+ this.table = table;
+ }
+
+ public HashMap<String, String> getPartitionMap() {
+ return partitions;
+ }
+
+ public void setPartitionMap(HashMap<String, String> partitions) {
+ this.partitions = partitions;
+ }
+
+ public String getParitionValue(String key) {
+ return partitions.get(key);
+ }
+
+ public String setParition(String key, String value) {
+ return partitions.put(key, value);
+ }
+
+ public boolean hasPartition(String key) {
+ return partitions.containsKey(key);
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("URI: ");
+ sb.append(uri.toString());
+ sb.append("\n");
+ sb.append("SCHEME: ");
+ sb.append(uri.getScheme());
+ sb.append("\n");
+ sb.append("SERVER: ");
+ sb.append(getServer());
+ sb.append("\n");
+ sb.append("DB: ");
+ sb.append(getDb());
+ sb.append("\n");
+ sb.append("TABLE: ");
+ sb.append(getTable());
+ int partcnt = 0;
+ for (Map.Entry<String, String> entry : partitions.entrySet()) {
+ sb.append("\n");
+ sb.append("PARTITION(" + partcnt + "): ");
+ sb.append(entry.getKey());
+ sb.append("=");
+ sb.append(entry.getValue());
+ partcnt++;
+ }
+ return sb.toString();
+ }
+}
Added:
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/util/TestHCatURI.java
URL:
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/util/TestHCatURI.java?rev=1404434&view=auto
==============================================================================
---
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/util/TestHCatURI.java
(added)
+++
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/util/TestHCatURI.java
Thu Nov 1 00:40:57 2012
@@ -0,0 +1,163 @@
+/**
+ * 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.
+ */
+package org.apache.oozie.util;
+
+import static org.junit.Assert.*;
+import java.net.URISyntaxException;
+import org.junit.Test;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.oozie.util.HCatURI;
+
+public class TestHCatURI {
+
+ @Test
+ public void testHCatURIParseValidURI() {
+ String input =
"hcat://hcat.yahoo.com:5080/mydb/clicks/?datastamp=12;region=us";
+ HCatURI uri = null;
+ try{
+ uri= new HCatURI(input);
+ }catch (Exception ex){
+ System.err.print(ex.getMessage());
+ }
+ assertEquals(uri.getServer(),"hcat.yahoo.com:5080");
+ assertEquals(uri.getDb(),"mydb");
+ assertEquals(uri.getTable(),"clicks");
+ assertEquals(uri.getParitionValue("datastamp"),"12");
+ assertEquals(uri.getParitionValue("region"),"us");
+
+ }
+
+ @Test
+ public void testHCatURIParseWithDefaultServer() {
+
+ String input = "hcat:///mydb/clicks/?datastamp=12;region=us";
+ Configuration conf = new Configuration(false);
+ conf.set("oozie.service.MetaAccessorService.hcat.server",
"hcat.yahoo.com:5080");
+ conf.set("oozie.service.MetaAccessorService.hcat.db", "mydb");
+ conf.set("oozie.service.MetaAccessorService.hcat.table", "clicks");
+
+ HCatURI uri = null;
+ try{
+ uri= new HCatURI(input,conf);
+ }catch (Exception ex){
+ System.err.println(ex.getMessage());
+ }
+ assertEquals(uri.getServer(),"hcat.yahoo.com:5080");
+ assertEquals(uri.getDb(),"mydb");
+ assertEquals(uri.getTable(),"clicks");
+ assertEquals(uri.getParitionValue("datastamp"),"12");
+ assertEquals(uri.getParitionValue("region"),"us");
+ }
+
+ @Test
+ public void testHCatURIParseWithDefaultDB() {
+
+ String input =
"hcat://hcat.yahoo.com:5080//clicks/?datastamp=12;region=us";
+ Configuration conf = new Configuration(false);
+ conf.set("oozie.service.MetaAccessorService.hcat.server",
"hcat.yahoo.com:5080");
+ conf.set("oozie.service.MetaAccessorService.hcat.db", "mydb");
+ conf.set("oozie.service.MetaAccessorService.hcat.table", "clicks");
+
+ HCatURI uri = null;
+ try{
+ uri= new HCatURI(input,conf);
+ }catch (Exception ex){
+ System.err.println(ex.getMessage());
+ }
+ assertEquals(uri.getServer(),"hcat.yahoo.com:5080");
+ assertEquals(uri.getDb(),"mydb");
+ assertEquals(uri.getTable(),"clicks");
+ assertEquals(uri.getParitionValue("datastamp"),"12");
+ assertEquals(uri.getParitionValue("region"),"us");
+ }
+
+ @Test
+ public void testHCatURIParseWithDefaultTable() {
+
+ String input =
"hcat://hcat.yahoo.com:5080/mydb//?datastamp=12;region=us";
+ Configuration conf = new Configuration(false);
+ conf.set("oozie.service.MetaAccessorService.hcat.server",
"hcat.yahoo.com:5080");
+ conf.set("oozie.service.MetaAccessorService.hcat.db", "mydb");
+ conf.set("oozie.service.MetaAccessorService.hcat.table", "clicks");
+
+ HCatURI uri = null;
+ try{
+ uri= new HCatURI(input,conf);
+ }catch (Exception ex){
+ System.err.println(ex.getMessage());
+ }
+ assertEquals(uri.getServer(),"hcat.yahoo.com:5080");
+ assertEquals(uri.getDb(),"mydb");
+ assertEquals(uri.getTable(),"clicks");
+ assertEquals(uri.getParitionValue("datastamp"),"12");
+ assertEquals(uri.getParitionValue("region"),"us");
+ }
+
+
+ @Test
+ public void testHCatURIParseWithAllDefault() {
+
+ String input = "hcat://///?datastamp=12;region=us";
+ Configuration conf = new Configuration(false);
+ conf.set("oozie.service.MetaAccessorService.hcat.server",
"hcat.yahoo.com:5080");
+ conf.set("oozie.service.MetaAccessorService.hcat.db", "mydb");
+ conf.set("oozie.service.MetaAccessorService.hcat.table", "clicks");
+
+ HCatURI uri = null;
+ try{
+ uri= new HCatURI(input,conf);
+ }catch (Exception ex){
+ System.err.println(ex.getMessage());
+ }
+ assertEquals(uri.getServer(),"hcat.yahoo.com:5080");
+ assertEquals(uri.getDb(),"mydb");
+ assertEquals(uri.getTable(),"clicks");
+ assertEquals(uri.getParitionValue("datastamp"),"12");
+ assertEquals(uri.getParitionValue("region"),"us");
+ }
+
+ @Test(expected = URISyntaxException.class)
+ public void testHCatURIParseInvalidURI() throws Exception{
+ String input = "hcat://hcat.yahoo.com:5080/
mydb/clicks/?datastamp=12;region=us";
+ HCatURI uri = new HCatURI(input);
+ }
+
+ @Test(expected = URISyntaxException.class)
+ public void testHCatURIParseInvalidPartition() throws Exception{
+ String input = "hcat://hcat.yahoo.com:5080/mydb/clicks/?datastamp";
+ HCatURI uri = new HCatURI(input);
+ }
+
+ @Test(expected = URISyntaxException.class)
+ public void testHCatURIParseServerMissing() throws Exception{
+ String input = "hcat:///mydb/clicks/?datastamp=12;region=us";
+ HCatURI uri = new HCatURI(input);
+ }
+
+ @Test(expected = URISyntaxException.class)
+ public void testHCatURIParseDBMissing() throws Exception{
+ String input =
"hcat://hcat.yahoo.com:5080//clicks/?datastamp=12;region=us";
+ HCatURI uri = new HCatURI(input);
+ }
+
+ @Test(expected = URISyntaxException.class)
+ public void testHCatURIParseTableMissing() throws Exception{
+ String input =
"hcat://hcat.yahoo.com:5080/mydb//?datastamp=12;region=us";
+ HCatURI uri = new HCatURI(input);
+ }
+}