设为首页 收藏本站
查看: 666|回复: 0

[经验分享] Solr(10)Async and Multiple Thread on SolrJ

[复制链接]
发表于 2016-12-16 09:34:43 | 显示全部楼层 |阅读模式
Solr(10)Async and Multiple Thread on SolrJ

1. Async Operation
I try to provide async http client there, but it seems does not help a lot

import jp.sf.amateras.solr.scala.async.AsyncSolrClient
import scala.concurrent.ExecutionContext.Implicits.global

import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.Duration
import scala.concurrent.{Future, Await}

  def addJobs(jobs:List[Job]):Unit = {
    val futures = new ListBuffer[Future[Unit]]
    for ( i<- 0 to jobs.size - 1){
      val future = solrAsyncClient.add(jobs(i))
      futures+= future
    }
    Await.ready(Future.sequence(futures),Duration.Inf)
  }

The test class is as follow, it helps, but not much.
        val num = 10000
        val batch = 350
        val jobs = ListBuffer[Job]()
        val start = System.currentTimeMillis()
        for ( i<- 1 to num){
          val job = Job("id" + i, "title" + i, "desc" + i, "industry" + i)
          jobs += job
          if(i % batch == 0){
            SolrClientDAO.addJobs(jobs.toList)
            jobs.clear()
          }
        }
        val end = System.currentTimeMillis()

        println("total time for " + num + " is " + (end-start))
        println("it is " + num / ((end-start)/1000) + " jobs/second")

2. SolrJ with Latest Version
package com.sillycat.jobsconsumer.persistence

import com.sillycat.jobsconsumer.models.Job
import com.sillycat.jobsconsumer.utilities.{IncludeConfig, IncludeLogger}
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient
import org.apache.solr.common.SolrInputDocument

import scala.collection.mutable.ListBuffer
import scala.collection.JavaConverters._

/**
* Created by carl on 8/7/15.
*/
object SolrJDAO  extends IncludeLogger with IncludeConfig{


  private val solrClient = {
    try {
      logger.info("Init the SOLR Client ---------------")
      val solrURL = config.getString(envStr("solr.url.jobs"))
      logger.info("SOLR URL = " + solrURL)
      val client = new ConcurrentUpdateSolrClient(solrURL,100000,100)
      client
    } catch {
      case x: Throwable =>
        logger.error("Couldn't connect to SOLR: " + x)
        null
    }
  }


  def releaseResource = {
    if(solrClient != null){
      solrClient.close()
    }
  }

  def addJobs(jobs:List[Job]):Unit = {
    val docs = new ListBuffer[SolrInputDocument]
    for( i<- 0 to (jobs.size - 1)){
      val job = jobs(i)
      val doc = new SolrInputDocument()
      doc.addField("title", job.title)
      doc.addField("id", job.id)
      doc.addField("desc", job.desc)
      doc.addField("industry", job.industry)
      docs += doc
    }
    solrClient.add(docs.toList.asJava)
  }


  def commit = {
    solrClient.commit()
  }


}

The test class is as follow:
package com.sillycat.jobsconsumer.persistence

import com.sillycat.jobsconsumer.models.Job
import com.sillycat.jobsconsumer.utilities.IncludeConfig
import org.scalatest.{BeforeAndAfterAll, Matchers, FunSpec}

import scala.collection.mutable.ListBuffer

/**
* Created by carl on 8/7/15.
*/
class SolrJDAOSpec extends FunSpec with Matchers with BeforeAndAfterAll with IncludeConfig{

  override def beforeAll() {
    if(config.getString("build.env").equals("test")){

    }
  }

  override def afterAll() {

  }

  describe("SolrDAO") {
    describe("#add and query"){
      it("Add one single job to Solr") {
        val expect = Job("id1","title1","desc1","industry1")

        val num = 1000000
        val batch = 300
        val jobs = ListBuffer[Job]()

        val start = System.currentTimeMillis()
        for ( i<- 1 to num ){

          val job = Job("id" + i, "title" + i, "desc" + i, "industry" + i)
          jobs += job
          if(i % batch == 0){
            SolrJDAO.addJobs(jobs.toList)
            jobs.clear()
          }
        }
        val end = System.currentTimeMillis()

        println("total time for " + num + " is " + (end-start))
        val duration = (end - start) / 1000
        println("it is " + num / duration  + " jobs/second")

        SolrJDAO.commit

      }
    }
  }

}

The result is amazing….
INFO [pool-4-thread-2-ScalaTest-running-SolrJDAOSpec] 2015-08-07 16:04:10,009 SolrJDAO.scala (line 24) Init the SOLR Client ---------------
INFO [pool-4-thread-2-ScalaTest-running-SolrJDAOSpec] 2015-08-07 16:04:10,012 SolrJDAO.scala (line 26) SOLR URL = http://ubuntu-master:8983/solr/jobs
total time for 1000000 is 8502
it is 125000 jobs/second

3. Try to Build the Driver myself


Tips
Add the open file limit on ubuntu and mac os
sudo sh -c "ulimit -n 65535 && exec su carl"
sudo ulimit -n 10000




References:
http://sillycat.iyunv.com/blog/2233708

http://www.cnblogs.com/wgp13x/p/3742653.html
http://blog.csdn.net/john_f_lau/article/details/8780013

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-314999-1-1.html 上篇帖子: 开源搜索引擎 solr 实践 (二)配置篇 下篇帖子: solr in action翻译-第三章Solr的关键概念 3.3
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表