Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup to drop the whole table #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions src/main/scala/com/criteo/dev/cluster/copy/CleanupHiveAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,38 @@ object CleanupHiveAction {
private val logger = LoggerFactory.getLogger(this.getClass)

def apply(config: GlobalConfig, target: Node): Try[CleanResult] = {
val action = GetSourceMetadataAction(config, target)
val getMetadata = new GetMetadataAction(config, config.backCompat, target)
val hiveScript = new SshHiveAction(target)
val shellScript = new SshMultiAction(target)
action(
config.source.tables.filter(t => {
if (t.skipCleanup) {
logger.info(s"Skip ${t.name}")
false
} else true
}),
useLocalScheme = false
) map {
case Left(invalid) =>
logger.info(s"${invalid.name} is invalid, skip cleaning")
case Right(FullTableInfo(tableInfo, hdfsInfo)) =>
logger.info(s"Cleaning up ${tableInfo.fullName}")
// remove tables/partitions
val partitions = tableInfo.partitions.map { part =>
s"PARTITION (${CopyUtilities.partitionSpecString(part.partSpec, tableInfo.ddl.partitionedBy)})"
}.mkString(", ")
val statement = if (partitions.isEmpty)
s"DROP TABLE ${tableInfo.fullName}"
else
s"ALTER TABLE ${tableInfo.fullName} DROP $partitions"
logger.info(s"Add Hive statement: $statement")
hiveScript.add(statement)
// remove files
hdfsInfo.files.foreach { case HDFSFileInfo(path, _) =>
val command = s"hdfs dfs -rm -r $path"
logger.info(s"Add HDFS command: $command")
shellScript.add(command)
config.source.tables.filter(t => {
if (t.skipCleanup) {
logger.info(s"Skip ${t.name}")
false
} else true
}).foreach(table =>
Try {
getMetadata(table.name, false)
} match {
case Success(t) => {
t.ddl.location match {
case Some(location) =>
// remove file
val command = s"hdfs dfs -rm -r -f $location"
logger.info(s"Add HDFS command: $command")
shellScript.add(command)
// drop table
val statement = s"DROP TABLE ${t.fullName}"
logger.info(s"Add Hive statement: $statement")
hiveScript.add(statement)
case None =>
logger.info(s"Cannot get the location of ${t.fullName}, skip cleaning")
}
}
}
case Failure(e) => {
logger.info(s"${table.name} is not valid, skip cleaning, message: ${e.getMessage}")
}
}
)
val res = for {
hive <- Try(hiveScript.run())
shell <- Try(shellScript.run())
Expand Down