Last active
November 23, 2016 19:42
-
-
Save dbalduini/9674832 to your computer and use it in GitHub Desktop.
SQL to File streaming with NIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Statefull File Streaming class that write files with java.nio. | |
* | |
*/ | |
class FileStreaming(filename: String) { | |
val output = new java.io.File(ApplicationConf.outputDir, filename) | |
output.createNewFile() | |
val channel = new RandomAccessFile(output, "rw").getChannel | |
val buffer = ByteBuffer.allocate(1024) | |
var count = 0 | |
def stream(rs: WrappedResultSet) { | |
buffer.clear | |
val data = Metadata(rs).data.map(t => t._2).mkString("", "|", "\n").getBytes | |
(0 to data.length - 1).foreach(i => buffer put data(i)) | |
buffer.flip | |
channel.write(buffer) | |
count += 1 | |
} | |
def close() = channel.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment