Implementing put and delete operations using BatchWriteItem

The BatchWriteItem operation allows us to work with groups of up to 25 items per request. Additionally, we can put and delete items to one or multiple tables. Amazon DynamoDB’s page says: A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB. In this article we are going to go through the process of putting and deleting items to a table that looks like this:

Inserting and updating items to DynamoDB lists and maps

Should I use a map or a list? A simple rule: If you need to update or remove an item, lists only allow you to do it by index. While maps let you do it by key. Their simplified representation goes like this: { "someMap": { "key1": "value1", "key2": "value2", }, "someList": ["value1", "value2"] } Inserting and updating to a map The following example adds or updates an instance of “SomeItem” as a json string into a map field called someMapField.

Testing scala classes and controllers

This article discusses different approaches for testing classes, services, and PlayFramework controllers using scalatest. Setting up the project dependencies First step is to add to build.sbt the next dependency: "org.scalatest" %% "scalatest" % "3.2.2" The following template unit test uses the WordSpec style which offers a natural way for writing tests. scalatest offers more styles, but I find this one more expressive. PlaySpec (found in the PaylFramework) extends WordSpec and has a similar behavior.

Enumerations

Using case object Unlike other languages, Scala does not have a keyword for enumerations. One way to implement them is by using case objects. For example: sealed trait OperatingSystem case object FreeBsd extends OperatingSystem case object Debian extends OperatingSystem case object Alpine extends OperatingSystem case object Arch extends OperatingSystem case object Osx extends OperatingSystem To use it, just define a value of type OperatingSystem. You can even get a string representation from these values:

Akka Actors

The Play Framework is built upon Akka actors, but does everything so that you don’t really need to use them. Despite this, actors are easy to integrate with Play, precisely because it is built on them (there is already an actor system for you to use) A few exceptions are: websockets handling (doing that using actors is a breeze) scheduling tasks for doing daily or weekly jobs Akka actors use messages to communicate between among them.

Check multiple option values in Scala

Two main approaches for doing this, the second one being for me the most appropriate. Check values using regular if conditions: if (opt1.isDefined && opt2.isDefined && opt3.isDefined) // do something Check values using match: (opt1, opt2, opt3) match { case(Some(val1), Some(val2), Some(val3)) => ??? case _ => ??? }

Creating and validating JWT JSON web tokens

This article goes through the process of creating and validating JWT’s (JSON web tokens) using Scala. Our implementation uses the awesome io.jsonwebtoken library, and can be added to a sbt project like this: libraryDependencies ++= Seq("io.jsonwebtoken" % "jjwt" % "0.9.1") Creating a token We are going to use Scala’s apply/unapply functions for this implementation. This will allow us to use matchers for checking JWT’s. import java.time.Instant import java.util.{Date, UUID} import io.

Make a request to a remote service

This article shows how to make remote requests to services using Scala and the Play-Framework. It documents some recommended features that can improve the reliability of the request. Setup Make sure you add the following dependency to build.sbt libraryDependencies += ws GET request GET request to a remote service import scala.concurrent.Future import play.api.http.Status._ import play.api.libs.ws.WSClient import play.api.libs.json.{JsSuccess, Json} case class UnexpectedResponseStatus(message: String) extends Exception(message) case class InvalidServiceResponse(message: String) extends Exception(message) class SomeClass @Inject()( ws: WSClient ) { def getFromRemoteService(url: String): Future[Option[MyClass]] = { ws.

A purely functional example using Scala

On this tutorial we’re going to explore the basics of purely functional programming using Scala. One of the principles with purely functional style, is that we have to define concepts in terms of functions. The basics: Comparing a number We’re going to create a simple function that lets us compare a number to a previously stored number in a purely functional way. For that, we can define the following function:

Examples of ‘for’ queries with Scala

On this page you are going to find some examples of ‘for’ queries. Let’s start by defining the following database: case class Book(title: String, authors: List[String]) val books: List[Book] = List( Book("structure and interpretation of computer programs", List("abelson, harald", "sussman, gerald j.")), Book("introduction to functional programming", List("bird, richard", "wadler, phil")), Book("effective java", List("bloch, joshua")), Book("java puzzlers", List("bloch, joshua", "gafter, neal")), Book("programming in scala", List("odersky, martin", "spoon, lex", "venners, bill")) ) Find the titles of books whose author’s name is ‘bird’