Skip to content

Instantly share code, notes, and snippets.

@rajkrrsingh
Created January 17, 2018 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajkrrsingh/4016f0a3f02c7f493171d29fb403b62b to your computer and use it in GitHub Desktop.
Save rajkrrsingh/4016f0a3f02c7f493171d29fb403b62b to your computer and use it in GitHub Desktop.
how to create own metastore event listner

Create DROP table listner which get triggered once the DROP table event happen

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.metastore.MetaStoreEventListener;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.events.DropTableEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DropTableListner extends MetaStoreEventListener  {
    private static final Logger log = LoggerFactory.getLogger(DropTableListner.class);


    public DropTableListner(Configuration config) {
        super(config);
        log.info("[Thread: "+Thread.currentThread().getName()+"] | [method: "+Thread.currentThread().getStackTrace()[1].getMethodName()+" ] | DropTableHook created ");
    }
    @Override
    public void onDropTable(DropTableEvent tableEvent) throws MetaException {
        System.out.println(tableEvent.getTable()+" Table dropped succesfully");
        log.info("[Thread: "+Thread.currentThread().getName()+"] | [method: "+Thread.currentThread().getStackTrace()[1].getMethodName()+" ] | "+tableEvent.getTable()+ " Table dropped succesfully");

    }

}

update hive-site.xml

hive.metastore.event.listeners=DropTableListner

compile DropTableListner and add jar to Hive classpath

on Each Drop table command you should see following messages in hivemetastore log.

2018-01-11 01:56:24,161 INFO  [pool-3-thread-1]: DropTableHook (DropTableHook.java:onDropTable(19)) - [Thread: pool-3-thread-1] | [method: onDropTable ] | Table(tableName:pre_load_table, dbName:default, owner:hive, createTime:1505128080, lastAccessTime:0, retention:0, sd:StorageDescriptor(cols:[FieldSchema(name:comp_id, type:string, comment:null), FieldSchema(name:item, type:string, comment:null), FieldSchema(name:local_supp_code, type:string, comment:null), FieldSchema(name:forecast_id, type:string, comment:null), FieldSchema(name:transaction_date, type:string, comment:null), FieldSchema(name:planned_need_date, type:string, comment:null), FieldSchema(name:mfg_partno, type:string, comment:null), FieldSchema(name:local_mfg_code, type:string, comment:null), FieldSchema(name:forecast_qty_inv, type:string, comment:null), FieldSchema(name:forecast_qty_pur, type:string, comment:null), FieldSchema(name:purchasing_uom, type:string, comment:null), FieldSchema(name:forecast_method, type:string, comment:null), FieldSchema(name:b2b_communicated, type:string, comment:null), FieldSchema(name:file_name, type:string, comment:null)], location:hdfs://rk253.openstack:8020/apps/hive/warehouse/pre_load_table, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{serialization.format=|, line.delim=
@aakashnand
Copy link

Could you please provide the full project with pom.xml as well?

@rajkrrsingh
Copy link
Author

have uploaded the sample code on https://github.com/rajkrrsingh/HiveDropTableListener

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment