本文整理汇总了Scala中org.apache.hadoop.fs.FSDataOutputStream类的典型用法代码示例。如果您正苦于以下问题:Scala FSDataOutputStream类的具体用法?Scala FSDataOutputStream怎么用?Scala FSDataOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FSDataOutputStream类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: StringWriter
//设置package包名称以及导入依赖的类
package yamrcraft.etlite.writers
import org.apache.hadoop.fs.{FSDataOutputStream, Path}
import yamrcraft.etlite.utils.FileUtils
class StringWriter(tempFile: String, outputFile: String) extends Writer[String] {
// lazy initialization
var writer: Option[FSDataOutputStream] = None
val tempPath = new Path(tempFile + ".txt")
val outputPath = new Path(outputFile + ".txt")
override def write(event: String): Unit = {
if (writer.isEmpty) {
writer = Some(createWriter(tempPath.toString))
}
writer.get.writeUTF(event)
writer.get.writeChar('\n')
}
override def commit(): Unit = {
writer.get.close()
val fs = FileUtils.getFS(outputPath.toString)
fs.mkdirs(outputPath.getParent)
if (fs.exists(outputPath)) {
fs.rename(outputPath, new Path(outputPath.getParent, s"__${outputPath.getName}.${System.currentTimeMillis()}.old.__"))
}
if (tempFile.startsWith("hdfs")) {
fs.copyFromLocalFile(true, true, tempPath, outputPath)
} else {
fs.rename(tempPath, outputPath)
}
}
private def createWriter(file: String) = {
val fs = FileUtils.getFS(file)
val path = new Path(file)
if (fs.exists(path)) {
fs.delete(path, true)
}
fs.create(path)
}
}