Skip to content

Instantly share code, notes, and snippets.

@warrenronsiek
Created May 11, 2020 11:26
Show Gist options
  • Select an option

  • Save warrenronsiek/fd19fc931299fa4a02e82f5298b4323a to your computer and use it in GitHub Desktop.

Select an option

Save warrenronsiek/fd19fc931299fa4a02e82f5298b4323a to your computer and use it in GitHub Desktop.
Stream file lines from S3 in Scala
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.{GetObjectRequest, S3Object => JS3Object}
import java.io.{BufferedInputStream, BufferedReader, File}
import java.util.zip.GZIPInputStream
def stream(bucket: String, key: String)(implicit client: AmazonS3): Iterator[String] = {
val js3: JS3Object = client.getObject(new GetObjectRequest(bucket, key))
val s3ObjectInputStream = js3.getObjectContent
val br = new BufferedReader(new BufferedInputStream(new GZIPInputStream(s3ObjectInputStream)).reader)
Stream.continually(br.readLine()).takeWhile(_ != null).toIterator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment