Wednesday, January 17, 2018

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=

No comments: