Skip to content

Instantly share code, notes, and snippets.

View iamthiago's full-sized avatar

Thiago Pereira iamthiago

  • Stockholm, Sweden
View GitHub Profile
import java.util.Properties
import akka.actor.{Actor, ActorLogging, Props}
import akka.event.LoggingReceive
import com.typesafe.config.{Config, ConfigFactory}
import KafkaProducerActor.KafkaMessage
import kafka.common.FailedToSendMessageException
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import scala.concurrent.duration._
import scala.annotation.tailrec
import scala.collection.immutable.ListMap
object WeightSorting extends App {
def sortWeight(str: String): String = {
val map = str
.split(" ") //split by empty space
.map(s => (sumTailRecursive(s), s)) //let's create a tuple of (weight, string)
.groupBy(_._1) //group it by weight
@iamthiago
iamthiago / AwsKinesisFlowStage.scala
Created July 19, 2017 21:12
Aws Kinesis Flow to be used in Akka Streams
import akka.stream._
import akka.stream.stage._
import com.amazonaws.handlers.AsyncHandler
import com.amazonaws.services.kinesis.AmazonKinesisAsync
import com.amazonaws.services.kinesis.model.{PutRecordsRequest, PutRecordsRequestEntry, PutRecordsResult}
import scala.collection.JavaConverters._
class AwsKinesisFlowStage(streamName: String, kinesisClient: AmazonKinesisAsync) extends GraphStage[FlowShape[Seq[PutRecordsRequestEntry], PutRecordsResult]] {
object AddressRepository extends DBConnection {
val table = TableQuery[AddressModel]
def search(filter: Address): Future[Seq[Address]] = {
@tailrec
def recursive(queryMap: Map[String, String], queryAccum: Query[AddressModel, Address, Seq]): Query[AddressModel, Address, Seq] = {
if (queryMap.isEmpty) queryAccum else {
val key = queryMap.head._1
val value = queryMap.head._2
case class Address(state: Option[String], city: Option[String], neighborhood: Option[String], street: Option[String], streetNumber: Option[String]) {
def toMap(): Map[String, String] = {
Map(
"state" -> this.state,
"city" -> this.city,
"neighborhood" -> this.neighborhood,
"street" -> this.street,
"streetNumber" -> this.streetNumber
).filter(_._2.isDefined).mapValues(_.get)
}
private Predicate createAdvancedSearchQuery(Long maxPrice, Long minPrice, Integer beds, Integer baths, String province) {
QProperty property = QProperty.property;
BooleanBuilder booleanBuilder = new BooleanBuilder();
if(maxPrice != null && minPrice != null) {
booleanBuilder.and(property.price.between(minPrice, maxPrice));
} else if (maxPrice != null) {
booleanBuilder.and(property.price.loe(maxPrice));
} else if (minPrice != null) {
@iamthiago
iamthiago / backpressure.scala
Last active May 23, 2017 12:56
Brief introduction to back-pressure and Akka Streams
//imports
object MyBackPressureTest extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val addresses = List(Address...) //a list of some addresses
val createRequestFlow = Flow[Address].map { address =>
@iamthiago
iamthiago / BigData.scala
Created April 24, 2017 17:36 — forked from davegurnell/BigData.scala
Slick database mapping of >22 columns using HLists
import slick.driver.MySQLDriver.simple._
import scala.slick.collection.heterogenous._
import scala.slick.collection.heterogenous.syntax._
import scala.slick.jdbc._
// This is the large case class we are mapping -- 25 string fields:
case class BigData(
field1: String,
field2: String,
field3: String,
package com.vivareal.columbus
import java.util.concurrent.CountDownLatch
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props}
import akka.routing.RoundRobinPool
import com.vivareal.columbus.infrastructure.ActorCreation
import scala.concurrent.duration.Duration
import java.util.UUID
import akka.NotUsed
import akka.actor.{ActorRef, ActorSystem}
import akka.stream._
import akka.stream.scaladsl.{Balance, Flow, GraphDSL, Sink, Source}
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.sqs.AmazonSQSAsyncClient
import com.amazonaws.services.sqs.model.Message
import com.github.dwhjames.awswrap.sqs.AmazonSQSScalaClient