diff --git ql/src/java/org/apache/hadoop/hive/ql/parse/MetaDataExportListener.java ql/src/java/org/apache/hadoop/hive/ql/parse/MetaDataExportListener.java new file mode 100644 index 0000000..15dc4e8 --- /dev/null +++ ql/src/java/org/apache/hadoop/hive/ql/parse/MetaDataExportListener.java @@ -0,0 +1,85 @@ +/** + * 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.ql.parse; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.HiveMetaStore.HMSHandler; +import org.apache.hadoop.hive.metastore.MetaStorePreEventListener; +import org.apache.hadoop.hive.metastore.Warehouse; +import org.apache.hadoop.hive.metastore.api.InvalidOperationException; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.events.PreDropTableEvent; +import org.apache.hadoop.hive.metastore.events.PreEventContext; +import org.apache.hadoop.hive.metastore.events.PreEventContext.PreEventType; + +public class MetaDataExportListener extends MetaStorePreEventListener { + + public MetaDataExportListener(Configuration config) { + super(config); + } + + private void export_meta_data(PreDropTableEvent tableEvent) throws MetaException { + FileSystem fs = null; + Table tbl = tableEvent.getTable(); + String name = tbl.getTableName(); + org.apache.hadoop.hive.ql.metadata.Table mTbl = + new org.apache.hadoop.hive.ql.metadata.Table(tbl); + HMSHandler handler = tableEvent.getHandler(); + HiveConf hiveconf = handler.getHiveConf(); + Warehouse wh = new Warehouse(hiveconf); + Path tblPath = new Path(tbl.getSd().getLocation()); + fs = wh.getFs(tblPath); + Date now = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); + String dateString = sdf.format(now); + Path metaPath = new Path(fs.getHomeDirectory(), name + "." + dateString); + try { + fs.mkdirs(metaPath); + } catch (IOException e) { + throw new MetaException(e.getMessage()); + } + Path outFile = new Path(metaPath, name + ".metadata"); + // Export the metadata and move it to the user's trash + try { + EximUtil.createExportDump(fs, outFile, mTbl, null); + wh.deleteDir(metaPath, true); + } catch (IOException e) { + throw new MetaException(e.getMessage()); + } catch (SemanticException e) { + throw new MetaException(e.getMessage()); + } + } + + @Override + public void onEvent(PreEventContext context) throws MetaException, NoSuchObjectException, + InvalidOperationException { + if (context.getEventType() == PreEventType.DROP_TABLE) { + export_meta_data((PreDropTableEvent) context); + } + } + +} \ No newline at end of file diff --git ql/src/test/queries/positive/metadata_export_drop.q ql/src/test/queries/positive/metadata_export_drop.q new file mode 100644 index 0000000..b67cb9c --- /dev/null +++ ql/src/test/queries/positive/metadata_export_drop.q @@ -0,0 +1,3 @@ +set hive.metastore.pre.event.listeners=org.apache.hadoop.hive.ql.parse.MetaDataExportListener; +create table tmp_meta_export_listener_drop_test (foo string); +drop table tmp_meta_export_listener_drop_test; \ No newline at end of file