http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java 
b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
index e2a7d7d..b43b4df 100644
--- a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
+++ b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
@@ -85,8 +85,12 @@ import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 import org.apache.hadoop.hive.io.HdfsUtils;
 import org.apache.hadoop.hive.metastore.api.*;
+import org.apache.hadoop.hive.metastore.events.AddForeignKeyEvent;
 import org.apache.hadoop.hive.metastore.events.AddIndexEvent;
+import org.apache.hadoop.hive.metastore.events.AddNotNullConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.AddPartitionEvent;
+import org.apache.hadoop.hive.metastore.events.AddPrimaryKeyEvent;
+import org.apache.hadoop.hive.metastore.events.AddUniqueConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.AlterIndexEvent;
 import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent;
 import org.apache.hadoop.hive.metastore.events.AlterTableEvent;
@@ -94,6 +98,7 @@ import 
org.apache.hadoop.hive.metastore.events.ConfigChangeEvent;
 import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent;
 import org.apache.hadoop.hive.metastore.events.CreateFunctionEvent;
 import org.apache.hadoop.hive.metastore.events.CreateTableEvent;
+import org.apache.hadoop.hive.metastore.events.DropConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent;
 import org.apache.hadoop.hive.metastore.events.DropFunctionEvent;
 import org.apache.hadoop.hive.metastore.events.DropIndexEvent;
@@ -1540,16 +1545,64 @@ public class HiveMetaStore extends ThriftHiveMetastore {
                 && uniqueConstraints == null && notNullConstraints == null) {
           ms.createTable(tbl);
         } else {
-          ms.createTableWithConstraints(tbl, primaryKeys, foreignKeys,
+          // Set constraint name if null before sending to listener
+          List<String> constraintNames = ms.createTableWithConstraints(tbl, 
primaryKeys, foreignKeys,
               uniqueConstraints, notNullConstraints);
+          int primaryKeySize = 0;
+          if (primaryKeys != null) {
+            primaryKeySize = primaryKeys.size();
+            for (int i = 0; i < primaryKeys.size(); i++) {
+              if (primaryKeys.get(i).getPk_name() == null) {
+                primaryKeys.get(i).setPk_name(constraintNames.get(i));
+              }
+            }
+          }
+          int foreignKeySize = 0;
+          if (foreignKeys != null) {
+            foreignKeySize = foreignKeys.size();
+            for (int i = 0; i < foreignKeySize; i++) {
+              if (foreignKeys.get(i).getFk_name() == null) {
+                
foreignKeys.get(i).setFk_name(constraintNames.get(primaryKeySize + i));
+              }
+            }
+          }
+          int uniqueConstraintSize = 0;
+          if (uniqueConstraints != null) {
+            uniqueConstraintSize = uniqueConstraints.size();
+            for (int i = 0; i < uniqueConstraintSize; i++) {
+              if (uniqueConstraints.get(i).getUk_name() == null) {
+                
uniqueConstraints.get(i).setUk_name(constraintNames.get(primaryKeySize + 
foreignKeySize + i));
+              }
+            }
+          }
+          if (notNullConstraints != null) {
+            for (int i = 0; i < notNullConstraints.size(); i++) {
+              if (notNullConstraints.get(i).getNn_name() == null) {
+                
notNullConstraints.get(i).setNn_name(constraintNames.get(primaryKeySize + 
foreignKeySize + uniqueConstraintSize + i));
+              }
+            }
+          }
         }
 
         if (!transactionalListeners.isEmpty()) {
-          transactionalListenerResponses =
-              MetaStoreListenerNotifier.notifyEvent(transactionalListeners,
-                                                    EventType.CREATE_TABLE,
-                                                    new CreateTableEvent(tbl, 
true, this),
-                                                    envContext);
+          transactionalListenerResponses = 
MetaStoreListenerNotifier.notifyEvent(transactionalListeners,
+              EventType.CREATE_TABLE, new CreateTableEvent(tbl, true, this), 
envContext);
+          if (primaryKeys != null && !primaryKeys.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(transactionalListeners, 
EventType.ADD_PRIMARYKEY,
+                new AddPrimaryKeyEvent(primaryKeys, true, this), envContext);
+          }
+          if (foreignKeys != null && !foreignKeys.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(transactionalListeners, 
EventType.ADD_FOREIGNKEY,
+                new AddForeignKeyEvent(foreignKeys, true, this), envContext);
+          }
+          if (uniqueConstraints != null && !uniqueConstraints.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(transactionalListeners, 
EventType.ADD_UNIQUECONSTRAINT,
+                new AddUniqueConstraintEvent(uniqueConstraints, true, this), 
envContext);
+          }
+          if (notNullConstraints != null && !notNullConstraints.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(transactionalListeners, 
EventType.ADD_NOTNULLCONSTRAINT,
+                new AddNotNullConstraintEvent(notNullConstraints, true, this), 
envContext);
+          }
         }
 
         success = ms.commitTransaction();
@@ -1562,11 +1615,24 @@ public class HiveMetaStore extends ThriftHiveMetastore {
         }
 
         if (!listeners.isEmpty()) {
-          MetaStoreListenerNotifier.notifyEvent(listeners,
-                                                EventType.CREATE_TABLE,
-                                                new CreateTableEvent(tbl, 
success, this),
-                                                envContext,
-                                                
transactionalListenerResponses, ms);
+          MetaStoreListenerNotifier.notifyEvent(listeners, 
EventType.CREATE_TABLE,
+              new CreateTableEvent(tbl, success, this), envContext, 
transactionalListenerResponses, ms);
+          if (primaryKeys != null && !primaryKeys.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(listeners, 
EventType.ADD_PRIMARYKEY,
+                new AddPrimaryKeyEvent(primaryKeys, success, this), 
envContext);
+          }
+          if (foreignKeys != null && !foreignKeys.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(listeners, 
EventType.ADD_FOREIGNKEY,
+                new AddForeignKeyEvent(foreignKeys, success, this), 
envContext);
+          }
+          if (uniqueConstraints != null && !uniqueConstraints.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(listeners, 
EventType.ADD_UNIQUECONSTRAINT,
+                new AddUniqueConstraintEvent(uniqueConstraints, success, 
this), envContext);
+          }
+          if (notNullConstraints != null && !notNullConstraints.isEmpty()) {
+            MetaStoreListenerNotifier.notifyEvent(listeners, 
EventType.ADD_NOTNULLCONSTRAINT,
+                new AddNotNullConstraintEvent(notNullConstraints, success, 
this), envContext);
+          }
         }
       }
     }
@@ -1647,9 +1713,18 @@ public class HiveMetaStore extends ThriftHiveMetastore {
       startFunction("drop_constraint", ": " + constraintName.toString());
       boolean success = false;
       Exception ex = null;
+      RawStore ms = getMS();
       try {
-        getMS().dropConstraint(dbName, tableName, constraintName);
-        success = true;
+        ms.openTransaction();
+        ms.dropConstraint(dbName, tableName, constraintName);
+        if (transactionalListeners.size() > 0) {
+          DropConstraintEvent dropConstraintEvent = new 
DropConstraintEvent(dbName,
+              tableName, constraintName, true, this);
+          for (MetaStoreEventListener transactionalListener : 
transactionalListeners) {
+            transactionalListener.onDropConstraint(dropConstraintEvent);
+          }
+        }
+        success = ms.commitTransaction();
       } catch (NoSuchObjectException e) {
         ex = e;
         throw new InvalidObjectException(e.getMessage());
@@ -1663,6 +1738,15 @@ public class HiveMetaStore extends ThriftHiveMetastore {
           throw newMetaException(e);
         }
       } finally {
+        if (!success) {
+          ms.rollbackTransaction();
+        } else {
+          for (MetaStoreEventListener listener : listeners) {
+            DropConstraintEvent dropConstraintEvent = new 
DropConstraintEvent(dbName,
+                tableName, constraintName, true, this);
+            listener.onDropConstraint(dropConstraintEvent);
+          }
+        }
         endFunction("drop_constraint", success, ex, constraintName);
       }
     }
@@ -1676,9 +1760,27 @@ public class HiveMetaStore extends ThriftHiveMetastore {
       startFunction("add_primary_key", ": " + constraintName);
       boolean success = false;
       Exception ex = null;
+      RawStore ms = getMS();
       try {
-        getMS().addPrimaryKeys(primaryKeyCols);
-        success = true;
+        ms.openTransaction();
+        List<String> constraintNames = ms.addPrimaryKeys(primaryKeyCols);
+        // Set primary key name if null before sending to listener
+        if (primaryKeyCols != null) {
+          for (int i = 0; i < primaryKeyCols.size(); i++) {
+            if (primaryKeyCols.get(i).getPk_name() == null) {
+              primaryKeyCols.get(i).setPk_name(constraintNames.get(i));
+            }
+          }
+        }
+        if (transactionalListeners.size() > 0) {
+          if (primaryKeyCols != null && primaryKeyCols.size() > 0) {
+            AddPrimaryKeyEvent addPrimaryKeyEvent = new 
AddPrimaryKeyEvent(primaryKeyCols, true, this);
+            for (MetaStoreEventListener transactionalListener : 
transactionalListeners) {
+              transactionalListener.onAddPrimaryKey(addPrimaryKeyEvent);
+            }
+          }
+        }
+        success = ms.commitTransaction();
       } catch (Exception e) {
         ex = e;
         if (e instanceof MetaException) {
@@ -1689,6 +1791,14 @@ public class HiveMetaStore extends ThriftHiveMetastore {
           throw newMetaException(e);
         }
       } finally {
+        if (!success) {
+          ms.rollbackTransaction();
+        } else if (primaryKeyCols != null && primaryKeyCols.size() > 0) {
+          for (MetaStoreEventListener listener : listeners) {
+            AddPrimaryKeyEvent addPrimaryKeyEvent = new 
AddPrimaryKeyEvent(primaryKeyCols, true, this);
+            listener.onAddPrimaryKey(addPrimaryKeyEvent);
+          }
+        }
         endFunction("add_primary_key", success, ex, constraintName);
       }
     }
@@ -1702,9 +1812,27 @@ public class HiveMetaStore extends ThriftHiveMetastore {
       startFunction("add_foreign_key", ": " + constraintName);
       boolean success = false;
       Exception ex = null;
+      RawStore ms = getMS();
       try {
-        getMS().addForeignKeys(foreignKeyCols);
-        success = true;
+        ms.openTransaction();
+        List<String> constraintNames = ms.addForeignKeys(foreignKeyCols);
+        // Set foreign key name if null before sending to listener
+        if (foreignKeyCols != null) {
+          for (int i = 0; i < foreignKeyCols.size(); i++) {
+            if (foreignKeyCols.get(i).getFk_name() == null) {
+              foreignKeyCols.get(i).setFk_name(constraintNames.get(i));
+            }
+          }
+        }
+        if (transactionalListeners.size() > 0) {
+          if (foreignKeyCols != null && foreignKeyCols.size() > 0) {
+            AddForeignKeyEvent addForeignKeyEvent = new 
AddForeignKeyEvent(foreignKeyCols, true, this);
+            for (MetaStoreEventListener transactionalListener : 
transactionalListeners) {
+              transactionalListener.onAddForeignKey(addForeignKeyEvent);
+            }
+          }
+        }
+        success = ms.commitTransaction();
       } catch (Exception e) {
         ex = e;
         if (e instanceof MetaException) {
@@ -1715,6 +1843,14 @@ public class HiveMetaStore extends ThriftHiveMetastore {
           throw newMetaException(e);
         }
       } finally {
+        if (!success) {
+          ms.rollbackTransaction();
+        } else if (foreignKeyCols != null && foreignKeyCols.size() > 0) {
+          for (MetaStoreEventListener listener : listeners) {
+            AddForeignKeyEvent addForeignKeyEvent = new 
AddForeignKeyEvent(foreignKeyCols, true, this);
+            listener.onAddForeignKey(addForeignKeyEvent);
+          }
+        }
         endFunction("add_foreign_key", success, ex, constraintName);
       }
     }
@@ -1728,9 +1864,27 @@ public class HiveMetaStore extends ThriftHiveMetastore {
       startFunction("add_unique_constraint", ": " + constraintName);
       boolean success = false;
       Exception ex = null;
+      RawStore ms = getMS();
       try {
-        getMS().addUniqueConstraints(uniqueConstraintCols);
-        success = true;
+        ms.openTransaction();
+        List<String> constraintNames = 
ms.addUniqueConstraints(uniqueConstraintCols);
+        // Set unique constraint name if null before sending to listener
+        if (uniqueConstraintCols != null) {
+          for (int i = 0; i < uniqueConstraintCols.size(); i++) {
+            if (uniqueConstraintCols.get(i).getUk_name() == null) {
+              uniqueConstraintCols.get(i).setUk_name(constraintNames.get(i));
+            }
+          }
+        }
+        if (transactionalListeners.size() > 0) {
+          if (uniqueConstraintCols != null && uniqueConstraintCols.size() > 0) 
{
+            AddUniqueConstraintEvent addUniqueConstraintEvent = new 
AddUniqueConstraintEvent(uniqueConstraintCols, true, this);
+            for (MetaStoreEventListener transactionalListener : 
transactionalListeners) {
+              
transactionalListener.onAddUniqueConstraint(addUniqueConstraintEvent);
+            }
+          }
+        }
+        success = ms.commitTransaction();
       } catch (Exception e) {
         ex = e;
         if (e instanceof MetaException) {
@@ -1741,6 +1895,14 @@ public class HiveMetaStore extends ThriftHiveMetastore {
           throw newMetaException(e);
         }
       } finally {
+        if (!success) {
+          ms.rollbackTransaction();
+        } else if (uniqueConstraintCols != null && uniqueConstraintCols.size() 
> 0) {
+          for (MetaStoreEventListener listener : listeners) {
+            AddUniqueConstraintEvent addUniqueConstraintEvent = new 
AddUniqueConstraintEvent(uniqueConstraintCols, true, this);
+            listener.onAddUniqueConstraint(addUniqueConstraintEvent);
+          }
+        }
         endFunction("add_unique_constraint", success, ex, constraintName);
       }
     }
@@ -1754,9 +1916,27 @@ public class HiveMetaStore extends ThriftHiveMetastore {
       startFunction("add_not_null_constraint", ": " + constraintName);
       boolean success = false;
       Exception ex = null;
+      RawStore ms = getMS();
       try {
-        getMS().addNotNullConstraints(notNullConstraintCols);
-        success = true;
+        ms.openTransaction();
+        List<String> constraintNames = 
ms.addNotNullConstraints(notNullConstraintCols);
+        // Set not null constraint name if null before sending to listener
+        if (notNullConstraintCols != null) {
+          for (int i = 0; i < notNullConstraintCols.size(); i++) {
+            if (notNullConstraintCols.get(i).getNn_name() == null) {
+              notNullConstraintCols.get(i).setNn_name(constraintNames.get(i));
+            }
+          }
+        }
+        if (transactionalListeners.size() > 0) {
+          if (notNullConstraintCols != null && notNullConstraintCols.size() > 
0) {
+            AddNotNullConstraintEvent addNotNullConstraintEvent = new 
AddNotNullConstraintEvent(notNullConstraintCols, true, this);
+            for (MetaStoreEventListener transactionalListener : 
transactionalListeners) {
+              
transactionalListener.onAddNotNullConstraint(addNotNullConstraintEvent);
+            }
+          }
+        }
+        success = ms.commitTransaction();
       } catch (Exception e) {
         ex = e;
         if (e instanceof MetaException) {
@@ -1767,6 +1947,14 @@ public class HiveMetaStore extends ThriftHiveMetastore {
           throw newMetaException(e);
         }
       } finally {
+        if (!success) {
+          ms.rollbackTransaction();
+        } else if (notNullConstraintCols != null && 
notNullConstraintCols.size() > 0) {
+          for (MetaStoreEventListener listener : listeners) {
+            AddNotNullConstraintEvent addNotNullConstraintEvent = new 
AddNotNullConstraintEvent(notNullConstraintCols, true, this);
+            listener.onAddNotNullConstraint(addNotNullConstraintEvent);
+          }
+        }
         endFunction("add_not_null_constraint", success, ex, constraintName);
       }
     }

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java
index c2594f7..5a72082 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java
@@ -23,7 +23,11 @@ import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.common.classification.InterfaceAudience;
 import org.apache.hadoop.hive.common.classification.InterfaceStability;
 import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.events.AddForeignKeyEvent;
 import org.apache.hadoop.hive.metastore.events.AddIndexEvent;
+import org.apache.hadoop.hive.metastore.events.AddNotNullConstraintEvent;
+import org.apache.hadoop.hive.metastore.events.AddPrimaryKeyEvent;
+import org.apache.hadoop.hive.metastore.events.AddUniqueConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.AlterIndexEvent;
 import org.apache.hadoop.hive.metastore.events.AddPartitionEvent;
 import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent;
@@ -32,6 +36,7 @@ import 
org.apache.hadoop.hive.metastore.events.ConfigChangeEvent;
 import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent;
 import org.apache.hadoop.hive.metastore.events.CreateFunctionEvent;
 import org.apache.hadoop.hive.metastore.events.CreateTableEvent;
+import org.apache.hadoop.hive.metastore.events.DropConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent;
 import org.apache.hadoop.hive.metastore.events.DropFunctionEvent;
 import org.apache.hadoop.hive.metastore.events.DropIndexEvent;
@@ -172,6 +177,41 @@ public abstract class MetaStoreEventListener implements 
Configurable {
 
   }
 
+  /**
+   * @param addPrimaryKeyEvent add primary key event
+   * @throws MetaException
+   */
+  public void onAddPrimaryKey(AddPrimaryKeyEvent addPrimaryKeyEvent) throws 
MetaException {
+  }
+
+  /**
+   * @param addForeignKeyEvent add foreign key event
+   * @throws MetaException
+   */
+  public void onAddForeignKey(AddForeignKeyEvent addForeignKeyEvent) throws 
MetaException {
+  }
+
+  /**
+   * @param addUniqueConstraintEvent add unique constraint event
+   * @throws MetaException
+   */
+  public void onAddUniqueConstraint(AddUniqueConstraintEvent 
addUniqueConstraintEvent) throws MetaException {
+  }
+
+  /**
+   * @param addNotNullConstraintEvent add not null constraint event
+   * @throws MetaException
+   */
+  public void onAddNotNullConstraint(AddNotNullConstraintEvent 
addNotNullConstraintEvent) throws MetaException {
+  }
+
+  /**
+   * @param dropConstraintEvent drop constraint event
+   * @throws MetaException
+   */
+  public void onDropConstraint(DropConstraintEvent dropConstraintEvent) throws 
MetaException {
+  }
+
   @Override
   public Configuration getConf() {
     return this.conf;

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreListenerNotifier.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreListenerNotifier.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreListenerNotifier.java
index 37327f8..75dc89c 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreListenerNotifier.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreListenerNotifier.java
@@ -24,8 +24,12 @@ import com.google.common.collect.Maps;
 import org.apache.hadoop.hive.common.classification.InterfaceAudience.Private;
 import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
 import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.events.AddForeignKeyEvent;
 import org.apache.hadoop.hive.metastore.events.AddIndexEvent;
+import org.apache.hadoop.hive.metastore.events.AddNotNullConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.AddPartitionEvent;
+import org.apache.hadoop.hive.metastore.events.AddPrimaryKeyEvent;
+import org.apache.hadoop.hive.metastore.events.AddUniqueConstraintEvent;
 import org.apache.hadoop.hive.metastore.events.AlterIndexEvent;
 import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent;
 import org.apache.hadoop.hive.metastore.events.AlterTableEvent;
@@ -141,6 +145,30 @@ public class MetaStoreListenerNotifier {
               listener.onAlterIndex((AlterIndexEvent)event);
             }
           })
+          .put(EventType.ADD_PRIMARYKEY, new EventNotifier() {
+            @Override
+            public void notify(MetaStoreEventListener listener, ListenerEvent 
event) throws MetaException {
+              listener.onAddPrimaryKey((AddPrimaryKeyEvent)event);
+            }
+          })
+          .put(EventType.ADD_FOREIGNKEY, new EventNotifier() {
+            @Override
+            public void notify(MetaStoreEventListener listener, ListenerEvent 
event) throws MetaException {
+              listener.onAddForeignKey((AddForeignKeyEvent)event);
+            }
+          })
+          .put(EventType.ADD_UNIQUECONSTRAINT, new EventNotifier() {
+            @Override
+            public void notify(MetaStoreEventListener listener, ListenerEvent 
event) throws MetaException {
+              listener.onAddUniqueConstraint((AddUniqueConstraintEvent)event);
+            }
+          })
+          .put(EventType.ADD_NOTNULLCONSTRAINT, new EventNotifier() {
+            @Override
+            public void notify(MetaStoreEventListener listener, ListenerEvent 
event) throws MetaException {
+              
listener.onAddNotNullConstraint((AddNotNullConstraintEvent)event);
+            }
+          })
           .build()
   );
 

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java 
b/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
index 043e2df..30f9e57 100644
--- a/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
+++ b/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
@@ -188,6 +188,7 @@ import com.google.common.collect.Maps;
 public class ObjectStore implements RawStore, Configurable {
   private static Properties prop = null;
   private static PersistenceManagerFactory pmf = null;
+  private static boolean forTwoMetastoreTesting = false;
 
   private static Lock pmfPropLock = new ReentrantLock();
   /**
@@ -290,8 +291,10 @@ public class ObjectStore implements RawStore, Configurable 
{
       if (propsChanged) {
         if (pmf != null){
           clearOutPmfClassLoaderCache(pmf);
-          // close the underlying connection pool to avoid leaks
-          pmf.close();
+          if (!forTwoMetastoreTesting) {
+            // close the underlying connection pool to avoid leaks
+            pmf.close();
+          }
         }
         pmf = null;
         prop = null;
@@ -979,7 +982,7 @@ public class ObjectStore implements RawStore, Configurable {
   }
 
   @Override
-  public void createTableWithConstraints(Table tbl,
+  public List<String> createTableWithConstraints(Table tbl,
     List<SQLPrimaryKey> primaryKeys, List<SQLForeignKey> foreignKeys,
     List<SQLUniqueConstraint> uniqueConstraints,
     List<SQLNotNullConstraint> notNullConstraints)
@@ -991,11 +994,12 @@ public class ObjectStore implements RawStore, 
Configurable {
       // Add constraints.
       // We need not do a deep retrieval of the Table Column Descriptor while 
persisting the
       // constraints since this transaction involving create table is not yet 
committed.
-      addPrimaryKeys(primaryKeys, false);
-      addForeignKeys(foreignKeys, false);
-      addUniqueConstraints(uniqueConstraints, false);
-      addNotNullConstraints(notNullConstraints, false);
+      List<String> constraintNames = addPrimaryKeys(primaryKeys, false);
+      constraintNames.addAll(addForeignKeys(foreignKeys, false));
+      constraintNames.addAll(addUniqueConstraints(uniqueConstraints, false));
+      constraintNames.addAll(addNotNullConstraints(notNullConstraints, false));
       success = commitTransaction();
+      return constraintNames;
     } finally {
       if (!success) {
         rollbackTransaction();
@@ -3506,7 +3510,7 @@ public class ObjectStore implements RawStore, 
Configurable {
   }
 
   private String generateConstraintName(String... parameters) throws 
MetaException {
-    int hashcode = ArrayUtils.toString(parameters).hashCode();
+    int hashcode = ArrayUtils.toString(parameters).hashCode() & 0xfffffff;
     int counter = 0;
     final int MAX_RETRIES = 10;
     while (counter < MAX_RETRIES) {
@@ -3520,9 +3524,9 @@ public class ObjectStore implements RawStore, 
Configurable {
   }
 
   @Override
-  public void addForeignKeys(
+  public List<String> addForeignKeys(
     List<SQLForeignKey> fks) throws InvalidObjectException, MetaException {
-   addForeignKeys(fks, true);
+   return addForeignKeys(fks, true);
   }
 
   @Override
@@ -3596,9 +3600,10 @@ public class ObjectStore implements RawStore, 
Configurable {
     return null;
   }
 
-  private void addForeignKeys(
+  private List<String> addForeignKeys(
     List<SQLForeignKey> fks, boolean retrieveCD) throws InvalidObjectException,
     MetaException {
+    List<String> fkNames = new ArrayList<String>();
     List<MConstraint> mpkfks = new ArrayList<MConstraint>();
     String currentConstraintName = null;
 
@@ -3653,6 +3658,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       } else {
         currentConstraintName = 
HiveStringUtils.normalizeIdentifier(fks.get(i).getFk_name());
       }
+      fkNames.add(currentConstraintName);
       Integer updateRule = fks.get(i).getUpdate_rule();
       Integer deleteRule = fks.get(i).getDelete_rule();
       int enableValidateRely = (fks.get(i).isEnable_cstr() ? 4 : 0) +
@@ -3674,16 +3680,18 @@ public class ObjectStore implements RawStore, 
Configurable {
       mpkfks.add(mpkfk);
     }
     pm.makePersistentAll(mpkfks);
+    return fkNames;
   }
 
   @Override
-  public void addPrimaryKeys(List<SQLPrimaryKey> pks) throws 
InvalidObjectException,
+  public List<String> addPrimaryKeys(List<SQLPrimaryKey> pks) throws 
InvalidObjectException,
     MetaException {
-    addPrimaryKeys(pks, true);
+    return addPrimaryKeys(pks, true);
   }
 
-  private void addPrimaryKeys(List<SQLPrimaryKey> pks, boolean retrieveCD) 
throws InvalidObjectException,
+  private List<String> addPrimaryKeys(List<SQLPrimaryKey> pks, boolean 
retrieveCD) throws InvalidObjectException,
     MetaException {
+    List<String> pkNames = new ArrayList<String>();
     List<MConstraint> mpks = new ArrayList<MConstraint>();
     String constraintName = null;
 
@@ -3719,7 +3727,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       } else {
         constraintName = 
HiveStringUtils.normalizeIdentifier(pks.get(i).getPk_name());
       }
-
+      pkNames.add(constraintName);
       int enableValidateRely = (pks.get(i).isEnable_cstr() ? 4 : 0) +
       (pks.get(i).isValidate_cstr() ? 2 : 0) + (pks.get(i).isRely_cstr() ? 1 : 
0);
       MConstraint mpk = new MConstraint(
@@ -3738,16 +3746,18 @@ public class ObjectStore implements RawStore, 
Configurable {
       mpks.add(mpk);
     }
     pm.makePersistentAll(mpks);
+    return pkNames;
   }
 
   @Override
-  public void addUniqueConstraints(List<SQLUniqueConstraint> uks)
+  public List<String> addUniqueConstraints(List<SQLUniqueConstraint> uks)
           throws InvalidObjectException, MetaException {
-    addUniqueConstraints(uks, true);
+    return addUniqueConstraints(uks, true);
   }
 
-  private void addUniqueConstraints(List<SQLUniqueConstraint> uks, boolean 
retrieveCD)
+  private List<String> addUniqueConstraints(List<SQLUniqueConstraint> uks, 
boolean retrieveCD)
           throws InvalidObjectException, MetaException {
+    List<String> ukNames = new ArrayList<String>();
     List<MConstraint> cstrs = new ArrayList<MConstraint>();
     String constraintName = null;
 
@@ -3777,6 +3787,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       } else {
         constraintName = 
HiveStringUtils.normalizeIdentifier(uks.get(i).getUk_name());
       }
+      ukNames.add(constraintName);
 
       int enableValidateRely = (uks.get(i).isEnable_cstr() ? 4 : 0) +
           (uks.get(i).isValidate_cstr() ? 2 : 0) + (uks.get(i).isRely_cstr() ? 
1 : 0);
@@ -3796,16 +3807,18 @@ public class ObjectStore implements RawStore, 
Configurable {
       cstrs.add(muk);
     }
     pm.makePersistentAll(cstrs);
+    return ukNames;
   }
 
   @Override
-  public void addNotNullConstraints(List<SQLNotNullConstraint> nns)
+  public List<String> addNotNullConstraints(List<SQLNotNullConstraint> nns)
           throws InvalidObjectException, MetaException {
-    addNotNullConstraints(nns, true);
+    return addNotNullConstraints(nns, true);
   }
 
-  private void addNotNullConstraints(List<SQLNotNullConstraint> nns, boolean 
retrieveCD)
+  private List<String> addNotNullConstraints(List<SQLNotNullConstraint> nns, 
boolean retrieveCD)
           throws InvalidObjectException, MetaException {
+    List<String> nnNames = new ArrayList<String>();
     List<MConstraint> cstrs = new ArrayList<MConstraint>();
     String constraintName = null;
 
@@ -3833,6 +3846,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       } else {
         constraintName = 
HiveStringUtils.normalizeIdentifier(nns.get(i).getNn_name());
       }
+      nnNames.add(constraintName);
 
       int enableValidateRely = (nns.get(i).isEnable_cstr() ? 4 : 0) +
           (nns.get(i).isValidate_cstr() ? 2 : 0) + (nns.get(i).isRely_cstr() ? 
1 : 0);
@@ -3852,6 +3866,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       cstrs.add(muk);
     }
     pm.makePersistentAll(cstrs);
+    return nnNames;
   }
 
   @Override
@@ -8869,4 +8884,13 @@ public class ObjectStore implements RawStore, 
Configurable {
       }
     }
   }
+
+  /**
+   * To make possible to run multiple metastore in unit test
+   * @param twoMetastoreTesting if we are using multiple metastore in unit test
+   */
+  @VisibleForTesting
+  public static void setTwoMetastoreTesting(boolean twoMetastoreTesting) {
+    forTwoMetastoreTesting = twoMetastoreTesting;
+  }
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java
----------------------------------------------------------------------
diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java 
b/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java
index 34f12a6..a2ae4c5 100644
--- a/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java
+++ b/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java
@@ -710,19 +710,19 @@ public interface RawStore extends Configurable {
   public abstract List<SQLNotNullConstraint> getNotNullConstraints(String 
db_name,
     String tbl_name) throws MetaException;
 
-  void createTableWithConstraints(Table tbl, List<SQLPrimaryKey> primaryKeys,
+  List<String> createTableWithConstraints(Table tbl, List<SQLPrimaryKey> 
primaryKeys,
     List<SQLForeignKey> foreignKeys, List<SQLUniqueConstraint> 
uniqueConstraints,
     List<SQLNotNullConstraint> notNullConstraints) throws 
InvalidObjectException, MetaException;
 
   void dropConstraint(String dbName, String tableName, String constraintName) 
throws NoSuchObjectException;
 
-  void addPrimaryKeys(List<SQLPrimaryKey> pks) throws InvalidObjectException, 
MetaException;
+  List<String> addPrimaryKeys(List<SQLPrimaryKey> pks) throws 
InvalidObjectException, MetaException;
 
-  void addForeignKeys(List<SQLForeignKey> fks) throws InvalidObjectException, 
MetaException;
+  List<String> addForeignKeys(List<SQLForeignKey> fks) throws 
InvalidObjectException, MetaException;
 
-  void addUniqueConstraints(List<SQLUniqueConstraint> uks) throws 
InvalidObjectException, MetaException;
+  List<String> addUniqueConstraints(List<SQLUniqueConstraint> uks) throws 
InvalidObjectException, MetaException;
 
-  void addNotNullConstraints(List<SQLNotNullConstraint> nns) throws 
InvalidObjectException, MetaException;
+  List<String> addNotNullConstraints(List<SQLNotNullConstraint> nns) throws 
InvalidObjectException, MetaException;
 
   /**
    * Gets the unique id of the backing datastore for the metadata

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java 
b/metastore/src/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java
index fb98ccf..ce98a6e 100644
--- a/metastore/src/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java
+++ b/metastore/src/java/org/apache/hadoop/hive/metastore/cache/CachedStore.java
@@ -1895,16 +1895,17 @@ public class CachedStore implements RawStore, 
Configurable {
   }
 
   @Override
-  public void createTableWithConstraints(Table tbl,
+  public List<String> createTableWithConstraints(Table tbl,
       List<SQLPrimaryKey> primaryKeys, List<SQLForeignKey> foreignKeys,
       List<SQLUniqueConstraint> uniqueConstraints,
       List<SQLNotNullConstraint> notNullConstraints)
       throws InvalidObjectException, MetaException {
     // TODO constraintCache
-    rawStore.createTableWithConstraints(tbl, primaryKeys, foreignKeys,
+    List<String> constraintNames = rawStore.createTableWithConstraints(tbl, 
primaryKeys, foreignKeys,
             uniqueConstraints, notNullConstraints);
     
SharedCache.addTableToCache(HiveStringUtils.normalizeIdentifier(tbl.getDbName()),
         HiveStringUtils.normalizeIdentifier(tbl.getTableName()), tbl);
+    return constraintNames;
   }
 
   @Override
@@ -1915,31 +1916,31 @@ public class CachedStore implements RawStore, 
Configurable {
   }
 
   @Override
-  public void addPrimaryKeys(List<SQLPrimaryKey> pks)
+  public List<String> addPrimaryKeys(List<SQLPrimaryKey> pks)
       throws InvalidObjectException, MetaException {
     // TODO constraintCache
-    rawStore.addPrimaryKeys(pks);
+    return rawStore.addPrimaryKeys(pks);
   }
 
   @Override
-  public void addForeignKeys(List<SQLForeignKey> fks)
+  public List<String> addForeignKeys(List<SQLForeignKey> fks)
       throws InvalidObjectException, MetaException {
     // TODO constraintCache
-    rawStore.addForeignKeys(fks);
+    return rawStore.addForeignKeys(fks);
   }
 
   @Override
-  public void addUniqueConstraints(List<SQLUniqueConstraint> uks)
+  public List<String> addUniqueConstraints(List<SQLUniqueConstraint> uks)
       throws InvalidObjectException, MetaException {
     // TODO constraintCache
-    rawStore.addUniqueConstraints(uks);
+    return rawStore.addUniqueConstraints(uks);
   }
 
   @Override
-  public void addNotNullConstraints(List<SQLNotNullConstraint> nns)
+  public List<String> addNotNullConstraints(List<SQLNotNullConstraint> nns)
       throws InvalidObjectException, MetaException {
     // TODO constraintCache
-    rawStore.addNotNullConstraints(nns);
+    return rawStore.addNotNullConstraints(nns);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddForeignKeyEvent.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddForeignKeyEvent.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddForeignKeyEvent.java
new file mode 100644
index 0000000..1dc9588
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddForeignKeyEvent.java
@@ -0,0 +1,37 @@
+/**
+ * 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.hadoop.hive.metastore.events;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler;
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+
+public class AddForeignKeyEvent extends ListenerEvent {
+  private final List<SQLForeignKey> fks;
+
+  public AddForeignKeyEvent(List<SQLForeignKey> fks, boolean status, 
HMSHandler handler) {
+    super(status, handler);
+    this.fks = fks;
+  }
+
+  public List<SQLForeignKey> getForeignKeyCols() {
+    return fks;
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddNotNullConstraintEvent.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddNotNullConstraintEvent.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddNotNullConstraintEvent.java
new file mode 100644
index 0000000..c01083c
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddNotNullConstraintEvent.java
@@ -0,0 +1,37 @@
+/**
+ * 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.hadoop.hive.metastore.events;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler;
+import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint;
+
+public class AddNotNullConstraintEvent extends ListenerEvent {
+  private final List<SQLNotNullConstraint> nns;
+
+  public AddNotNullConstraintEvent(List<SQLNotNullConstraint> nns, boolean 
status, HMSHandler handler) {
+    super(status, handler);
+    this.nns = nns;
+  }
+
+  public List<SQLNotNullConstraint> getNotNullConstraintCols() {
+    return nns;
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddPrimaryKeyEvent.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddPrimaryKeyEvent.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddPrimaryKeyEvent.java
new file mode 100644
index 0000000..cb0f562
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddPrimaryKeyEvent.java
@@ -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.hadoop.hive.metastore.events;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler;
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
+
+public class AddPrimaryKeyEvent extends ListenerEvent {
+
+  private final List<SQLPrimaryKey> pks;
+
+  public AddPrimaryKeyEvent(List<SQLPrimaryKey> pks, boolean status, 
HMSHandler handler) {
+    super(status, handler);
+    this.pks = pks;
+  }
+
+  public List<SQLPrimaryKey> getPrimaryKeyCols() {
+    return pks;
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddUniqueConstraintEvent.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddUniqueConstraintEvent.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddUniqueConstraintEvent.java
new file mode 100644
index 0000000..c02a309
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/AddUniqueConstraintEvent.java
@@ -0,0 +1,37 @@
+/**
+ * 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.hadoop.hive.metastore.events;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler;
+import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint;
+
+public class AddUniqueConstraintEvent extends ListenerEvent {
+  private final List<SQLUniqueConstraint> uks;
+
+  public AddUniqueConstraintEvent(List<SQLUniqueConstraint> uks, boolean 
status, HMSHandler handler) {
+    super(status, handler);
+    this.uks = uks;
+  }
+
+  public List<SQLUniqueConstraint> getUniqueConstraintCols() {
+    return uks;
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/events/DropConstraintEvent.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/events/DropConstraintEvent.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/DropConstraintEvent.java
new file mode 100644
index 0000000..5396863
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/events/DropConstraintEvent.java
@@ -0,0 +1,47 @@
+/**
+ * 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.hadoop.hive.metastore.events;
+
+import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler;
+
+public class DropConstraintEvent extends ListenerEvent {
+
+  private final String dbName;
+  private final String tableName;
+  private final String constraintName;
+  public DropConstraintEvent(String dbName, String tableName, String 
constraintName,
+      boolean status, HMSHandler handler) {
+    super(status, handler);
+    this.dbName = dbName;
+    this.tableName = tableName;
+    this.constraintName = constraintName;
+  }
+
+  public String getDbName() {
+    return dbName;
+  }
+
+  public String getTableName() {
+    return tableName;
+  }
+
+  public String getConstraintName() {
+    return constraintName;
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddForeignKeyMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddForeignKeyMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddForeignKeyMessage.java
new file mode 100644
index 0000000..2eb14a1
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddForeignKeyMessage.java
@@ -0,0 +1,36 @@
+/**
+ * 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.hadoop.hive.metastore.messaging;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+
+public abstract class AddForeignKeyMessage extends EventMessage {
+  protected AddForeignKeyMessage() {
+    super(EventType.ADD_FOREIGNKEY);
+  }
+
+  /**
+   * Getter for list of foreign keys.
+   * @return List of SQLForeignKey
+   */
+  public abstract List<SQLForeignKey> getForeignKeys() throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddNotNullConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddNotNullConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddNotNullConstraintMessage.java
new file mode 100644
index 0000000..28ee610
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddNotNullConstraintMessage.java
@@ -0,0 +1,36 @@
+/**
+ * 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.hadoop.hive.metastore.messaging;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint;
+
+public abstract class AddNotNullConstraintMessage extends EventMessage {
+  protected AddNotNullConstraintMessage() {
+    super(EventType.ADD_NOTNULLCONSTRAINT);
+  }
+
+  /**
+   * Getter for list of not null constraints.
+   * @return List of SQLNotNullConstraint
+   */
+  public abstract List<SQLNotNullConstraint> getNotNullConstraints() throws 
Exception;
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddPrimaryKeyMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddPrimaryKeyMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddPrimaryKeyMessage.java
new file mode 100644
index 0000000..0e899ad
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddPrimaryKeyMessage.java
@@ -0,0 +1,35 @@
+/**
+ * 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.hadoop.hive.metastore.messaging;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
+
+public abstract class AddPrimaryKeyMessage extends EventMessage {
+  protected AddPrimaryKeyMessage() {
+    super(EventType.ADD_PRIMARYKEY);
+  }
+
+  /**
+   * Getter for list of primary keys.
+   * @return List of SQLPrimaryKey
+   */
+  public abstract List<SQLPrimaryKey> getPrimaryKeys() throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddUniqueConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddUniqueConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddUniqueConstraintMessage.java
new file mode 100644
index 0000000..8072d84f
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/AddUniqueConstraintMessage.java
@@ -0,0 +1,36 @@
+/**
+ * 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.hadoop.hive.metastore.messaging;
+
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint;
+
+public abstract class AddUniqueConstraintMessage extends EventMessage {
+  protected AddUniqueConstraintMessage() {
+    super(EventType.ADD_UNIQUECONSTRAINT);
+  }
+
+  /**
+   * Getter for list of unique constraints.
+   * @return List of SQLUniqueConstraint
+   */
+  public abstract List<SQLUniqueConstraint> getUniqueConstraints() throws 
Exception;
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/DropConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/DropConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/DropConstraintMessage.java
new file mode 100644
index 0000000..6e691e9
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/DropConstraintMessage.java
@@ -0,0 +1,29 @@
+/**
+ * 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.hadoop.hive.metastore.messaging;
+
+public abstract class DropConstraintMessage extends EventMessage {
+  protected DropConstraintMessage() {
+    super(EventType.DROP_CONSTRAINT);
+  }
+
+  public abstract String getTable();
+
+  public abstract String getConstraint();
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/EventMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/EventMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/EventMessage.java
index 1ec0de0..3b37cb3 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/EventMessage.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/EventMessage.java
@@ -43,7 +43,12 @@ public abstract class EventMessage {
     DROP_FUNCTION(MessageFactory.DROP_FUNCTION_EVENT),
     CREATE_INDEX(MessageFactory.CREATE_INDEX_EVENT),
     DROP_INDEX(MessageFactory.DROP_INDEX_EVENT),
-    ALTER_INDEX(MessageFactory.ALTER_INDEX_EVENT);
+    ALTER_INDEX(MessageFactory.ALTER_INDEX_EVENT),
+    ADD_PRIMARYKEY(MessageFactory.ADD_PRIMARYKEY_EVENT),
+    ADD_FOREIGNKEY(MessageFactory.ADD_FOREIGNKEY_EVENT),
+    ADD_UNIQUECONSTRAINT(MessageFactory.ADD_UNIQUECONSTRAINT_EVENT),
+    ADD_NOTNULLCONSTRAINT(MessageFactory.ADD_NOTNULLCONSTRAINT_EVENT),
+    DROP_CONSTRAINT(MessageFactory.DROP_CONSTRAINT_EVENT);
 
     private String typeString;
 

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
index 515c455..50a2728 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageDeserializer.java
@@ -58,7 +58,16 @@ public abstract class MessageDeserializer {
       return getAlterIndexMessage(messageBody);
     case INSERT:
       return getInsertMessage(messageBody);
-
+    case ADD_PRIMARYKEY:
+      return getAddPrimaryKeyMessage(messageBody);
+    case ADD_FOREIGNKEY:
+      return getAddForeignKeyMessage(messageBody);
+    case ADD_UNIQUECONSTRAINT:
+      return getAddUniqueConstraintMessage(messageBody);
+    case ADD_NOTNULLCONSTRAINT:
+      return getAddNotNullConstraintMessage(messageBody);
+    case DROP_CONSTRAINT:
+      return getDropConstraintMessage(messageBody);
     default:
       throw new IllegalArgumentException("Unsupported event-type: " + 
eventTypeString);
     }
@@ -140,6 +149,31 @@ public abstract class MessageDeserializer {
    */
   public abstract InsertMessage getInsertMessage(String messageBody);
 
+  /**
+   * Method to de-serialize AddPrimaryKeyMessage instance.
+   */
+  public abstract AddPrimaryKeyMessage getAddPrimaryKeyMessage(String 
messageBody);
+
+  /**
+   * Method to de-serialize AddForeignKeyMessage instance.
+   */
+  public abstract AddForeignKeyMessage getAddForeignKeyMessage(String 
messageBody);
+
+  /**
+   * Method to de-serialize AddUniqueConstraintMessage instance.
+   */
+  public abstract AddUniqueConstraintMessage 
getAddUniqueConstraintMessage(String messageBody);
+
+  /**
+   * Method to de-serialize AddNotNullConstraintMessage instance.
+   */
+  public abstract AddNotNullConstraintMessage 
getAddNotNullConstraintMessage(String messageBody);
+
+  /**
+   * Method to de-serialize DropConstraintMessage instance.
+   */
+  public abstract DropConstraintMessage getDropConstraintMessage(String 
messageBody);
+
   // Protection against construction.
   protected MessageDeserializer() {}
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageFactory.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageFactory.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageFactory.java
index 9437e8b..e639e48 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageFactory.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/MessageFactory.java
@@ -25,10 +25,15 @@ import org.apache.hadoop.hive.metastore.api.Database;
 import org.apache.hadoop.hive.metastore.api.Function;
 import org.apache.hadoop.hive.metastore.api.Index;
 import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint;
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
+import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint;
 import org.apache.hadoop.hive.metastore.api.Table;
 import org.apache.hadoop.util.ReflectionUtils;
 
 import java.util.Iterator;
+import java.util.List;
 
 /**
  * Abstract Factory for the construction of HCatalog message instances.
@@ -50,7 +55,11 @@ public abstract class MessageFactory {
   public static final String CREATE_INDEX_EVENT = "CREATE_INDEX";
   public static final String DROP_INDEX_EVENT = "DROP_INDEX";
   public static final String ALTER_INDEX_EVENT = "ALTER_INDEX";
-
+  public static final String ADD_PRIMARYKEY_EVENT = "ADD_PRIMARYKEY";
+  public static final String ADD_FOREIGNKEY_EVENT = "ADD_FOREIGNKEY";
+  public static final String ADD_UNIQUECONSTRAINT_EVENT = 
"ADD_UNIQUECONSTRAINT";
+  public static final String ADD_NOTNULLCONSTRAINT_EVENT = 
"ADD_NOTNULLCONSTRAINT";
+  public static final String DROP_CONSTRAINT_EVENT = "DROP_CONSTRAINT";
 
   private static MessageFactory instance = null;
 
@@ -237,4 +246,46 @@ public abstract class MessageFactory {
    */
   public abstract InsertMessage buildInsertMessage(Table tableObj, Partition 
ptnObj,
                                                    boolean replace, 
Iterator<String> files);
+
+  /***
+   * Factory method for building add primary key message
+   *
+   * @param pks list of primary keys
+   * @return instance of AddPrimaryKeyMessage
+   */
+  public abstract AddPrimaryKeyMessage 
buildAddPrimaryKeyMessage(List<SQLPrimaryKey> pks);
+
+  /***
+   * Factory method for building add foreign key message
+   *
+   * @param fks list of foreign keys
+   * @return instance of AddForeignKeyMessage
+   */
+  public abstract AddForeignKeyMessage 
buildAddForeignKeyMessage(List<SQLForeignKey> fks);
+
+  /***
+   * Factory method for building add unique constraint message
+   *
+   * @param uks list of unique constraints
+   * @return instance of SQLUniqueConstraint
+   */
+  public abstract AddUniqueConstraintMessage 
buildAddUniqueConstraintMessage(List<SQLUniqueConstraint> uks);
+
+  /***
+   * Factory method for building add not null constraint message
+   *
+   * @param nns list of not null constraints
+   * @return instance of SQLNotNullConstraint
+   */
+  public abstract AddNotNullConstraintMessage 
buildAddNotNullConstraintMessage(List<SQLNotNullConstraint> nns);
+
+  /***
+   * Factory method for building drop constraint message
+   * @param dbName
+   * @param tableName
+   * @param constraintName
+   * @return
+   */
+  public abstract DropConstraintMessage buildDropConstraintMessage(String 
dbName, String tableName,
+      String constraintName);
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddForeignKeyMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddForeignKeyMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddForeignKeyMessage.java
new file mode 100644
index 0000000..9c04c15
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddForeignKeyMessage.java
@@ -0,0 +1,101 @@
+/**
+ * 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.hadoop.hive.metastore.messaging.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+import org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage;
+import org.apache.thrift.TException;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * JSON implementation of AddForeignKeyMessage
+ */
+public class JSONAddForeignKeyMessage extends AddForeignKeyMessage {
+
+  @JsonProperty
+  String server, servicePrincipal;
+
+  @JsonProperty
+  Long timestamp;
+
+  @JsonProperty
+  List<String> foreignKeyListJson;
+
+  /**
+   * Default constructor, needed for Jackson.
+   */
+  public JSONAddForeignKeyMessage() {
+  }
+
+  public JSONAddForeignKeyMessage(String server, String servicePrincipal, 
List<SQLForeignKey> fks,
+      Long timestamp) {
+    this.server = server;
+    this.servicePrincipal = servicePrincipal;
+    this.timestamp = timestamp;
+    this.foreignKeyListJson = new ArrayList<String>();
+    try {
+      for (SQLForeignKey pk : fks) {
+        foreignKeyListJson.add(JSONMessageFactory.createForeignKeyObjJson(pk));
+      }
+    } catch (TException e) {
+      throw new IllegalArgumentException("Could not serialize: ", e);
+    }
+  }
+
+  @Override
+  public String getServer() {
+    return server;
+  }
+
+  @Override
+  public String getServicePrincipal() {
+    return servicePrincipal;
+  }
+
+  @Override
+  public String getDB() {
+    return null;
+  }
+
+  @Override
+  public Long getTimestamp() {
+    return timestamp;
+  }
+
+  @Override
+  public List<SQLForeignKey> getForeignKeys() throws Exception {
+    List<SQLForeignKey> fks = new ArrayList<SQLForeignKey>();
+    for (String pkJson : foreignKeyListJson) {
+      fks.add((SQLForeignKey)JSONMessageFactory.getTObj(pkJson, 
SQLForeignKey.class));
+    }
+    return fks;
+  }
+
+  @Override
+  public String toString() {
+    try {
+      return JSONMessageDeserializer.mapper.writeValueAsString(this);
+    } catch (Exception exception) {
+      throw new IllegalArgumentException("Could not serialize: ", exception);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddNotNullConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddNotNullConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddNotNullConstraintMessage.java
new file mode 100644
index 0000000..acc94ce
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddNotNullConstraintMessage.java
@@ -0,0 +1,79 @@
+package org.apache.hadoop.hive.metastore.messaging.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint;
+import org.apache.hadoop.hive.metastore.messaging.AddNotNullConstraintMessage;
+import org.apache.thrift.TException;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+public class JSONAddNotNullConstraintMessage extends 
AddNotNullConstraintMessage {
+  @JsonProperty
+  String server, servicePrincipal;
+
+  @JsonProperty
+  Long timestamp;
+
+  @JsonProperty
+  List<String> notNullConstraintListJson;
+
+  /**
+   * Default constructor, needed for Jackson.
+   */
+  public JSONAddNotNullConstraintMessage() {
+  }
+
+  public JSONAddNotNullConstraintMessage(String server, String 
servicePrincipal, List<SQLNotNullConstraint> nns,
+      Long timestamp) {
+    this.server = server;
+    this.servicePrincipal = servicePrincipal;
+    this.timestamp = timestamp;
+    this.notNullConstraintListJson = new ArrayList<String>();
+    try {
+      for (SQLNotNullConstraint nn : nns) {
+        
notNullConstraintListJson.add(JSONMessageFactory.createNotNullConstraintObjJson(nn));
+      }
+    } catch (TException e) {
+      throw new IllegalArgumentException("Could not serialize: ", e);
+    }
+  }
+
+  @Override
+  public String getServer() {
+    return server;
+  }
+
+  @Override
+  public String getServicePrincipal() {
+    return servicePrincipal;
+  }
+
+  @Override
+  public String getDB() {
+    return null;
+  }
+
+  @Override
+  public Long getTimestamp() {
+    return timestamp;
+  }
+
+  @Override
+  public List<SQLNotNullConstraint> getNotNullConstraints() throws Exception {
+    List<SQLNotNullConstraint> nns = new ArrayList<SQLNotNullConstraint>();
+    for (String nnJson : notNullConstraintListJson) {
+      nns.add((SQLNotNullConstraint)JSONMessageFactory.getTObj(nnJson, 
SQLNotNullConstraint.class));
+    }
+    return nns;
+  }
+
+  @Override
+  public String toString() {
+    try {
+      return JSONMessageDeserializer.mapper.writeValueAsString(this);
+    } catch (Exception exception) {
+      throw new IllegalArgumentException("Could not serialize: ", exception);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddPrimaryKeyMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddPrimaryKeyMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddPrimaryKeyMessage.java
new file mode 100644
index 0000000..2551cbf
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddPrimaryKeyMessage.java
@@ -0,0 +1,101 @@
+/**
+ * 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.hadoop.hive.metastore.messaging.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
+import org.apache.hadoop.hive.metastore.messaging.AddPrimaryKeyMessage;
+import org.apache.thrift.TException;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * JSON implementation of AddPrimaryKeyMessage
+ */
+public class JSONAddPrimaryKeyMessage extends AddPrimaryKeyMessage {
+
+  @JsonProperty
+  String server, servicePrincipal;
+
+  @JsonProperty
+  Long timestamp;
+
+  @JsonProperty
+  List<String> primaryKeyListJson;
+
+  /**
+   * Default constructor, needed for Jackson.
+   */
+  public JSONAddPrimaryKeyMessage() {
+  }
+
+  public JSONAddPrimaryKeyMessage(String server, String servicePrincipal, 
List<SQLPrimaryKey> pks,
+      Long timestamp) {
+    this.server = server;
+    this.servicePrincipal = servicePrincipal;
+    this.timestamp = timestamp;
+    this.primaryKeyListJson = new ArrayList<String>();
+    try {
+      for (SQLPrimaryKey pk : pks) {
+        primaryKeyListJson.add(JSONMessageFactory.createPrimaryKeyObjJson(pk));
+      }
+    } catch (TException e) {
+      throw new IllegalArgumentException("Could not serialize: ", e);
+    }
+  }
+
+  @Override
+  public String getServer() {
+    return server;
+  }
+
+  @Override
+  public String getServicePrincipal() {
+    return servicePrincipal;
+  }
+
+  @Override
+  public String getDB() {
+    return null;
+  }
+
+  @Override
+  public Long getTimestamp() {
+    return timestamp;
+  }
+
+  @Override
+  public List<SQLPrimaryKey> getPrimaryKeys() throws Exception {
+    List<SQLPrimaryKey> pks = new ArrayList<SQLPrimaryKey>();
+    for (String pkJson : primaryKeyListJson) {
+      pks.add((SQLPrimaryKey)JSONMessageFactory.getTObj(pkJson, 
SQLPrimaryKey.class));
+    }
+    return pks;
+  }
+
+  @Override
+  public String toString() {
+    try {
+      return JSONMessageDeserializer.mapper.writeValueAsString(this);
+    } catch (Exception exception) {
+      throw new IllegalArgumentException("Could not serialize: ", exception);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddUniqueConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddUniqueConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddUniqueConstraintMessage.java
new file mode 100644
index 0000000..37f6b81
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONAddUniqueConstraintMessage.java
@@ -0,0 +1,98 @@
+/**
+ * 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.hadoop.hive.metastore.messaging.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint;
+import org.apache.hadoop.hive.metastore.messaging.AddUniqueConstraintMessage;
+import org.apache.thrift.TException;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+public class JSONAddUniqueConstraintMessage extends AddUniqueConstraintMessage 
{
+  @JsonProperty
+  String server, servicePrincipal;
+
+  @JsonProperty
+  Long timestamp;
+
+  @JsonProperty
+  List<String> uniqueConstraintListJson;
+
+  /**
+   * Default constructor, needed for Jackson.
+   */
+  public JSONAddUniqueConstraintMessage() {
+  }
+
+  public JSONAddUniqueConstraintMessage(String server, String 
servicePrincipal, List<SQLUniqueConstraint> uks,
+      Long timestamp) {
+    this.server = server;
+    this.servicePrincipal = servicePrincipal;
+    this.timestamp = timestamp;
+    this.uniqueConstraintListJson = new ArrayList<String>();
+    try {
+      for (SQLUniqueConstraint uk : uks) {
+        
uniqueConstraintListJson.add(JSONMessageFactory.createUniqueConstraintObjJson(uk));
+      }
+    } catch (TException e) {
+      throw new IllegalArgumentException("Could not serialize: ", e);
+    }
+  }
+
+  @Override
+  public String getServer() {
+    return server;
+  }
+
+  @Override
+  public String getServicePrincipal() {
+    return servicePrincipal;
+  }
+
+  @Override
+  public String getDB() {
+    return null;
+  }
+
+  @Override
+  public Long getTimestamp() {
+    return timestamp;
+  }
+
+  @Override
+  public List<SQLUniqueConstraint> getUniqueConstraints() throws Exception {
+    List<SQLUniqueConstraint> uks = new ArrayList<SQLUniqueConstraint>();
+    for (String pkJson : uniqueConstraintListJson) {
+      uks.add((SQLUniqueConstraint)JSONMessageFactory.getTObj(pkJson, 
SQLUniqueConstraint.class));
+    }
+    return uks;
+  }
+
+  @Override
+  public String toString() {
+    try {
+      return JSONMessageDeserializer.mapper.writeValueAsString(this);
+    } catch (Exception exception) {
+      throw new IllegalArgumentException("Could not serialize: ", exception);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONDropConstraintMessage.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONDropConstraintMessage.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONDropConstraintMessage.java
new file mode 100644
index 0000000..4d3422b
--- /dev/null
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONDropConstraintMessage.java
@@ -0,0 +1,90 @@
+/**
+ * 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.hadoop.hive.metastore.messaging.json;
+
+import org.apache.hadoop.hive.metastore.messaging.DropConstraintMessage;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * JSON implementation of DropConstraintMessage
+ */
+public class JSONDropConstraintMessage extends DropConstraintMessage {
+
+  @JsonProperty
+  String server, servicePrincipal, dbName, tableName, constraintName;
+
+  @JsonProperty
+  Long timestamp;
+
+  /**
+   * Default constructor, needed for Jackson.
+   */
+  public JSONDropConstraintMessage() {
+  }
+
+  public JSONDropConstraintMessage(String server, String servicePrincipal, 
String dbName,
+      String tableName, String constraintName, Long timestamp) {
+    this.server = server;
+    this.servicePrincipal = servicePrincipal;
+    this.timestamp = timestamp;
+    this.dbName = dbName;
+    this.tableName = tableName;
+    this.constraintName = constraintName;
+  }
+
+  @Override
+  public String getServer() {
+    return server;
+  }
+
+  @Override
+  public String getServicePrincipal() {
+    return servicePrincipal;
+  }
+
+  @Override
+  public String getDB() {
+    return dbName;
+  }
+
+  @Override
+  public String getTable() {
+    return tableName;
+  }
+
+  @Override
+  public String getConstraint() {
+    return constraintName;
+  }
+
+  @Override
+  public Long getTimestamp() {
+    return timestamp;
+  }
+
+  @Override
+  public String toString() {
+    try {
+      return JSONMessageDeserializer.mapper.writeValueAsString(this);
+    } catch (Exception exception) {
+      throw new IllegalArgumentException("Could not serialize: ", exception);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageDeserializer.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageDeserializer.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageDeserializer.java
index 40ef5fb..7f588a0 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageDeserializer.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageDeserializer.java
@@ -19,7 +19,11 @@
 
 package org.apache.hadoop.hive.metastore.messaging.json;
 
+import org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddNotNullConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.AddPartitionMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddPrimaryKeyMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddUniqueConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterIndexMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterPartitionMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterTableMessage;
@@ -27,6 +31,7 @@ import 
org.apache.hadoop.hive.metastore.messaging.CreateDatabaseMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateFunctionMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateIndexMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateTableMessage;
+import org.apache.hadoop.hive.metastore.messaging.DropConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropDatabaseMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropFunctionMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropIndexMessage;
@@ -190,4 +195,49 @@ public class JSONMessageDeserializer extends 
MessageDeserializer {
       throw new IllegalArgumentException("Could not construct InsertMessage", 
e);
     }
   }
+
+  @Override
+  public AddPrimaryKeyMessage getAddPrimaryKeyMessage(String messageBody) {
+    try {
+      return mapper.readValue(messageBody, JSONAddPrimaryKeyMessage.class);
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Could not construct 
AddPrimaryKeyMessage", e);
+    }
+  }
+
+  @Override
+  public AddForeignKeyMessage getAddForeignKeyMessage(String messageBody) {
+    try {
+      return mapper.readValue(messageBody, JSONAddForeignKeyMessage.class);
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Could not construct 
AddForeignKeyMessage", e);
+    }
+  }
+
+  @Override
+  public AddUniqueConstraintMessage getAddUniqueConstraintMessage(String 
messageBody) {
+    try {
+      return mapper.readValue(messageBody, 
JSONAddUniqueConstraintMessage.class);
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Could not construct 
AddUniqueConstraintMessage", e);
+    }
+  }
+
+  @Override
+  public AddNotNullConstraintMessage getAddNotNullConstraintMessage(String 
messageBody) {
+    try {
+      return mapper.readValue(messageBody, 
JSONAddNotNullConstraintMessage.class);
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Could not construct 
AddNotNullConstraintMessage", e);
+    }
+  }
+
+  @Override
+  public DropConstraintMessage getDropConstraintMessage(String messageBody) {
+    try {
+      return mapper.readValue(messageBody, JSONDropConstraintMessage.class);
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Could not construct 
DropConstraintMessage", e);
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/90a92b74/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageFactory.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageFactory.java
 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageFactory.java
index b24d1e3..71e25ed 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageFactory.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/messaging/json/JSONMessageFactory.java
@@ -33,8 +33,16 @@ import org.apache.hadoop.hive.metastore.api.Function;
 import org.apache.hadoop.hive.metastore.api.Index;
 import org.apache.hadoop.hive.metastore.api.NotificationEvent;
 import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint;
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
+import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint;
 import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddNotNullConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.AddPartitionMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddPrimaryKeyMessage;
+import org.apache.hadoop.hive.metastore.messaging.AddUniqueConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterIndexMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterPartitionMessage;
 import org.apache.hadoop.hive.metastore.messaging.AlterTableMessage;
@@ -42,6 +50,7 @@ import 
org.apache.hadoop.hive.metastore.messaging.CreateDatabaseMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateFunctionMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateIndexMessage;
 import org.apache.hadoop.hive.metastore.messaging.CreateTableMessage;
+import org.apache.hadoop.hive.metastore.messaging.DropConstraintMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropDatabaseMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropFunctionMessage;
 import org.apache.hadoop.hive.metastore.messaging.DropIndexMessage;
@@ -166,6 +175,33 @@ public class JSONMessageFactory extends MessageFactory {
     return new JSONInsertMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, 
tableObj, partObj, replace, fileIter, now());
   }
 
+  @Override
+  public AddPrimaryKeyMessage buildAddPrimaryKeyMessage(List<SQLPrimaryKey> 
pks) {
+    return new JSONAddPrimaryKeyMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, 
pks, now());
+  }
+
+  @Override
+  public AddForeignKeyMessage buildAddForeignKeyMessage(List<SQLForeignKey> 
fks) {
+    return new JSONAddForeignKeyMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, 
fks, now());
+  }
+
+  @Override
+  public AddUniqueConstraintMessage 
buildAddUniqueConstraintMessage(List<SQLUniqueConstraint> uks) {
+    return new JSONAddUniqueConstraintMessage(MS_SERVER_URL, 
MS_SERVICE_PRINCIPAL, uks, now());
+  }
+
+  @Override
+  public AddNotNullConstraintMessage 
buildAddNotNullConstraintMessage(List<SQLNotNullConstraint> nns) {
+    return new JSONAddNotNullConstraintMessage(MS_SERVER_URL, 
MS_SERVICE_PRINCIPAL, nns, now());
+  }
+
+  @Override
+  public DropConstraintMessage buildDropConstraintMessage(String dbName, 
String tableName,
+      String constraintName) {
+    return new JSONDropConstraintMessage(MS_SERVER_URL, MS_SERVICE_PRINCIPAL, 
dbName, tableName,
+        constraintName, now());
+  }
+
   private long now() {
     return System.currentTimeMillis() / 1000;
   }
@@ -188,6 +224,26 @@ public class JSONMessageFactory extends MessageFactory {
         }));
   }
 
+  static String createPrimaryKeyObjJson(SQLPrimaryKey primaryKeyObj) throws 
TException {
+    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
+    return serializer.toString(primaryKeyObj, "UTF-8");
+  }
+
+  static String createForeignKeyObjJson(SQLForeignKey foreignKeyObj) throws 
TException {
+    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
+    return serializer.toString(foreignKeyObj, "UTF-8");
+  }
+
+  static String createUniqueConstraintObjJson(SQLUniqueConstraint 
uniqueConstraintObj) throws TException {
+    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
+    return serializer.toString(uniqueConstraintObj, "UTF-8");
+  }
+
+  static String createNotNullConstraintObjJson(SQLNotNullConstraint 
notNullConstaintObj) throws TException {
+    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
+    return serializer.toString(notNullConstaintObj, "UTF-8");
+  }
+
   static String createTableObjJson(Table tableObj) throws TException {
     TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
     return serializer.toString(tableObj, "UTF-8");

Reply via email to