本文整理汇总了Scala中com.sksamuel.elastic4s.ElasticsearchClientUri类的典型用法代码示例。如果您正苦于以下问题:Scala ElasticsearchClientUri类的具体用法?Scala ElasticsearchClientUri怎么用?Scala ElasticsearchClientUri使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElasticsearchClientUri类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: getClient
//设置package包名称以及导入依赖的类
package au.csiro.data61.magda.search.elasticsearch
import akka.actor.Scheduler
import akka.event.LoggingAdapter
import au.csiro.data61.magda.AppConfig
import au.csiro.data61.magda.util.ErrorHandling.retry
import com.sksamuel.elastic4s.{ TcpClient, ElasticsearchClientUri }
import org.elasticsearch.common.settings.Settings
import scala.concurrent.{ ExecutionContext, Future }
import scala.concurrent.duration._
trait ClientProvider {
def getClient(implicit scheduler: Scheduler, logger: LoggingAdapter, ec: ExecutionContext): Future[TcpClient]
}
class DefaultClientProvider extends ClientProvider {
private var clientFuture: Option[Future[TcpClient]] = None
override def getClient(implicit scheduler: Scheduler, logger: LoggingAdapter, ec: ExecutionContext): Future[TcpClient] = {
val outerFuture = clientFuture match {
case Some(future) => future
case None =>
val future = retry(() => Future {
val uri = ElasticsearchClientUri(AppConfig.conf().getString("elasticSearch.serverUrl"))
val settings = Settings.builder().put("cluster.name", "myesdb").build()
TcpClient.transport(settings, uri)
}, 10 seconds, 10, onRetry(logger))
.map { client =>
logger.info("Successfully connected to elasticsearch client")
client
}
clientFuture = Some(future)
future
}
outerFuture
}
private def onRetry(logger: LoggingAdapter)(retriesLeft: Int, error: Throwable) = logger.error("Failed to make initial contact with ES server, {} retries left", retriesLeft, error)
}
示例2: SpecificationConstants
//设置package包名称以及导入依赖的类
package com.clemble.query
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.mappings.FieldType.{IntegerType, StringType}
import com.sksamuel.elastic4s.{ElasticsearchClientUri, ElasticClient}
import reactivemongo.api.MongoDriver
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
case object SpecificationConstants {
val db = Await.result(MongoDriver().connection(List("localhost:27017")).database("test"), 1 minute)
val client: ElasticClient = {
val uri = ElasticsearchClientUri("localhost", 9300)
val client = ElasticClient.transport(uri)
initClient(client)
client
}
private def initClient(client: ElasticClient) = {
val needToRemove = client.execute(indexExists("test")).map(_.isExists()).await
if (needToRemove) {
require(client.execute(deleteIndex("test")).await().isAcknowledged())
}
val nameMapping = mapping("employee").fields(
field("name", StringType).index("not_analyzed"),
field("salary", IntegerType)
)
val createCommand = create index "test" mappings (nameMapping)
val createResponse = client.execute(createCommand).await
createResponse.isAcknowledged()
}
}
示例3: ApplicationModule
//设置package包名称以及导入依赖的类
package com.pharmpress.dmdbrowser
import com.google.inject.AbstractModule
import com.pharmpress.elasticsearch.{Elastic4sServiceImpl, IElasticSearch}
import com.sksamuel.elastic4s.{ElasticClient, ElasticsearchClientUri}
import org.elasticsearch.common.settings.{ImmutableSettings, Settings}
import play.api.{Configuration, Environment, Logger}
class ApplicationModule(
environment: Environment,
configuration: Configuration
) extends AbstractModule {
def configure: Unit = {
val conf = configuration.underlying
val rpsConfigUri = conf.getString("es.rps.client.uri")
val rpsConfigCluster = conf.getString("es.rps.cluster.name")
bind(classOf[IElasticSearch]) toInstance new Elastic4sServiceImpl(() => {
Logger.info(s"Connecting to Elasticsearch at $rpsConfigUri with cluster $rpsConfigCluster")
ElasticClient.remote(
ImmutableSettings.settingsBuilder()
.classLoader(classOf[Settings].getClassLoader)
.put("cluster.name", rpsConfigCluster)
.build(),
ElasticsearchClientUri(rpsConfigUri)
)
})
}
}
示例4: LogHandler
//设置package包名称以及导入依赖的类
package actors.stateless
import akka.actor.Actor
import akka.event.Logging._
import com.sksamuel.elastic4s.{ElasticClient, ElasticsearchClientUri}
class LogHandler extends Actor{
val uri = ElasticsearchClientUri("elasticsearch://127.0.0.1:9300")
lazy val esClient = ElasticClient.transport(uri)
def receive = {
case InitializeLogger(_) =>
sender() ! LoggerInitialized
case [email protected] Error(cause, logSource, logClass, message) =>
println(s"DEBUG: [$logSource - $logClass] $message ${e.mdc}")
persistElasticsearch(e)
case [email protected] Warning(logSource, logClass, message) =>
println(s"DEBUG: [$logSource - $logClass] $message ${w.mdc}")
persistElasticsearch(w)
case [email protected] Info(logSource, logClass, message) =>
println(s"DEBUG: [$logSource - $logClass] $message ${i.mdc}")
persistElasticsearch(i)
case [email protected] Debug(logSource, logClass, message) =>
println(s"DEBUG: [$logSource - $logClass] $message ${d.mdc}")
persistElasticsearch(d)
}
def persistElasticsearch(logEvent: LogEvent): Unit = {
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.jackson.ElasticJackson
import ElasticJackson.Implicits._
import scala.concurrent.ExecutionContext.Implicits.global
esClient.execute {
index into "actor" / "Log" source logEvent id logEvent.timestamp
}.map { t =>
println(s"Persisted to Elastic: $t")
}.onFailure{
case t : Throwable => println(t, "Persisted to Elastic Exception: $t")
}
}
}
示例5: dropNulls
//设置package包名称以及导入依赖的类
package ch.epfl.telegram
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.{ElasticsearchClientUri, TcpClient}
import scala.concurrent.ExecutionContext.Implicits.global
package object models {
private[models] val es = {
import Config.elasticsearch.{clusterName, host, port}
TcpClient.transport(
ElasticsearchClientUri(s"elasticsearch://$host:$port?cluster.name=$clusterName")
)
}
List(Reaction.feedbackIndex, Reaction.surveyIndex, EPFLUser.epflUserIndex)
.foreach { indexType =>
println(indexType)
es.execute {
createIndex(indexType.index)
}
.map(println)
}
import com.sksamuel.elastic4s.{Hit, HitReader, Indexable}
import io.circe.jawn.decode
import io.circe.{Decoder, Encoder, Json, Printer}
import scala.annotation.implicitNotFound
object dropNulls {
@implicitNotFound(
"No Decoder for type ${T} found. Use 'import io.circe.generic.auto._' or provide an implicit Decoder instance ")
implicit def hitReaderWithCirce[T](implicit decoder: Decoder[T]): HitReader[T] = new HitReader[T] {
override def read(hit: Hit): Either[Throwable, T] = decode[T](hit.sourceAsString)
}
@implicitNotFound(
"No Encoder for type ${T} found. Use 'import io.circe.generic.auto._' or provide an implicit Encoder instance ")
implicit def indexableWithCirce[T](
implicit encoder: Encoder[T],
printer: Json => String = Printer.noSpaces.copy(dropNullKeys = true).pretty): Indexable[T] = new Indexable[T] {
override def json(t: T): String = printer(encoder(t))
}
}
}