EzDevInfo.com

rediscala

Non-blocking, Reactive Redis driver for Scala (with Sentinel support)

Mass insert using rediscala

I am trying to prepare a mass insert using https://github.com/etaty/rediscala version 1.2 without any success. Best way that I figured was to make something like this:

implicit val akkaSystem = akka.actor.ActorSystem()

val redis = RedisClient()

RedisProtocolRequest.multiBulk("SET", Seq(ByteString("mykey"), ByteString("myvalue")) ) ++ RedisProtocolRequest.multiBulk("SET", Seq(ByteString("yourkey"), ByteString("yourvalue")) )

???????????

akkaSystem.shutdown()

Unfortunetly, I was not able to find a method to send a ByteString to the server. Can someone help me finish the script, or am I on the wrong path?


Source: (StackOverflow)

Correct way of creating an ActorSystem inside an Actor

I'm using a third-party library (rediscala) to access a Redis DB inside my own Actor. Following is an example of how I'm currently doing it. Is this correct ? Are there any potential problems with the following code because I'm creating an akkaSystem inside my actor. If SimpleRedisClientActor crashes then I need to restart SimpleRedisClientActor which will create another Actor system. Should I override preStart and postStop ?

import akka.actor.{Props, Actor, ActorLogging}
import redis.RedisClient

import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

class SimpleRedisClientActor extends Actor with ActorLogging {

  implicit val akkaSystem = akka.actor.ActorSystem()
  val redis = RedisClient()

  def receive: Receive = {

    case PingRedis => {
      val futurePong = redis.ping()
      futurePong.onComplete{
        case Success(s) =>  log.info("Redis replied back with a pong")
        case Failure(f) =>  log.info("Something was wrong ...")
      }
    }
  }
}

object RedisActorDemo extends App {

  implicit val akkaSystem = akka.actor.ActorSystem()
  val simpleActor = akkaSystem.actorOf(Props(classOf[SimpleRedisClientActor]))
  simpleActor ! PingRedis
}

object PingRedis

Source: (StackOverflow)

Advertisements

How to extract value from a scala Future

How to I perform a reduce/fold operation on the Seq and then get the final value.

I'm performing an operation (in this case a Redis call) that returns a Future. I'm processing the Future (results) using a map operation.

The map operation returns a Future[Seq[Any]] type.

res0: scala.concurrent.Future[Seq[Any]] = scala.concurrent.impl.Promise$DefaultPromise@269f8f79

Now I want to perform some operations(fold/reduce) on this Seq and then get a final value. How can I achieve this?

  implicit val akkaSystem = akka.actor.ActorSystem()
  val redisClient = RedisClient()
  val sentimentZSetKey = "dummyzset"
  val currentTimeStamp = System.currentTimeMillis()
  val end = Limit(currentTimeStamp)
  val start = Limit(currentTimeStamp - 60 * 100000)
  val results = redisClient.zrangebyscoreWithscores(ZSetKey, start, end)
  implicit val formats = DefaultFormats
  import org.json4s._
  import org.json4s.native.JsonMethods._
  import org.json4s.DefaultFormats


  results.map {
    seq => seq.map {
      element => element match {

        case (byteString, value) => {
          val p = byteString.decodeString("UTF-8")
          try {
            val ph = parse(p).extract[MyClass]
            ph

          } catch {
            case e: Exception => println(e.getMessage)
          }
        }
        case _ =>
      }
    }
  } 

Source: (StackOverflow)