加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 从Play Framework调用Solr异步

发布时间:2020-12-16 09:23:31 所属栏目:安全 来源:网络整理
导读:我创建了一个Play 2.1 Scala应用程序.我不确定从Play应用程序调用Solr的最佳方法是什么: Play 2没有Solr模块. AFAIK所有像SolrJ这样的Solr-API都在阻塞. 我可以将SolrJ调用包装到Future中,但是这也会阻塞一个线程,对吗? 我应该使用play.api.libs.ws.WS库来
我创建了一个Play 2.1 Scala应用程序.我不确定从Play应用程序调用Solr的最佳方法是什么:

> Play 2没有Solr模块.
> AFAIK所有像SolrJ这样的Solr-API都在阻塞.
>我可以将SolrJ调用包装到Future中,但是这也会阻塞一个线程,对吗?
>我应该使用play.api.libs.ws.WS库来调用Solr并使用Plays JSON支持来提取结果(如下例所示)还是有更简单/更快的方法?

val solrQuery: Future[play.api.libs.ws.Response] = WS.url("http://localhost:8983/solr/collection1/select?q=id%3A123&wt=json").get()

解决方法

以下是我在我的项目中使用WS的方法:

val itselfNodeFuture = Statix.doParams( Statix.SolrSelectWSReq,List(
    "wt"     -> "json","q"      -> "*:*","fq"     -> "node_type:collection","fq"     -> "id:%d".format( nodeId),"indent" -> "true","rows"   -> "1","fl"     -> "id,parent_id,title","fl"     -> "date_created,date_about,date_modified")
).get()

//Use the first Await after the last future
val itselfJson = Await.result(
    itselfNodeFuture,Duration("2 sec")).json

val mainRow = (itselfJson  "response"  "docs").as[ Seq[JsValue]]
val mainNodeParent = (mainRow(0)  "parent_id").as[Long]
val mainNodeTitle = (mainRow(0)  "title").as[String]

这是我使用的实用程序类,doParams特别有用.

object Statix { //Noder must extend this
    def SolrSelectWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/select/")
    def SolrUpdateWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/update/json/")

    def doParams(request: WS.WSRequestHolder,params: List[(String,String)]) = {
        params.foldLeft( request){
            (wsReq,tuple) => wsReq.withQueryString( tuple)}}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读