Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

– This document is a work in progress.

Overview

There are presently three ways to issue HCatalog DDL commands:

  1. Command line interface
  2. Templeton REST APIs (upcoming)
  3. HiveMetaStore Client

Presently, java developers go through the Hive meta store (HMS) client interface to issue HCatalog DDl commands. Though the HMS client interface is public, it is not intended for public users. According to the hive user mailing list, the HMS client is not a public API and is subject to change in the future. So, it will be a good idea to have a java APIs in HCatalog which will provide a protect users from the changes made to the hive meta store client. Also, the under the covers either the Templeton Rest APIS or the hive metastore client can be used to provide end users with the required data.

...

The HCatClient is an abstract class containing all the APIs permitted HCatalog DDL commands. The implementation class will be provided as a configuration property, which will be used by the
"create" method. In this way, the implementation details will be masked to the users.

Code Block

/**
 * The abstract class HCatClient containing APIs for HCatalog DDL commands.
 */
public abstract class HCatClient {

    /**
     * Creates an instance of HCatClient.
     *
     * @param conf An instance of configuration.
     * @return An instance of HCatClient.
     * @throws IOException
     */
    public static HCatClient create(Configuration conf) throws IOException{
      HCatClient client // Obtain details of implementation class and return an instance.
    }

    /**
= HCatUtil.getHCatClient(conf);
        if(client != null){
        * Gets the database likeclient.initialize(conf);
     *
   }
  * @param regex The regular expression. Providing "*" would retrieve all the names
     *   return client;
    }

    abstract void initialize(Configuration conf) throws HCatException;

    /**
     * Get all existing databases that ofmatch the databases.given
     * @returnpattern. The matching listoccurs ofas allper theJava databaseregular names.expressions
     *
 @throws HCatException
   * @param */databasePattern
    public abstract List<String> getDatabases(String regex*          java re pattern
     * @return list of database names
     * @throws HCatException
     */
    public abstract List<String> listDatabaseNamesByPattern(String pattern) throws HCatException;

    /**
     * Gets the database.
     *
     * @param dbName The name of the database.
     * @return An instance of HCatDatabaseInfo.
     * @throws HCatException
     */
    public abstract HCatDatabase getDatabase(String dbName) throws HCatException;

    /**
     * Creates the database.
     *
     * @param dbInfo An instance of HCatCreateDBDesc.
     * @return true, if successful
     * @throws HCatException
@throws HCatException
     */
    public abstract booleanvoid createDatabase(HCatCreateDBDesc dbInfo)
            throws HCatException;

    /**
     * DeletesDrops 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.
     * @param@throws userGroupHCatException
 The user group to use*/
    public abstract * @param permissions The permissions string to use. The format is "rwxrw-r-x".void dropDatabase(String dbName, boolean ifExists, String mode) throws HCatException;

    /**
     * @return true, if successful
     * @throws HCatExceptionReturns all existing tables from the specified database which match the given
     */
 pattern. The matching publicoccurs abstractas booleanper deleteDatabase(String dbName, boolean ifExists, String mode,
    Java regular expressions.
     * @param dbName
     * @param tablePattern
 String userGroup, String permissions) throws HCatException;

    /*** @return list of table names
     * Gets@throws theHCatException
 tables like a pattern specified.*/
    public *
abstract List<String> listTableNamesByPattern(String dbName, String *tablePattern)
 @param dbName The name of the database.
     * @param regex The regular expression. Providing "*" would retrieve all the namesthrows HCatException;

    /**
     * Gets the table.
     *
     * @param dbName The  name    of  the tabledatabase.
     * @param @returntableName AThe listname of allthe table names matching the specified pattern.
     * @return An instance of HCatTableInfo.
     * @throws HCatException
     */
    public abstract List<String>HCatTable getTablesgetTable(String dbName, String regextableName)
            throws HCatException;

    /**
     * GetsCreates the table.
     *
     * @param dbNamecreateTableDesc TheAn nameinstance of theHCatCreateTableDesc databaseclass.
     * @param tableName The name of@throws HCatException the table.
h     * @return An instance of HCatTableInfo.
     * @throws HCatExceptioncat exception
     */
    public abstract HCatTablevoid getTablecreateTable(String dbName, String tableNameHCatCreateTableDesc createTableDesc)
            throws HCatException;

    /**
     * Creates the table.
 like an existing table.
     *
     * @param createTableDescdbName AnThe instancename of HCatCreateTableDescthe classdatabase.
     * @return true, if successful@param existingTblName The name of the existing table.
     * @throws HCatException@param newTableName The name of the h cat exceptionnew table.
     */
 @param ifNotExists If publictrue, abstractthen booleanerror createTable(HCatCreateTableDesc createTableDesc)
related to already table existing is skipped.
     * @param isExternal Set to "true", if table throws HCatException;

    /**has be created at a different
     * Creates the table like an existing table.
     *
     * @param dbName The namelocation ofother thethan databasedefault.
     * @param existingTblNamelocation The namelocation offor the existing table.
     * @param@throws newTableNameHCatException
 The name of the new table. */
    public *abstract @paramvoid ifExists the if existscreateTableLike(String dbName, String existingTblName,
     * @param isExternal Set to "true", if tableString hasnewTableName, beboolean createdifNotExists, at a differentboolean isExternal,
     *       String location) throws HCatException;

    /**
     * Drop table.
  location other than default.*
     * @param locationdbName The locationname forof the tabledatabase.
     * @return true, if successful @param tableName The name of the table.
     * @throws@param HCatException
ifExists Hive returns an error */
if the database specified publicdoes abstract boolean createTableLike(String dbName, String existingTblName,
not exist,
     *         String newTableName, boolean ifExists, boolean isExternal,
   unless ifExists is set to true.
    String location)* throws@throws HCatException;

     */**
    public abstract void * Delete a table.dropTable(String dbName, String tableName,
     *
     * @param dbNameboolean TheifExists) name of the database.throws HCatException;

    /**
 * @param tableName The name* ofRenames thea table.
     *
 @param ifExists Hive returns an* error@param ifdbName theThe databasename specifiedof does not exist,the database.
     * @param oldName The name of the table to be renamed.
     * @param unlessnewName The ifExistsnew isname setof tothe truetable.
     * @param@throws userGroupHCatException
 The user group to use.*/
    public *abstract @paramvoid permissions The permissions string to use. The format is "rwxrw-r-x".renameTable(String dbName, String oldName, String newName) throws HCatException;

     /**
 @return true, if successful
 * Gets all  * @throws HCatExceptionthe partitions.
     */
    public abstract* boolean@param deleteTable(String dbName, StringThe tableName,
name of the database.
     * @param tblName The booleanname ifExists,of String userGroup, String permissions)the table.
     * @return A list of partition names.
 throws HCatException;

    /**
 @throws HCatException the h * Renames a table.cat exception
     */
    public * @paramabstract List<HCatPartition> getPartitions(String dbName, TheString nametblName)
 of the database.
     * @param oldName The name of the table to be renamed.throws HCatException;

    /**
     * @paramGets newNamethe Thepartition.
 new name of the table.*
     * @param userGroupdbName The user group to usedatabase name.
     * @param permissionstableName The permissions string to use. The format is "rwxrw-r-x".table name.
     * @return@param true,partitionName ifThe successful
partition name, Comma separated list * @throwsof col_name='value'.
     * @return An instance of HCatPartitionInfo.
     * @throws HCatException
     */
    public abstract booleanHCatPartition renameTablegetPartition(String dbName, String oldNametableName, String newName,
            String userGroup, String permissionspartitionName) throws HCatException;

    /**
     * GetsAdds all the partitionspartition.
     *
     * @param dbNamepartInfo TheAn nameinstance of the databaseHCatAddPartitionDesc.
     * @param tblName The name of the table.
     * @return A list of partition names.
     * @throws HCatException the h cat exception
     */
    public abstract List<HCatPartition>void getPartitionsaddPartition(String dbName, String tblName)
            HCatAddPartitionDesc partInfo) throws HCatException;

    /**
     * GetsDrops the partition.
     *
     * @param dbName The database name.
     * @param tableName The table name.
     * @param partitionName The partition name, Comma separated list of col_name='value'.
     * @return An instance of HCatPartitionInfo @param ifExists Hive returns an error if the partition specified does not exist, unless ifExists is set to true.
     * @throws HCatException
     */
    public abstract HCatPartitionvoid getPartitiondropPartition(String dbName, String tableName,
            String partitionName, boolean ifExists) throws HCatException;

    /**
     * List Addspartitions theby partitionfilter.
     *
     * @param partInfodbName AnThe instance of HCatAddPartitionDesc.database name.
     * @return true, if successful@param tblName The table name.
     * @throws@param HCatExceptionfilter theThe h cat exceptionfilter string,
     */
    publicfor abstractexample boolean addPartition(HCatAddPartitionDesc partInfo) throws HCatException;

    /**"part1 = \"p1_abc\" and part2 <= "\p2_test\"". Filtering can
     * Deletes partition.
     *   be done only on string partition keys.
     * @param@return dbNamelist The database name.of partitions
     * @throws @paramHCatException tableNamethe Theh tablecat name.exception
     */
 @param partitionName The partitionpublic name,abstract CommaList<HCatPartition> separated list of col_name='value'.listPartitionsByFilter(String dbName, String tblName,
     *     @param ifExists HiveString returnsfilter) an error if the partition specified does not exist, unless ifExists is set to true.throws HCatException;

    /**
     * Mark partition for event.
     *
     * @param userGroupdbName The user group to usedatabase name.
     * @param permissionstblName The permissions string to use. The format is "rwxrw-r-x".table name.
     * @param partKVs the part k vs
     * @param @returneventType true,the ifevent successfultype
     * @throws HCatException the h cat exception
     */
    public abstract booleanvoid deletePartitionmarkPartitionForEvent(String dbName, String tableNametblName,
            String partitionNameMap<String, booleanString> ifExistspartKVs, StringPartitionEventType userGroup,eventType)
            String permissions) throws HCatException;

    /**
     * List partitions by filterChecks if is partition marked for event.
     *
     * @param dbName Thethe databasedb name.
     * @param tblName Thethe tabletbl name.
     * @param partKVs filterthe Thepart filterk string,vs
     * @param eventType the forevent exampletype
 "part1 = \"p1_abc\" and part2* <= "\p2_test\"". Filtering can
     *    be done only on string partition keys.
     * @return list of partitions@return true, if is partition marked for event
     * @throws HCatException the h cat exception
     */
    public abstract List<HCatPartition>boolean listPartitionsByFilterisPartitionMarkedForEvent(String dbName, String tblName,
            StringMap<String, filter)String> throws HCatException;

partKVs, PartitionEventType eventType)
    /**
     * Mark partition for event.throws HCatException;

     /**
     * @param dbName The database name.Gets the delegation token.
     *
     * @param tblNameowner The table name.the owner
     * @param partKVsrenewerKerberosPrincipalName the renewer partkerberos kprincipal vsname
     * @param@return eventType the eventdelegation typetoken
     * @throws HCatException the h cat exception
     */
    public abstract voidString markPartitionForEventgetDelegationToken(String dbNameowner, String tblName,
            Map<String, String> partKVs, PartitionEventType eventType)
  renewerKerberosPrincipalName) throws
          throws HCatException;

    /**
     * ChecksRenew if is partition marked for eventdelegation token.
     *
     * @param dbNametokenStrForm the dbtoken str nameform
     * @param tblName@return the tbllong
 name
     * @param@throws partKVsHCatException the parth kcat vsexception
     */
 @param eventType the eventpublic type
abstract long renewDelegationToken(String tokenStrForm) throws *HCatException;

 @return true, if is partition marked for event /**
     * @throwsCancel HCatException the h cat exceptiondelegation token.
     */
    public abstract* boolean isPartitionMarkedForEvent(String dbName, String tblName,@param tokenStrForm the token str form
     * @throws HCatException the h cat exception
 Map<String, String> partKVs, PartitionEventType eventType)*/
    public abstract void cancelDelegationToken(String tokenStrForm)    throws HCatException;

    /**
     * GetsClose the delegationhcatalog tokenclient.
     *
     * @param@throws ownerHCatException the ownerh cat exception
     */
 @param renewerKerberosPrincipalName the renewer kerberos principal name
     * @return the delegation token
     * @throws HCatException the h cat exception
     */
    public abstract String getDelegationToken(String owner, String renewerKerberosPrincipalName) throws
        HCatException;

    /**
     * Renew delegation token.
     *
     * @param tokenStrForm the token str form
     * @return the long
     * @throws HCatException the h cat exception
     */
    public abstract long renewDelegationToken(String tokenStrForm) throws HCatException;

    /**
     * Cancel delegation token.
     *
     * @param tokenStrForm the token str form
     * @throws HCatException the h cat exception
     */
    public abstract void cancelDelegationToken(String tokenStrForm) throws HCatException;

}
HCatCommandDesc

This is an abstract class that helps in validating user input and building command descriptors.

Code Block

abstract class HCatCommandDesc{

     String dbName;
     String userGroup;
     String permissions;

   protected static abstract class Builder {

        String innerUserGroup;
         String innerPerms;
         String innerDBName;

         protected Builder(String dbName){
            this.innerDBName = dbName;
        }

        public Builder setUserGroup(String user){
            this.innerUserGroup = user;
            return this;
        }

        /**
         * Sets the permissions.
         *
         * @param perms The permissions string to use. The format is "rwxrw-r-x".
         */
        protected Builder setPermissions(String perms){
            this.innerPerms = perms;
            return this;
        }

        protected abstract HCatCommandDesc build() throws HCatException;


    }

    protected HCatCommandDesc(String dbName, String userGroup, String permissions) {
    this.dbName = dbName;
    this.userGroup = userGroup;
    this.permissions = permissions;
}

    public String getDatabaseName() {
        return this.dbName;
    }

    public String getUserGroup(){
        return this.userGroup;
    }


    public String getPermissions(){
        return this.permissions;
    }


}public abstract void close() throws HCatException;
HCatCreateTableDesc

This class is a sub class of HCatCommandDesc and will be used by the users to create descriptor and validate it for the "create table" command.
Image Removed Image Added

HCatCreateDBDesc

This class is a sub class of HCatCommandDesc and will be used by the users to create descriptors and validate it for the "create database" command.

Image Removed!createdb.png|

HCatAddPartitionDesc

This class is a sub class of HCatCommandDesc and will be used by the users to create descriptors and validate it for the "add partition" command.

Image Removed Image Added

HCatTable

This class encapsulates the table information returned the HCatClient implementation class and provides a uniform view to the user.

Image RemovedImage Added

HCatDatabase

This class encapsulates the database information returned the HCatClient implementation class and provides a uniform view to the user.

Image RemovedImage Added

HCatPartition

This class encapsulates the partition information returned the HCatClient implementation class and provides a uniform view to the user.

Image RemovedImage Added

Usage

Code Block
 Configuration config = new Configuration();
 config.add("hive-site.xml");
 HCatClient client = HCatClient.create(config);
 ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>();
 cols.add(new HCatFieldSchema("col1id", Type.INT, "comment1id columns"));
 cols.add(new HCatFieldSchema("col2value", Type.STRING, "comment2id columns"));
HCatCreateTableDesc desctableDesc = new HCatCreateTableDesc.Buildercreate(db, "defaulttesttable","demo_table" cols).setFileFormatfileFormat("rcfile").setCols(cols).build();

 boolean success = client.createTable(desctableDesc);

Discussion Topics