Skip to content

Instantly share code, notes, and snippets.

@michalbogacz
Created February 20, 2017 07:37
Show Gist options
  • Save michalbogacz/e8716bbc17f355312ea82de61933545f to your computer and use it in GitHub Desktop.
Save michalbogacz/e8716bbc17f355312ea82de61933545f to your computer and use it in GitHub Desktop.
Backup MongoDB collection to S3 with Akka Streams and Alpakka
/*
Description:
Simple example how to backup MongoDB collection to S3.
It's so easy with Akka Streams and Alpakka :)
Dependencies:
libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.4.17"
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-s3" % "0.6"
libraryDependencies += "org.mongodb" % "mongodb-driver-reactivestreams" % "1.3.0"
*/
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.alpakka.s3.impl.MetaHeaders
import akka.stream.alpakka.s3.scaladsl.S3Client
import akka.stream.scaladsl.{Compression, Keep, Source}
import akka.util.ByteString
import com.mongodb.reactivestreams.client.MongoClients
import scala.util.{Failure, Success}
object MongoS3Backup {
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
implicit val ec = system.dispatcher
def main(args: Array[String]): Unit = {
val mongoClient = MongoClients.create("mongodb://localhost:27017")
val collectionToBackup = mongoClient.getDatabase("test").getCollection("export")
val meta = MetaHeaders(Map("someMetadata" → "abc")) //we can add some metadata information
val sink = S3Client().multipartUpload("supersophisticatedbucketname", "123456.json", metaHeaders = meta)
val r =
Source
.fromPublisher(collectionToBackup.find())
.map(d ⇒ d.toJson)
.map(d ⇒ ByteString(d))
.via(Compression.gzip) //optional step to lower file size
.toMat(sink)(Keep.right)
.run()
r.onComplete {
case Success(s) ⇒
println(s)
mongoClient.close()
system.terminate()
case Failure(e) ⇒
e.printStackTrace()
mongoClient.close()
system.terminate()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment