Quantcast
Channel: Charsyam's Blog
Viewing all articles
Browse latest Browse all 122

[Tip] HBase Asynchbase 의 Defer를 Future로 변경하기

$
0
0

OpenTSDB에서 만든 Asynchbase는 Future가 아닌 Defer를 이용합니다. 이를 Future로 바꾸는 함수는 다음과 같습니다. 이 때 주의할 것은 HBase는 protobuf-java 2.5.0을 쓰고 있기 때문에,
현재 다른 library에서 다른 protobuf-java 버전을 쓰면 문제가 발생할 수 있습니다.


import java.nio.charset.StandardCharsets

import org.hbase.async._

import scala.collection.JavaConversions._
import scala.concurrent.ExecutionContext
import com.twitter.util.{Future, Promise, Return, Throw}
import com.stumbleupon.async.{Callback, Deferred}

object Test {
  implicit val ec = scala.concurrent.ExecutionContext.global

  implicit def futureFromDeferred[A](d: Deferred[A]): Future[A] = {
    val promise = new Promise[A]

    d.addCallback(new Callback[Unit, A] {
      def call(arg: A) = (promise() = Return(arg))
    })

    d.addErrback(new Callback[Unit, Throwable] {
      def call(arg: Throwable) = (promise() = Throw(arg))
    })

    promise
  }

  def makeClient() = {
    val client = new HBaseClient("localhost")
    client
  }

  def test(key: String) = {
    val defer = futureFromDeferred(client.get(buildRequest(key)))
    for {
      keyValue <- FutureOption(defer)
      v <- Future.value(Some(
        new String(keyValue.head.value(), StandardCharsets.UTF_8)))
    } yield v
  }

  def buildRequest(key: String)(implicit ec: ExecutionContext) = {
    val get =
      new GetRequest(tableName, key)
    get.qualifier("cf1:content_id")
  }
}


Viewing all articles
Browse latest Browse all 122

Trending Articles