Repository: incubator-griffin Updated Branches: refs/heads/master 664d14bea -> a414f4bcb
added close function for closing opened file in class HdfsUtils Added close function for closing opened file in class HdfsUtils, to avoid memory leak. Author: root <[email protected]> This patch had conflicts when merged, resolved by Committer: Lionel Liu <[email protected]> Closes #15 from yiyef/master. Project: http://git-wip-us.apache.org/repos/asf/incubator-griffin/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-griffin/commit/a414f4bc Tree: http://git-wip-us.apache.org/repos/asf/incubator-griffin/tree/a414f4bc Diff: http://git-wip-us.apache.org/repos/asf/incubator-griffin/diff/a414f4bc Branch: refs/heads/master Commit: a414f4bcb0c0a8834c5bb4779d0a721fb9328d04 Parents: 664d14b Author: root <[email protected]> Authored: Fri Apr 27 10:24:28 2018 +0800 Committer: Lionel Liu <[email protected]> Committed: Fri Apr 27 10:24:28 2018 +0800 ---------------------------------------------------------------------- .../griffin/measure/utils/HdfsUtils.scala | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/a414f4bc/measure/src/main/scala/org/apache/griffin/measure/utils/HdfsUtils.scala ---------------------------------------------------------------------- diff --git a/measure/src/main/scala/org/apache/griffin/measure/utils/HdfsUtils.scala b/measure/src/main/scala/org/apache/griffin/measure/utils/HdfsUtils.scala new file mode 100644 index 0000000..bd7611f --- /dev/null +++ b/measure/src/main/scala/org/apache/griffin/measure/utils/HdfsUtils.scala @@ -0,0 +1,54 @@ +package org.apache.griffin.util + +import java.io.File + +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.fs.{FSDataInputStream, FSDataOutputStream, FileSystem, Path} + +object HdfsUtils { + + private val conf = new Configuration() + + private val dfs = FileSystem.get(conf) + + def createFile(filePath: String): FSDataOutputStream = { + return dfs.create(new Path(filePath)) + } + + def openFile(filePath: String): FSDataInputStream = { + return dfs.open(new Path(filePath)) + } + + def writeFile(filePath: String, message: String): Unit = { + val out = createFile(filePath) + out.write(message.getBytes("utf-8")) + closeFile(out) + } + + /** + * Close inputStream or outputStream + */ + def closeFile(stream:Object): Unit = { + stream match{ + case inputObj:FSDataInputStream => { + try{ + stream.asInstanceOf[FSDataInputStream].close() + }catch{ + case e:Exception=>e.printStackTrace() + } + } + case outputObj:FSDataOutputStream => { + try{ + stream.asInstanceOf[FSDataOutputStream].close() + }catch{ + case e:Exception=>{ + e.printStackTrace() + } + } + } + case _ => println("wrong object type,it not closable object!") + } + } + + +}
