Skip to content

Instantly share code, notes, and snippets.

@cr0t
Created September 26, 2011 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cr0t/1243431 to your computer and use it in GitHub Desktop.
Save cr0t/1243431 to your computer and use it in GitHub Desktop.
Play! framework: How do get files from Hadoop and send them via HTTP
object Download extends Controller {
import org.apache.commons.logging.Log
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs._
import java.io.InputStream
import java.net.URI
import java.net.URLDecoder
def download(filename: String) = {
try {
val uri = play.configuration("hadoop.uri") // "hadoop.uri=hdfs://hadoop" in conf/application.conf file
val conf = new Configuration()
val hdfs = FileSystem.get(URI.create(uri), conf)
response.setHeader("Content-type", "application/force-download")
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"")
// response.setHeader("Content-Length", filesize + "");
response.direct = hdfs.open(new Path(filename)).asInstanceOf[java.io.InputStream]
}
catch {
case e: Exception => Logger.error(e, "Couldn't get file from Hadoop by the given filename")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment