incubator-hcatalog-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tof...@apache.org
Subject svn commit: r1371533 [1/2] - in /incubator/hcatalog/trunk: ./ webhcat/java-client/ webhcat/java-client/src/ webhcat/java-client/src/main/ webhcat/java-client/src/main/java/ webhcat/java-client/src/main/java/org/ webhcat/java-client/src/main/java/org/ap...
Date Fri, 10 Aug 2012 00:17:00 GMT
Author: toffer
Date: Fri Aug 10 00:16:59 2012
New Revision: 1371533

URL: http://svn.apache.org/viewvc?rev=1371533&view=rev
Log:
HCATALOG-419 Java APIs for HCatalog DDL commands (avandana via fcliu)

Added:
    incubator/hcatalog/trunk/webhcat/java-client/
    incubator/hcatalog/trunk/webhcat/java-client/build.xml
    incubator/hcatalog/trunk/webhcat/java-client/ivy.xml
    incubator/hcatalog/trunk/webhcat/java-client/src/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatAddPartitionDesc.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClient.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateDBDesc.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateTableDesc.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatDatabase.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatPartition.java
    incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatTable.java
    incubator/hcatalog/trunk/webhcat/java-client/src/test/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/org/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/org/apache/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/org/apache/hcatalog/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/org/apache/hcatalog/api/
    incubator/hcatalog/trunk/webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java
Modified:
    incubator/hcatalog/trunk/CHANGES.txt
    incubator/hcatalog/trunk/build.xml

Modified: incubator/hcatalog/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/CHANGES.txt?rev=1371533&r1=1371532&r2=1371533&view=diff
==============================================================================
--- incubator/hcatalog/trunk/CHANGES.txt (original)
+++ incubator/hcatalog/trunk/CHANGES.txt Fri Aug 10 00:16:59 2012
@@ -23,6 +23,8 @@ Trunk (unreleased changes)
   INCOMPATIBLE CHANGES
 
   NEW FEATURES
+  HCAT-419 Java APIs for HCatalog DDL commands (avandana via fcliu)
+
   HCAT-182 Web services interface for HCatalog access and Pig, Hive, and MR job execution (ctdean, Rachel Gollub, and thejas via gates)
 
   HCAT-328 HCatLoader should report its input size so pig can estimate the number of reducers (traviscrawford via gates)

Modified: incubator/hcatalog/trunk/build.xml
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/build.xml?rev=1371533&r1=1371532&r2=1371533&view=diff
==============================================================================
--- incubator/hcatalog/trunk/build.xml (original)
+++ incubator/hcatalog/trunk/build.xml Fri Aug 10 00:16:59 2012
@@ -200,6 +200,7 @@
   <target name="jar" depends="clientjar,server-extensions,jar-storage-handlers">
     <ant target="jar" dir="hcatalog-pig-adapter" inheritAll="false"/>
     <ant target="jar" dir="webhcat/svr" inheritAll="false"/>
+    <ant target="jar" dir="webhcat/java-client" inheritAll="false"/>
 
     <!-- Build hcatalog.jar, bundling the pig adapter. Our intention is to stop producing this
          fat jar after some migration period. -->
@@ -247,6 +248,7 @@
     <_junit srcDir="${basedir}/src/test"/>
     <ant target="test" dir="hcatalog-pig-adapter" inheritAll="false"/>
     <ant target="test" dir="webhcat/svr" inheritAll="false"/>
+    <ant target="test" dir="webhcat/java-client" inheritAll="false"/>
     <ant target="test" dir="storage-handlers" inheritAll="false" useNativeBasedir="true"/>
   </target>
 
@@ -255,6 +257,7 @@
     <_junit srcDir="${basedir}/src/test"/>
     <ant target="_test-with-clover" dir="hcatalog-pig-adapter" inheritAll="false"/>
     <ant target="_test-with-clover" dir="webhcat/svr" inheritAll="false"/>
+    <ant target="_test-with-clover" dir="webhcat/java-client" inheritAll="false"/>
     <!-- storage-handlers do not have coverage as they have not
          yet been migrated to the new build files. -->
     <ant target="test" dir="storage-handlers" inheritAll="false" useNativeBasedir="true"/>
@@ -286,6 +289,7 @@
 
     <ant target="findbugs" dir="hcatalog-pig-adapter" inheritAll="false"/>
     <ant target="findbugs" dir="webhcat/svr" inheritAll="false"/>
+    <ant target="findbugs" dir="webhcat/java-client" inheritAll="false"/>
   </target>
 
   <!--
@@ -300,6 +304,7 @@
     <delete dir="${test.warehouse.dir}"/>
     <ant target="clean" dir="hcatalog-pig-adapter" inheritAll="false"/>
     <ant target="clean" dir="webhcat/svr" inheritAll="false"/>
+    <ant target="clean" dir="webhcat/java-client" inheritAll="false"/>
     <ant target="clean" dir="storage-handlers" inheritAll="false" useNativeBasedir="true"/>
   </target>
  
@@ -337,6 +342,7 @@
       <packageset dir="${src.dir}" />
       <packageset dir="hcatalog-pig-adapter/src/main/java" />
       <packageset dir="webhcat/svr/src/main/java" />
+      <packageset dir="webhcat/java-client/src/main/java" />
       <classpath>
         <path refid="uber.classpath"/>
       </classpath>
@@ -385,6 +391,9 @@
       <fileset dir="webhcat/svr/build">
         <include name="webhcat-*.jar"/>
       </fileset>
+      <fileset dir="webhcat/java-client/build">
+        <include name="webhcat-java-client*.jar"/>
+      </fileset>
     </copy>
 
     <copy todir="${dist.dir}/bin">

