Skip to content

Instantly share code, notes, and snippets.

@dbalduini
Last active November 23, 2016 19:42
Show Gist options
  • Save dbalduini/9674832 to your computer and use it in GitHub Desktop.
Save dbalduini/9674832 to your computer and use it in GitHub Desktop.
SQL to File streaming with NIO
/**
*
* 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