Added: incubator/hcatalog/trunk/webhcat/java-client/build.xml
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/build.xml?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/build.xml (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/build.xml Fri Aug 10 00:16:59 2012
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+   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.
+-->
+
+<project name="webhcat-java-client">
+  <property name="path.to.basedir" location="${basedir}/../.."/>
+
+  <import file="../../build-common.xml"/>
+  <path id="compile.class.path">
+    <fileset dir="${build.dir}/ivy/lib/default" includes="*.jar"/>
+  </path>
+
+  <path id="test.class.path">
+    <fileset dir="${build.dir}/ivy/lib/test" includes="*.jar"/>
+    <dirset dir="${path.to.basedir}/build/test/classes"/>
+    <dirset dir="${basedir}/build/classes"/>
+    <dirset dir="${basedir}/build/test/classes"/>
+    <dirset dir="../../build/classes"/>
+  </path>
+
+  <path id="findbugs.class.path">
+    <fileset dir="${build.dir}/ivy/lib/default" includes="*.jar"/>
+  </path>
+
+</project>

Added: incubator/hcatalog/trunk/webhcat/java-client/ivy.xml
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/ivy.xml?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/ivy.xml (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/ivy.xml Fri Aug 10 00:16:59 2012
@@ -0,0 +1,40 @@
+<!-- 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. -->
+
+<ivy-module version="2.0">
+  <info organisation="org.apache.hcatalog"
+        module="${ant.project.name}"
+        revision="${hcatalog.version}">
+    <license name="Apache 2.0"/>
+    <ivyauthor name="Apache HCatalog Team" url="http://incubator.apache.org/hcatalog"/>
+    <description>Apache HCatalog</description>
+  </info>
+
+  <configurations defaultconfmapping="default">
+    <conf name="default"/>
+    <conf name="test" extends="default"/>
+  </configurations>
+
+  <publications>
+    <artifact name="webhcat-java-client"/>
+  </publications>
+
+  <dependencies>
+    <dependency org="org.apache.hcatalog" name="hcatalog-core" rev="${hcatalog.version}"/>
+    <!-- test dependencies -->
+    <dependency org="junit" name="junit" rev="${junit.version}" conf="test->default"/>
+  </dependencies> 
+</ivy-module>

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,38 @@
+/*
+ * 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.hcatalog.api;
+
+import org.apache.hcatalog.common.HCatException;
+/**
+ * Class representing exceptions resulting from connection problems
+ * between HCat client and server.
+ */
+public class ConnectionFailureException extends HCatException {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * @param message
+     * @param cause
+     */
+    public ConnectionFailureException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatAddPartitionDesc.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatAddPartitionDesc.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatAddPartitionDesc.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatAddPartitionDesc.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,184 @@
+/*
+ * 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.hcatalog.api;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.metastore.MetaStoreUtils;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hcatalog.common.HCatException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class HCatAddPartitionDesc helps users in defining partition attributes.
+ */
+public class HCatAddPartitionDesc {
+
+    private static final Logger LOG = LoggerFactory.getLogger(HCatAddPartitionDesc.class);
+    private String tableName;
+    private String dbName;
+    private String location;
+    private Map<String,String> partSpec;
+
+     private HCatAddPartitionDesc(String dbName, String tbl, String loc, Map<String,String> spec){
+         this.dbName = dbName;
+         this.tableName = tbl;
+         this.location = loc;
+         this.partSpec = spec;
+     }
+
+    /**
+     * Gets the location.
+     *
+     * @return the location
+     */
+    public String getLocation() {
+        return this.location;
+    }
+
+
+    /**
+     * Gets the partition spec.
+     *
+     * @return the partition spec
+     */
+    public Map<String, String> getPartitionSpec() {
+        return this.partSpec;
+    }
+
+    /**
+     * Gets the table name.
+     *
+     * @return the table name
+     */
+    public String getTableName() {
+        return this.tableName;
+    }
+
+    /**
+     * Gets the database name.
+     *
+     * @return the database name
+     */
+    public String getDatabaseName() {
+        return this.dbName;
+    }
+
+     @Override
+    public String toString() {
+        return "HCatAddPartitionDesc ["
+                + (tableName != null ? "tableName=" + tableName + ", " : "tableName=null")
+                + (dbName != null ? "dbName=" + dbName + ", " : "dbName=null")
+                + (location != null ? "location=" + location + ", " : "location=null")
+                + (partSpec != null ? "partSpec=" + partSpec : "partSpec=null") + "]";
+    }
+
+    /**
+      * Creates the builder for specifying attributes.
+      *
+      * @param dbName the db name
+      * @param tableName the table name
+      * @param location the location
+      * @param partSpec the part spec
+      * @return the builder
+      * @throws HCatException
+      */
+     public static Builder create(String dbName, String tableName, String location,
+             Map<String,String> partSpec) throws HCatException {
+         return new Builder(dbName, tableName, location, partSpec);
+     }
+
+     Partition toHivePartition(Table hiveTable) throws HCatException{
+         Partition hivePtn = new Partition();
+         hivePtn.setDbName(this.dbName);
+         hivePtn.setTableName(this.tableName);
+
+         List<String> pvals = new ArrayList<String>();
+         for (FieldSchema field : hiveTable.getPartitionKeys()) {
+           String val = partSpec.get(field.getName());
+           if (val == null || val.length() == 0) {
+               throw new HCatException("create partition: Value for key "
+                   + field.getName() + " is null or empty");
+             }
+           pvals.add(val);
+         }
+
+         hivePtn.setValues(pvals);
+         StorageDescriptor sd = new StorageDescriptor(hiveTable.getSd());
+         hivePtn.setSd(sd);
+         hivePtn.setParameters(hiveTable.getParameters());
+         if (this.location != null) {
+             hivePtn.getSd().setLocation(this.location);
+         } else {
+             String partName;
+            try {
+                partName = Warehouse.makePartName(
+                         hiveTable.getPartitionKeys(), pvals);
+                LOG.info("Setting partition location to :" + partName);
+            } catch (MetaException e) {
+                throw new HCatException("Exception while creating partition name.", e);
+            }
+             Path partPath = new Path(hiveTable.getSd().getLocation(), partName);
+             hivePtn.getSd().setLocation(partPath.toString());
+         }
+         hivePtn.setCreateTime((int) (System.currentTimeMillis() / 1000));
+         hivePtn.setLastAccessTimeIsSet(false);
+         return hivePtn;
+     }
+
+     public static class Builder {
+
+         private String tableName;
+         private String location;
+         private Map<String,String> values;
+         private String dbName;
+
+         private Builder(String dbName, String tableName, String location, Map<String,String> values){
+             this.dbName = dbName;
+             this.tableName = tableName;
+             this.location = location;
+             this.values = values;
+         }
+
+         /**
+          * Builds the HCatAddPartitionDesc.
+          *
+          * @return the h cat add partition desc
+          * @throws HCatException
+          */
+         public HCatAddPartitionDesc build() throws HCatException {
+             if(this.dbName == null){
+                 this.dbName = MetaStoreUtils.DEFAULT_DATABASE_NAME;
+             }
+             HCatAddPartitionDesc desc = new HCatAddPartitionDesc(
+                     this.dbName, this.tableName, this.location,
+                     this.values);
+             return desc;
+         }
+     }
+
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClient.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClient.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClient.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClient.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,317 @@
+/*
+ * 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.hcatalog.api;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.metastore.api.PartitionEventType;
+import org.apache.hcatalog.common.HCatException;
+
+/**
+ * The abstract class HCatClient containing APIs for HCatalog DDL commands.
+ */
+public abstract class HCatClient {
+
+    public enum DROP_DB_MODE { RESTRICT, CASCADE };
+    public static final String HCAT_CLIENT_IMPL_CLASS = "hcat.client.impl.class";
+    /**
+     * Creates an instance of HCatClient.
+     *
+     * @param conf An instance of configuration.
+     * @return An instance of HCatClient.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public static HCatClient create(Configuration conf) throws HCatException,
+            ConnectionFailureException {
+        HCatClient client = null;
+        String className = conf.get(HCAT_CLIENT_IMPL_CLASS,
+                HCatClientHMSImpl.class.getName());
+        try {
+            Class<? extends HCatClient> clientClass = Class.forName(className,
+                    true, JavaUtils.getClassLoader()).asSubclass(
+                    HCatClient.class);
+            client = (HCatClient) clientClass.newInstance();
+        } catch (ClassNotFoundException e) {
+            throw new HCatException(
+                    "ClassNotFoundException while creating client class.", e);
+        } catch (InstantiationException e) {
+            throw new HCatException(
+                    "InstantiationException while creating client class.", e);
+        } catch (IllegalAccessException e) {
+            throw new HCatException(
+                    "IllegalAccessException while creating client class.", e);
+        }
+        if(client != null){
+            client.initialize(conf);
+        }
+        return client;
+    }
+
+    abstract void initialize(Configuration conf) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Get all existing databases that match the given
+     * pattern. The matching occurs as per Java regular expressions
+     *
+     * @param databasePattern  java re pattern
+     * @return list of database names
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract List<String> listDatabaseNamesByPattern(String pattern)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * Gets the database.
+     *
+     * @param dbName The name of the database.
+     * @return An instance of HCatDatabaseInfo.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract HCatDatabase getDatabase(String dbName) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Creates the database.
+     *
+     * @param dbInfo An instance of HCatCreateDBDesc.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void createDatabase(HCatCreateDBDesc dbInfo)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Drops a database.
+     *
+     * @param dbName The name of the database to delete.
+     * @param ifExists Hive returns an error if the database specified does not exist,
+     *                 unless ifExists is set to true.
+     * @param mode This is set to either "restrict" or "cascade". Restrict will
+     *             remove the schema if all the tables are empty. Cascade removes
+     *             everything including data and definitions.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void dropDatabase(String dbName, boolean ifExists,
+            DROP_DB_MODE mode) throws HCatException, ConnectionFailureException;
+
+    /**
+     * Returns all existing tables from the specified database which match the given
+     * pattern. The matching occurs as per Java regular expressions.
+     * @param dbName
+     * @param tablePattern
+     * @return list of table names
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract List<String> listTableNamesByPattern(String dbName, String tablePattern)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Gets the table.
+     *
+     * @param dbName The name of the database.
+     * @param tableName The name of the table.
+     * @return An instance of HCatTableInfo.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract HCatTable getTable(String dbName, String tableName)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Creates the table.
+     *
+     * @param createTableDesc An instance of HCatCreateTableDesc class.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void createTable(HCatCreateTableDesc createTableDesc)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Creates the table like an existing table.
+     *
+     * @param dbName The name of the database.
+     * @param existingTblName The name of the existing table.
+     * @param newTableName The name of the new table.
+     * @param ifNotExists If true, then error related to already table existing is skipped.
+     * @param isExternal Set to "true", if table has be created at a different
+     *                   location other than default.
+     * @param location The location for the table.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void createTableLike(String dbName, String existingTblName,
+            String newTableName, boolean ifNotExists, boolean isExternal,
+            String location) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Drop table.
+     *
+     * @param dbName The name of the database.
+     * @param tableName The name of the table.
+     * @param ifExists Hive returns an error if the database specified does not exist,
+     *                 unless ifExists is set to true.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void dropTable(String dbName, String tableName,
+            boolean ifExists) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Renames a table.
+     *
+     * @param dbName The name of the database.
+     * @param oldName The name of the table to be renamed.
+     * @param newName The new name of the table.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void renameTable(String dbName, String oldName,
+            String newName) throws HCatException, ConnectionFailureException;
+
+    /**
+     * Gets all the partitions.
+     *
+     * @param dbName The name of the database.
+     * @param tblName The name of the table.
+     * @return A list of partitions.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract List<HCatPartition> getPartitions(String dbName, String tblName)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Gets the partition.
+     *
+     * @param dbName The database name.
+     * @param tableName The table name.
+     * @param partitionSpec The partition specification, {[col_name,value],[col_name2,value2]}.
+     * @return An instance of HCatPartitionInfo.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract HCatPartition getPartition(String dbName, String tableName,
+            Map<String,String> partitionSpec) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Adds the partition.
+     *
+     * @param partInfo An instance of HCatAddPartitionDesc.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void addPartition(HCatAddPartitionDesc partInfo)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * Adds a list of partitions.
+     *
+     * @param partInfoList A list of HCatAddPartitionDesc.
+     * @return
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract int addPartitions(List<HCatAddPartitionDesc> partInfoList)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * Drops partition.
+     *
+     * @param dbName The database name.
+     * @param tableName The table name.
+     * @param partitionSpec The partition specification, {[col_name,value],[col_name2,value2]}.
+     * @param ifExists Hive returns an error if the partition specified does not exist, unless ifExists is set to true.
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void dropPartition(String dbName, String tableName,
+            Map<String, String> partitionSpec, boolean ifExists)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * List partitions by filter.
+     *
+     * @param dbName The database name.
+     * @param tblName The table name.
+     * @param filter The filter string,
+     *    for example "part1 = \"p1_abc\" and part2 <= "\p2_test\"". Filtering can
+     *    be done only on string partition keys.
+     * @return list of partitions
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract List<HCatPartition> listPartitionsByFilter(String dbName, String tblName,
+            String filter) throws HCatException,ConnectionFailureException;
+
+    /**
+     * Mark partition for event.
+     *
+     * @param dbName The database name.
+     * @param tblName The table name.
+     * @param partKVs the key-values associated with the partition.
+     * @param eventType the event type
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void markPartitionForEvent(String dbName, String tblName,
+            Map<String, String> partKVs, PartitionEventType eventType)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Checks if a partition is marked for event.
+     *
+     * @param dbName the db name
+     * @param tblName the table name
+     * @param partKVs the key-values associated with the partition.
+     * @param eventType the event type
+     * @return true, if is partition marked for event
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract boolean isPartitionMarkedForEvent(String dbName, String tblName,
+            Map<String, String> partKVs, PartitionEventType eventType)
+            throws HCatException,ConnectionFailureException;
+
+    /**
+     * Gets the delegation token.
+     *
+     * @param owner the owner
+     * @param renewerKerberosPrincipalName the renewer kerberos principal name
+     * @return the delegation token
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract String getDelegationToken(String owner,
+            String renewerKerberosPrincipalName) throws HCatException,
+            ConnectionFailureException;
+
+    /**
+     * Renew delegation token.
+     *
+     * @param tokenStrForm the token string
+     * @return the new expiration time
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract long renewDelegationToken(String tokenStrForm)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * Cancel delegation token.
+     *
+     * @param tokenStrForm the token string
+     * @throws HCatException,ConnectionFailureException
+     */
+    public abstract void cancelDelegationToken(String tokenStrForm)
+            throws HCatException, ConnectionFailureException;
+
+    /**
+     * Close the hcatalog client.
+     *
+     * @throws HCatException
+     */
+    public abstract void close() throws HCatException;
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,663 @@
+/*
+ * 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.hcatalog.api;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
+import org.apache.hadoop.hive.metastore.MetaStoreUtils;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
+import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
+import org.apache.hadoop.hive.metastore.api.InvalidPartitionException;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.PartitionEventType;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.api.UnknownDBException;
+import org.apache.hadoop.hive.metastore.api.UnknownPartitionException;
+import org.apache.hadoop.hive.metastore.api.UnknownTableException;
+import org.apache.hcatalog.common.HCatException;
+import org.apache.hcatalog.common.HCatUtil;
+import org.apache.thrift.TException;
+
+/**
+ * The HCatClientHMSImpl is the Hive Metastore client based implementation of
+ * HCatClient.
+ */
+public class HCatClientHMSImpl extends HCatClient {
+
+    private HiveMetaStoreClient hmsClient;
+    private Configuration  config;
+    private HiveConf hiveConfig;
+
+    @Override
+    public List<String> listDatabaseNamesByPattern(String pattern)
+            throws HCatException, ConnectionFailureException {
+        List<String> dbNames = null;
+        try {
+            dbNames = hmsClient.getDatabases(pattern);
+        } catch (MetaException exp) {
+            throw new HCatException("MetaException while listing db names", exp);
+        }
+        return dbNames;
+    }
+
+    @Override
+    public HCatDatabase getDatabase(String dbName) throws HCatException,
+            ConnectionFailureException {
+        HCatDatabase db = null;
+        try {
+            Database hiveDB = hmsClient.getDatabase(checkDB(dbName));
+            if (hiveDB != null) {
+                db = new HCatDatabase(hiveDB);
+            }
+        } catch (NoSuchObjectException exp) {
+            throw new HCatException(
+                    "NoSuchObjectException while fetching database", exp);
+        } catch (MetaException exp) {
+            throw new HCatException("MetaException while fetching database",
+                    exp);
+        } catch (TException exp) {
+            throw new ConnectionFailureException(
+                    "TException while fetching database", exp);
+        }
+        return db;
+    }
+
+    @Override
+    public void createDatabase(HCatCreateDBDesc dbInfo) throws HCatException,
+            ConnectionFailureException {
+        try {
+            hmsClient.createDatabase(dbInfo.toHiveDb());
+        } catch (AlreadyExistsException exp) {
+            if (!dbInfo.getIfNotExists()) {
+                throw new HCatException(
+                        "AlreadyExistsException while creating database", exp);
+            }
+        } catch (InvalidObjectException exp) {
+            throw new HCatException(
+                    "InvalidObjectException while creating database", exp);
+        } catch (MetaException exp) {
+            throw new HCatException("MetaException while creating database",
+                    exp);
+        } catch (TException exp) {
+            throw new ConnectionFailureException(
+                    "TException while creating database", exp);
+        }
+    }
+
+    @Override
+    public void dropDatabase(String dbName, boolean ifExists, DROP_DB_MODE mode)
+            throws HCatException, ConnectionFailureException {
+        boolean isCascade;
+        if (mode.toString().equalsIgnoreCase("cascade")) {
+            isCascade = true;
+        } else {
+            isCascade = false;
+        }
+        try {
+            hmsClient.dropDatabase(checkDB(dbName), true, ifExists, isCascade);
+        } catch (NoSuchObjectException e) {
+            if (!ifExists) {
+                throw new HCatException(
+                        "NoSuchObjectException while dropping db.", e);
+            }
+        } catch (InvalidOperationException e) {
+            throw new HCatException(
+                    "InvalidOperationException while dropping db.", e);
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while dropping db.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException("TException while dropping db.",
+                    e);
+        }
+    }
+
+    @Override
+    public List<String> listTableNamesByPattern(String dbName,
+            String tablePattern) throws HCatException, ConnectionFailureException {
+        List<String> tableNames = null;
+        try {
+            tableNames = hmsClient.getTables(checkDB(dbName), tablePattern);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while fetching table names.", e);
+        }
+        return tableNames;
+    }
+
+    @Override
+    public HCatTable getTable(String dbName, String tableName)
+            throws HCatException, ConnectionFailureException {
+        HCatTable table = null;
+        try {
+            Table hiveTable = hmsClient.getTable(checkDB(dbName), tableName);
+            if (hiveTable != null) {
+                table = new HCatTable(hiveTable);
+            }
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while fetching table.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while fetching table.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while fetching table.", e);
+        }
+        return table;
+    }
+
+    @Override
+    public void createTable(HCatCreateTableDesc createTableDesc)
+            throws HCatException, ConnectionFailureException {
+        try {
+            hmsClient.createTable(createTableDesc.toHiveTable(hiveConfig));
+        } catch (AlreadyExistsException e) {
+            if (createTableDesc.getIfNotExists() == false) {
+                throw new HCatException(
+                        "AlreadyExistsException while creating table.", e);
+            }
+        } catch (InvalidObjectException e) {
+            throw new HCatException(
+                    "InvalidObjectException while creating table.", e);
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while creating table.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while creating table.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while creating table.", e);
+        } catch (IOException e) {
+            throw new HCatException("IOException while creating hive conf.", e);
+        }
+
+    }
+
+    @Override
+    public void createTableLike(String dbName, String existingTblName,
+            String newTableName, boolean ifNotExists, boolean isExternal,
+            String location) throws HCatException, ConnectionFailureException {
+
+        Table hiveTable = getHiveTableLike(checkDB(dbName), existingTblName,
+                newTableName, ifNotExists, location);
+        if (hiveTable != null) {
+            try {
+                hmsClient.createTable(hiveTable);
+            } catch (AlreadyExistsException e) {
+                if (!ifNotExists) {
+                    throw new HCatException(
+                            "A table already exists with the name "
+                                    + newTableName, e);
+                }
+            } catch (InvalidObjectException e) {
+                throw new HCatException(
+                        "InvalidObjectException in create table like command.",
+                        e);
+            } catch (MetaException e) {
+                throw new HCatException(
+                        "MetaException in create table like command.", e);
+            } catch (NoSuchObjectException e) {
+                throw new HCatException(
+                        "NoSuchObjectException in create table like command.",
+                        e);
+            } catch (TException e) {
+                throw new ConnectionFailureException(
+                        "TException in create table like command.", e);
+            }
+        }
+    }
+
+    @Override
+    public void dropTable(String dbName, String tableName, boolean ifExists)
+            throws HCatException, ConnectionFailureException {
+        try {
+            hmsClient.dropTable(checkDB(dbName), tableName);
+        } catch (NoSuchObjectException e) {
+            if (!ifExists) {
+                throw new HCatException(
+                        "NoSuchObjectException while dropping table.", e);
+            }
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while dropping table.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while dropping table.", e);
+        }
+    }
+
+    @Override
+    public void renameTable(String dbName, String oldName, String newName)
+            throws HCatException, ConnectionFailureException {
+        Table tbl;
+        try {
+            Table oldtbl = hmsClient.getTable(checkDB(dbName), oldName);
+            if (oldtbl != null) {
+                // TODO : Should be moved out.
+                if (oldtbl
+                        .getParameters()
+                        .get(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_STORAGE) != null) {
+                    throw new HCatException(
+                            "Cannot use rename command on a non-native table");
+                }
+                tbl = new Table(oldtbl);
+                tbl.setTableName(newName);
+                hmsClient.alter_table(checkDB(dbName), oldName, tbl);
+            }
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while renaming table", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while renaming table", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while renaming table", e);
+        } catch (InvalidOperationException e) {
+            throw new HCatException(
+                    "InvalidOperationException while renaming table", e);
+        }
+    }
+
+    @Override
+    public List<HCatPartition> getPartitions(String dbName, String tblName)
+            throws HCatException, ConnectionFailureException {
+        List<HCatPartition> hcatPtns = new ArrayList<HCatPartition>();
+        try {
+            List<Partition> hivePtns = hmsClient.listPartitions(
+                    checkDB(dbName), tblName, (short) -1);
+            for (Partition ptn : hivePtns) {
+                hcatPtns.add(new HCatPartition(ptn));
+            }
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while retrieving partition.", e);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while retrieving partition.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while retrieving partition.", e);
+        }
+        return hcatPtns;
+    }
+
+    @Override
+    public HCatPartition getPartition(String dbName, String tableName,
+            Map<String, String> partitionSpec) throws HCatException,
+            ConnectionFailureException {
+        HCatPartition partition = null;
+        try {
+            ArrayList<String> ptnValues = new ArrayList<String>();
+            ptnValues.addAll(partitionSpec.values());
+            Partition hivePartition = hmsClient.getPartition(checkDB(dbName),
+                    tableName, ptnValues);
+            if (hivePartition != null) {
+                partition = new HCatPartition(hivePartition);
+            }
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while retrieving partition.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while retrieving partition.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while retrieving partition.", e);
+        }
+        return partition;
+    }
+
+    @Override
+    public void addPartition(HCatAddPartitionDesc partInfo)
+            throws HCatException, ConnectionFailureException {
+        Table tbl = null;
+        try {
+            tbl = hmsClient.getTable(partInfo.getDatabaseName(),
+                    partInfo.getTableName());
+            // TODO: Should be moved out.
+            if (tbl.getPartitionKeysSize() == 0) {
+                throw new HCatException("The table " + partInfo.getTableName()
+                        + " is not partitioned.");
+            }
+
+            hmsClient.add_partition(partInfo.toHivePartition(tbl));
+        } catch (InvalidObjectException e) {
+            throw new HCatException(
+                    "InvalidObjectException while adding partition.", e);
+        } catch (AlreadyExistsException e) {
+            throw new HCatException(
+                    "AlreadyExistsException while adding partition.", e);
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while adding partition.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while adding partition.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException("The table " + partInfo.getTableName()
+                    + " is could not be found.", e);
+        }
+    }
+
+    @Override
+    public void dropPartition(String dbName, String tableName,
+            Map<String, String> partitionSpec, boolean ifExists)
+            throws HCatException, ConnectionFailureException {
+        try {
+            List<String> ptnValues = new ArrayList<String>();
+            ptnValues.addAll(partitionSpec.values());
+            hmsClient.dropPartition(checkDB(dbName), tableName, ptnValues,
+                    ifExists);
+        } catch (NoSuchObjectException e) {
+            if (!ifExists) {
+                throw new HCatException(
+                        "NoSuchObjectException while dropping partition.", e);
+            }
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while dropping partition.",
+                    e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while dropping partition.", e);
+        }
+    }
+
+    @Override
+    public List<HCatPartition> listPartitionsByFilter(String dbName,
+            String tblName, String filter) throws HCatException,
+            ConnectionFailureException {
+        List<HCatPartition> hcatPtns = new ArrayList<HCatPartition>();
+        try {
+            List<Partition> hivePtns = hmsClient.listPartitionsByFilter(
+                    checkDB(dbName), tblName, filter, (short) -1);
+            for (Partition ptn : hivePtns) {
+                hcatPtns.add(new HCatPartition(ptn));
+            }
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while fetching partitions.",
+                    e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while fetching partitions.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while fetching partitions.", e);
+        }
+        return hcatPtns;
+    }
+
+    @Override
+    public void markPartitionForEvent(String dbName, String tblName,
+            Map<String, String> partKVs, PartitionEventType eventType)
+            throws HCatException, ConnectionFailureException {
+        try {
+            hmsClient.markPartitionForEvent(checkDB(dbName), tblName, partKVs,
+                    eventType);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while marking partition for event.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while marking partition for event.",
+                    e);
+        } catch (UnknownTableException e) {
+            throw new HCatException(
+                    "UnknownTableException while marking partition for event.",
+                    e);
+        } catch (UnknownDBException e) {
+            throw new HCatException(
+                    "UnknownDBException while marking partition for event.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while marking partition for event.", e);
+        } catch (InvalidPartitionException e) {
+            throw new HCatException(
+                    "InvalidPartitionException while marking partition for event.",
+                    e);
+        } catch (UnknownPartitionException e) {
+            throw new HCatException(
+                    "UnknownPartitionException while marking partition for event.",
+                    e);
+        }
+    }
+
+    @Override
+    public boolean isPartitionMarkedForEvent(String dbName, String tblName,
+            Map<String, String> partKVs, PartitionEventType eventType)
+            throws HCatException, ConnectionFailureException {
+        boolean isMarked = false;
+        try {
+            isMarked = hmsClient.isPartitionMarkedForEvent(checkDB(dbName),
+                    tblName, partKVs, eventType);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while checking partition for event.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException(
+                    "NoSuchObjectException while checking partition for event.",
+                    e);
+        } catch (UnknownTableException e) {
+            throw new HCatException(
+                    "UnknownTableException while checking partition for event.",
+                    e);
+        } catch (UnknownDBException e) {
+            throw new HCatException(
+                    "UnknownDBException while checking partition for event.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while checking partition for event.", e);
+        } catch (InvalidPartitionException e) {
+            throw new HCatException(
+                    "InvalidPartitionException while checking partition for event.",
+                    e);
+        } catch (UnknownPartitionException e) {
+            throw new HCatException(
+                    "UnknownPartitionException while checking partition for event.",
+                    e);
+        }
+        return isMarked;
+    }
+
+    @Override
+    public String getDelegationToken(String owner,
+            String renewerKerberosPrincipalName) throws HCatException,
+            ConnectionFailureException {
+        String token = null;
+        try {
+            token = hmsClient.getDelegationToken(owner,
+                    renewerKerberosPrincipalName);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while getting delegation token.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while getting delegation token.", e);
+        }
+
+        return token;
+    }
+
+    @Override
+    public long renewDelegationToken(String tokenStrForm) throws HCatException,
+            ConnectionFailureException {
+        long time = 0;
+        try {
+            time = hmsClient.renewDelegationToken(tokenStrForm);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while renewing delegation token.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while renewing delegation token.", e);
+        }
+
+        return time;
+    }
+
+    @Override
+    public void cancelDelegationToken(String tokenStrForm)
+            throws HCatException, ConnectionFailureException {
+        try {
+            hmsClient.cancelDelegationToken(tokenStrForm);
+        } catch (MetaException e) {
+            throw new HCatException(
+                    "MetaException while canceling delegation token.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while canceling delegation token.", e);
+        }
+    }
+
+    /*
+     * @param conf /* @throws HCatException,ConnectionFailureException
+     *
+     * @see
+     * org.apache.hcatalog.api.HCatClient#initialize(org.apache.hadoop.conf.
+     * Configuration)
+     */
+    @Override
+    void initialize(Configuration conf) throws HCatException,
+            ConnectionFailureException {
+        this.config = conf;
+        try {
+            hiveConfig = HCatUtil.getHiveConf(config);
+            hmsClient = HCatUtil.createHiveClient(hiveConfig);
+        } catch (MetaException exp) {
+            throw new HCatException("MetaException while creating HMS client",
+                    exp);
+        } catch (IOException exp) {
+            throw new HCatException("IOException while creating HMS client",
+                    exp);
+        }
+
+    }
+
+    private Table getHiveTableLike(String dbName, String existingTblName,
+            String newTableName, boolean isExternal, String location)
+            throws HCatException, ConnectionFailureException {
+        Table oldtbl = null;
+        Table newTable = null;
+        try {
+            oldtbl = hmsClient.getTable(checkDB(dbName), existingTblName);
+        } catch (MetaException e1) {
+            throw new HCatException(
+                    "MetaException while retrieving existing table.", e1);
+        } catch (TException e1) {
+            throw new ConnectionFailureException(
+                    "TException while retrieving existing table.", e1);
+        } catch (NoSuchObjectException e1) {
+            throw new HCatException(
+                    "NoSuchObjectException while retrieving existing table.",
+                    e1);
+        }
+        if (oldtbl != null) {
+            newTable = new Table();
+            newTable.setTableName(newTableName);
+            newTable.setDbName(dbName);
+            StorageDescriptor sd = new StorageDescriptor(oldtbl.getSd());
+            newTable.setSd(sd);
+            newTable.setParameters(oldtbl.getParameters());
+            if (location == null) {
+                newTable.getSd().setLocation(oldtbl.getSd().getLocation());
+            } else {
+                newTable.getSd().setLocation(location);
+            }
+            if (isExternal) {
+                newTable.putToParameters("EXTERNAL", "TRUE");
+                newTable.setTableType(TableType.EXTERNAL_TABLE.toString());
+            } else {
+                newTable.getParameters().remove("EXTERNAL");
+            }
+            // set create time
+            newTable.setCreateTime((int) (System.currentTimeMillis() / 1000));
+            newTable.setLastAccessTimeIsSet(false);
+        }
+        return newTable;
+    }
+
+    /*
+     * @throws HCatException
+     *
+     * @see org.apache.hcatalog.api.HCatClient#closeClient()
+     */
+    @Override
+    public void close() throws HCatException {
+        hmsClient.close();
+    }
+
+    private String checkDB(String name) {
+        if (StringUtils.isEmpty(name)) {
+            return MetaStoreUtils.DEFAULT_DATABASE_NAME;
+        } else {
+            return name;
+        }
+    }
+
+    /*
+     * @param partInfoList
+     *  @return The size of the list of partitions.
+     * @throws HCatException,ConnectionFailureException
+     * @see org.apache.hcatalog.api.HCatClient#addPartitions(java.util.List)
+     */
+    @Override
+    public int addPartitions(List<HCatAddPartitionDesc> partInfoList)
+            throws HCatException, ConnectionFailureException {
+        int numPartitions = -1;
+        if ((partInfoList == null) || (partInfoList.size() == 0)) {
+            throw new HCatException("The partition list is null or empty.");
+        }
+
+        Table tbl = null;
+        try {
+            tbl = hmsClient.getTable(partInfoList.get(0).getDatabaseName(),
+                    partInfoList.get(0).getTableName());
+            ArrayList<Partition> ptnList = new ArrayList<Partition>();
+            for (HCatAddPartitionDesc desc : partInfoList) {
+                ptnList.add(desc.toHivePartition(tbl));
+            }
+            numPartitions = hmsClient.add_partitions(ptnList);
+        } catch (InvalidObjectException e) {
+            throw new HCatException(
+                    "InvalidObjectException while adding partition.", e);
+        } catch (AlreadyExistsException e) {
+            throw new HCatException(
+                    "AlreadyExistsException while adding partition.", e);
+        } catch (MetaException e) {
+            throw new HCatException("MetaException while adding partition.", e);
+        } catch (TException e) {
+            throw new ConnectionFailureException(
+                    "TException while adding partition.", e);
+        } catch (NoSuchObjectException e) {
+            throw new HCatException("The table "
+                    + partInfoList.get(0).getTableName()
+                    + " is could not be found.", e);
+        }
+        return numPartitions;
+    }
+
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateDBDesc.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateDBDesc.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateDBDesc.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateDBDesc.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,193 @@
+/*
+ * 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.hcatalog.api;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hcatalog.common.HCatException;
+
+/**
+ * The Class HCatCreateDBDesc for defining database attributes.
+ */
+public class HCatCreateDBDesc {
+
+    private String dbName;
+    private String locationUri;
+    private String comment;
+    private Map<String, String> dbProperties;
+    private boolean ifNotExits = false;
+
+    /**
+     * Gets the database properties.
+     *
+     * @return the database properties
+     */
+    public Map<String, String> getDatabaseProperties() {
+        return this.dbProperties;
+    }
+
+    /**
+     * Gets the if not exists.
+     *
+     * @return the if not exists
+     */
+    public boolean getIfNotExists(){
+        return this.ifNotExits;
+    }
+
+    /**
+     * Gets the comments.
+     *
+     * @return the comments
+     */
+    public String getComments() {
+        return this.comment;
+    }
+
+    /**
+     * Gets the location.
+     *
+     * @return the location
+     */
+    public String getLocation() {
+        return this.locationUri;
+    }
+
+    /**
+     * Gets the database name.
+     *
+     * @return the database name
+     */
+    public String getDatabaseName() {
+        return this.dbName;
+    }
+
+    private HCatCreateDBDesc(String dbName){
+       this.dbName = dbName;
+    }
+
+    @Override
+    public String toString() {
+        return "HCatCreateDBDesc ["
+                + (dbName != null ? "dbName=" + dbName + ", " : "dbName=null")
+                + (locationUri != null ? "location=" + locationUri + ", "
+                        : "location=null")
+                + (comment != null ? "comment=" + comment + ", " : "comment=null")
+                + (dbProperties != null ? "dbProperties=" + dbProperties + ", "
+                        : "dbProperties=null") + "ifNotExits=" + ifNotExits + "]";
+    }
+
+    /**
+     * Creates the builder for defining attributes.
+     *
+     * @param dbName the db name
+     * @return the builder
+     */
+    public static Builder create(String dbName){
+        return new Builder(dbName);
+    }
+
+    Database toHiveDb(){
+        Database hiveDB = new Database();
+        hiveDB.setDescription(this.comment);
+        hiveDB.setLocationUri(this.locationUri);
+        hiveDB.setName(this.dbName);
+        hiveDB.setParameters(this.dbProperties);
+        return hiveDB;
+    }
+
+    public static class Builder {
+
+        private String innerLoc;
+        private String innerComment;
+        private Map<String, String> innerDBProps;
+        private String dbName;
+        private boolean ifNotExists = false;
+
+        private Builder(String dbName){
+            this.dbName = dbName;
+        }
+
+        /**
+         * Location.
+         *
+         * @param value the location of the database.
+         * @return the builder
+         */
+        public Builder location(String value){
+            this.innerLoc = value;
+            return this;
+        }
+
+        /**
+         * Comment.
+         *
+         * @param value comments.
+         * @return the builder
+         */
+        public Builder comment(String value){
+            this.innerComment = value;
+            return this;
+        }
+
+        /**
+         * If not exists.
+         * @param ifNotExists If set to true, hive will not throw exception, if a
+         * database with the same name already exists.
+         * @return the builder
+         */
+        public Builder ifNotExists(boolean ifNotExists){
+            this.ifNotExists = ifNotExists;
+            return this;
+        }
+
+        /**
+         * Database properties.
+         *
+         * @param dbProps the database properties
+         * @return the builder
+         */
+        public Builder databaseProperties(Map<String, String> dbProps) {
+            this.innerDBProps = dbProps;
+            return this;
+        }
+
+
+        /**
+         * Builds the create database descriptor.
+         *
+         * @return An instance of HCatCreateDBDesc
+         * @throws HCatException
+         */
+        public HCatCreateDBDesc build() throws HCatException {
+            if(this.dbName == null){
+                throw new HCatException("Database name cannot be null.");
+            }
+            HCatCreateDBDesc desc = new HCatCreateDBDesc(this.dbName);
+            desc.comment = this.innerComment;
+            desc.locationUri = this.innerLoc;
+            desc.dbProperties = this.innerDBProps;
+            desc.ifNotExits = this.ifNotExists;
+            return desc;
+
+        }
+
+    }
+
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateTableDesc.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateTableDesc.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateTableDesc.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatCreateTableDesc.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,519 @@
+/*
+ * 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.hcatalog.api;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.MetaStoreUtils;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.Order;
+import org.apache.hadoop.hive.metastore.api.SerDeInfo;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat;
+import org.apache.hadoop.hive.ql.io.RCFileInputFormat;
+import org.apache.hadoop.hive.ql.io.RCFileOutputFormat;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.metadata.HiveStorageHandler;
+import org.apache.hadoop.hive.ql.metadata.HiveUtils;
+import org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe;
+import org.apache.hadoop.mapred.SequenceFileInputFormat;
+import org.apache.hadoop.mapred.SequenceFileOutputFormat;
+import org.apache.hadoop.mapred.TextInputFormat;
+import org.apache.hcatalog.common.HCatException;
+import org.apache.hcatalog.data.schema.HCatFieldSchema;
+import org.apache.hcatalog.data.schema.HCatSchemaUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class HCatCreateTableDesc for defining attributes for a new table.
+ */
+@SuppressWarnings("deprecation")
+public class HCatCreateTableDesc{
+
+    private static final Logger LOG = LoggerFactory.getLogger(HCatCreateTableDesc.class);
+
+    private String tableName;
+    private String dbName;
+    private boolean isExternal;
+    private String comment;
+    private String location;
+    private List<HCatFieldSchema> cols;
+    private List<HCatFieldSchema> partCols;
+    private List<String> bucketCols;
+    private int numBuckets;
+    private List<Order> sortCols;
+    private Map<String, String> tblProps;
+    private boolean ifNotExists;
+    private String fileFormat;
+    private String inputformat;
+    private String outputformat;
+    private String serde;
+    private String storageHandler;
+
+    private HCatCreateTableDesc(String dbName, String tableName, List<HCatFieldSchema> columns){
+        this.dbName = dbName;
+        this.tableName = tableName;
+        this.cols = columns;
+    }
+
+    /**
+     * Creates a builder for defining attributes.
+     *
+     * @param dbName the db name
+     * @param tableName the table name
+     * @param columns the columns
+     * @return the builder
+     */
+    public static Builder create(String dbName, String tableName, List<HCatFieldSchema> columns){
+        return new Builder(dbName, tableName, columns);
+    }
+
+    Table toHiveTable(HiveConf conf) throws HCatException{
+
+        Table newTable = new Table();
+        newTable.setDbName(dbName);
+        newTable.setTableName(tableName);
+        if (tblProps != null) {
+            newTable.setParameters(tblProps);
+        }
+
+        if (isExternal) {
+            newTable.putToParameters("EXTERNAL", "TRUE");
+            newTable.setTableType(TableType.EXTERNAL_TABLE.toString());
+        } else {
+            newTable.setTableType(TableType.MANAGED_TABLE.toString());
+        }
+
+        StorageDescriptor sd = new StorageDescriptor();
+        sd.setSerdeInfo(new SerDeInfo());
+        if (location != null) {
+            sd.setLocation(location);
+        }
+        if (this.comment != null) {
+            newTable.putToParameters("comment", comment);
+        }
+        if (!StringUtils.isEmpty(fileFormat)) {
+            sd.setInputFormat(inputformat);
+            sd.setOutputFormat(outputformat);
+            if (serde != null) {
+                sd.getSerdeInfo().setSerializationLib(serde);
+            } else {
+                LOG.info("Using LazySimpleSerDe for table " + tableName);
+                sd.getSerdeInfo()
+                        .setSerializationLib(
+                                org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class
+                                        .getName());
+            }
+        } else {
+            try {
+                LOG.info("Creating instance of storage handler to get input/output, serder info.");
+                HiveStorageHandler sh = HiveUtils.getStorageHandler(conf,
+                        storageHandler);
+                sd.setInputFormat(sh.getInputFormatClass().getName());
+                sd.setOutputFormat(sh.getOutputFormatClass().getName());
+                sd.getSerdeInfo().setSerializationLib(
+                        sh.getSerDeClass().getName());
+                newTable.putToParameters(
+                        org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_STORAGE,
+                        storageHandler);
+            } catch (HiveException e) {
+                throw new HCatException(
+                        "Exception while creating instance of storage handler",
+                        e);
+            }
+        }
+        newTable.setSd(sd);
+        if (this.partCols != null) {
+            ArrayList<FieldSchema> hivePtnCols = new ArrayList<FieldSchema>();
+            for (HCatFieldSchema fs : this.partCols) {
+                hivePtnCols.add(HCatSchemaUtils.getFieldSchema(fs));
+            }
+            newTable.setPartitionKeys(hivePtnCols);
+        }
+
+        if (this.cols != null) {
+            ArrayList<FieldSchema> hiveTblCols = new ArrayList<FieldSchema>();
+            for (HCatFieldSchema fs : this.cols) {
+                hiveTblCols.add(HCatSchemaUtils.getFieldSchema(fs));
+            }
+            newTable.getSd().setCols(hiveTblCols);
+        }
+
+        if (this.bucketCols != null) {
+            newTable.getSd().setBucketCols(bucketCols);
+            newTable.getSd().setNumBuckets(numBuckets);
+        }
+
+        if (this.sortCols != null) {
+            newTable.getSd().setSortCols(sortCols);
+        }
+
+        newTable.setCreateTime((int) (System.currentTimeMillis() / 1000));
+        newTable.setLastAccessTimeIsSet(false);
+        return newTable;
+    }
+
+      /**
+       * Gets the if not exists.
+       *
+       * @return the if not exists
+       */
+      public boolean getIfNotExists() {
+          return this.ifNotExists;
+      }
+
+     /**
+      * Gets the table name.
+      *
+      * @return the table name
+      */
+     public String getTableName() {
+          return this.tableName;
+      }
+
+      /**
+       * Gets the cols.
+       *
+       * @return the cols
+       */
+      public List<HCatFieldSchema> getCols() {
+         return this.cols;
+      }
+
+      /**
+       * Gets the partition cols.
+       *
+       * @return the partition cols
+       */
+      public List<HCatFieldSchema> getPartitionCols() {
+          return this.partCols;
+      }
+
+      /**
+       * Gets the bucket cols.
+       *
+       * @return the bucket cols
+       */
+      public List<String> getBucketCols() {
+          return this.bucketCols;
+      }
+
+      public int getNumBuckets() {
+          return this.numBuckets;
+      }
+
+      /**
+       * Gets the comments.
+       *
+       * @return the comments
+       */
+      public String getComments() {
+          return this.comment;
+      }
+
+      /**
+       * Gets the storage handler.
+       *
+       * @return the storage handler
+       */
+      public String getStorageHandler() {
+          return this.storageHandler;
+      }
+
+      /**
+       * Gets the location.
+       *
+       * @return the location
+       */
+      public String getLocation() {
+          return this.location;
+      }
+
+      /**
+       * Gets the external.
+       *
+       * @return the external
+       */
+      public boolean getExternal() {
+          return this.isExternal;
+      }
+
+      /**
+       * Gets the sort cols.
+       *
+       * @return the sort cols
+       */
+      public List<Order> getSortCols() {
+          return this.sortCols;
+      }
+
+      /**
+       * Gets the tbl props.
+       *
+       * @return the tbl props
+       */
+      public Map<String, String> getTblProps() {
+          return this.tblProps;
+      }
+
+      /**
+       * Gets the file format.
+       *
+       * @return the file format
+       */
+      public String getFileFormat(){
+          return this.fileFormat;
+      }
+
+      /**
+       * Gets the database name.
+       *
+       * @return the database name
+       */
+      public String getDatabaseName() {
+          return this.dbName;
+      }
+
+      @Override
+    public String toString() {
+        return "HCatCreateTableDesc ["
+                + (tableName != null ? "tableName=" + tableName + ", " : "tableName=null")
+                + (dbName != null ? "dbName=" + dbName + ", " : "dbName=null")
+                + "isExternal="
+                + isExternal
+                + ", "
+                + (comment != null ? "comment=" + comment + ", " : "comment=null")
+                + (location != null ? "location=" + location + ", " : "location=null")
+                + (cols != null ? "cols=" + cols + ", " : "cols=null")
+                + (partCols != null ? "partCols=" + partCols + ", " : "partCols=null")
+                + (bucketCols != null ? "bucketCols=" + bucketCols + ", " : "bucketCols=null")
+                + "numBuckets="
+                + numBuckets
+                + ", "
+                + (sortCols != null ? "sortCols=" + sortCols + ", " : "sortCols=null")
+                + (tblProps != null ? "tblProps=" + tblProps + ", " : "tblProps=null")
+                + "ifNotExists="
+                + ifNotExists
+                + ", "
+                + (fileFormat != null ? "fileFormat=" + fileFormat + ", " : "fileFormat=null")
+                + (inputformat != null ? "inputformat=" + inputformat + ", "
+                        : "inputformat=null")
+                + (outputformat != null ? "outputformat=" + outputformat + ", "
+                        : "outputformat=null")
+                + (serde != null ? "serde=" + serde + ", " : "serde=null")
+                + (storageHandler != null ? "storageHandler=" + storageHandler
+                        : "storageHandler=null") + "]";
+    }
+
+    public static class Builder{
+
+          private String tableName;
+          private boolean isExternal;
+          private List<HCatFieldSchema> cols;
+          private List<HCatFieldSchema> partCols;
+          private List<String> bucketCols;
+          private List<Order> sortCols;
+          private int numBuckets;
+          private String comment;
+          private String fileFormat;
+          private String location;
+          private String storageHandler;
+          private Map<String, String> tblProps;
+          private boolean ifNotExists;
+          private String dbName;
+
+
+          private Builder(String dbName, String tableName, List<HCatFieldSchema> columns) {
+              this.dbName = dbName;
+              this.tableName = tableName;
+              this.cols = columns;
+          }
+
+
+          /**
+           * If not exists.
+           *
+           * @param ifNotExists If set to true, hive will not throw exception, if a
+           * table with the same name already exists.
+           * @return the builder
+           */
+          public Builder ifNotExists(boolean ifNotExists) {
+            this.ifNotExists = ifNotExists;
+            return this;
+          }
+
+
+          /**
+           * Partition cols.
+           *
+           * @param partCols the partition cols
+           * @return the builder
+           */
+          public Builder partCols(ArrayList<HCatFieldSchema> partCols) {
+              this.partCols = partCols;
+              return this;
+          }
+
+
+          /**
+           * Bucket cols.
+           *
+           * @param bucketCols the bucket cols
+           * @return the builder
+           */
+          public Builder bucketCols(ArrayList<String> bucketCols, int buckets) {
+            this.bucketCols = bucketCols;
+            this.numBuckets = buckets;
+            return this;
+          }
+
+          /**
+           * Storage handler.
+           *
+           * @param storageHandler the storage handler
+           * @return the builder
+           */
+          public Builder storageHandler(String storageHandler) {
+            this.storageHandler = storageHandler;
+            return this;
+          }
+
+          /**
+           * Location.
+           *
+           * @param location the location
+           * @return the builder
+           */
+          public Builder location(String location) {
+            this.location = location;
+            return this;
+          }
+
+          /**
+           * Comments.
+           *
+           * @param comment the comment
+           * @return the builder
+           */
+          public Builder comments(String comment) {
+            this.comment = comment;
+            return this;
+          }
+
+          /**
+           * Checks if is table external.
+           *
+           * @param isExternal the is external
+           * @return the builder
+           */
+          public Builder isTableExternal(boolean isExternal) {
+            this.isExternal = isExternal;
+            return this;
+          }
+
+          /**
+           * Sort cols.
+           *
+           * @param sortCols the sort cols
+           * @return the builder
+           */
+          public Builder sortCols(ArrayList<Order> sortCols) {
+            this.sortCols = sortCols;
+            return this;
+          }
+
+          /**
+           * Tbl props.
+           *
+           * @param tblProps the tbl props
+           * @return the builder
+           */
+          public Builder tblProps(Map<String, String> tblProps) {
+            this.tblProps = tblProps;
+            return this;
+          }
+
+          /**
+           * File format.
+           *
+           * @param format the format
+           * @return the builder
+           */
+          public Builder fileFormat(String format){
+              this.fileFormat = format;
+              return this;
+          }
+
+          /**
+           * Builds the HCatCreateTableDesc.
+           *
+           * @return HCatCreateTableDesc
+           * @throws HCatException
+           */
+          public HCatCreateTableDesc build() throws HCatException {
+              if(this.dbName == null){
+                LOG.info("Database name found null. Setting db to :"
+                        + MetaStoreUtils.DEFAULT_DATABASE_NAME);
+                this.dbName = MetaStoreUtils.DEFAULT_DATABASE_NAME;
+              }
+              HCatCreateTableDesc desc = new HCatCreateTableDesc(this.dbName,
+                      this.tableName, this.cols);
+              desc.ifNotExists = this.ifNotExists;
+              desc.isExternal = this.isExternal;
+              desc.comment = this.comment;
+              desc.partCols = this.partCols;
+              desc.bucketCols = this.bucketCols;
+              desc.numBuckets = this.numBuckets;
+              desc.location = this.location;
+              desc.tblProps = this.tblProps;
+              desc.sortCols = this.sortCols;
+              desc.serde = null;
+              if (!StringUtils.isEmpty(fileFormat)) {
+                  desc.fileFormat = fileFormat;
+                  if ("SequenceFile".equalsIgnoreCase(fileFormat)) {
+                      desc.inputformat = SequenceFileInputFormat.class.getName();
+                      desc.outputformat = SequenceFileOutputFormat.class
+                              .getName();
+                  } else if ("RCFile".equalsIgnoreCase(fileFormat)) {
+                      desc.inputformat = RCFileInputFormat.class.getName();
+                      desc.outputformat = RCFileOutputFormat.class.getName();
+                      desc.serde = ColumnarSerDe.class.getName();
+                  }
+                  desc.storageHandler = StringUtils.EMPTY;
+              } else if (!StringUtils.isEmpty(storageHandler)) {
+                  desc.storageHandler = storageHandler;
+              } else {
+                  desc.fileFormat = "TextFile";
+                  LOG.info("Using text file format for the table.");
+                  desc.inputformat = TextInputFormat.class.getName();
+                  LOG.info("Table input format:" + desc.inputformat);
+                  desc.outputformat = IgnoreKeyTextOutputFormat.class
+                          .getName();
+                  LOG.info("Table output format:" + desc.outputformat);
+              }
+              return desc;
+          }
+      }
+}

Added: incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatDatabase.java
URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatDatabase.java?rev=1371533&view=auto
==============================================================================
--- incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatDatabase.java (added)
+++ incubator/hcatalog/trunk/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatDatabase.java Fri Aug 10 00:16:59 2012
@@ -0,0 +1,87 @@
+/*
+ * 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.hcatalog.api;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.metastore.api.Database;
+
+
+/**
+ * HCatDatabase is wrapper class around org.apache.hadoop.hive.metastore.api.Database.
+ */
+public class HCatDatabase {
+
+    private String dbName;
+    private String dbLocation;
+    private String comment;
+    private Map<String, String> props;
+
+    HCatDatabase(Database db) {
+        this.dbName = db.getName();
+        this.props = db.getParameters();
+        this.dbLocation = db.getLocationUri();
+        this.comment = db.getDescription();
+    }
+
+    /**
+     * Gets the database name.
+     *
+     * @return the database name
+     */
+    public String getName(){
+        return dbName;
+    }
+
+    /**
+     * Gets the dB location.
+     *
+     * @return the dB location
+     */
+    public String getLocation(){
+        return dbLocation;
+    }
+
+    /**
+     * Gets the comment.
+     *
+     * @return the comment
+     */
+    public String getComment(){
+        return comment;
+    }
+
+    /**
+     * Gets the dB properties.
+     *
+     * @return the dB properties
+     */
+    public Map<String, String> getProperties(){
+        return props;
+    }
+
+    @Override
+    public String toString() {
+        return "HCatDatabase ["
+                + (dbName != null ? "dbName=" + dbName + ", " : "dbName=null")
+                + (dbLocation != null ? "dbLocation=" + dbLocation + ", " : "dbLocation=null")
+                + (comment != null ? "comment=" + comment + ", " : "comment=null")
+                + (props != null ? "props=" + props : "props=null") + "]";
+    }
+
+}



Mime
View raw message