用scala向es寫數據

package com.zhengkw.gmall.realtime.util

import io.searchbox.client.JestClientFactory
import io.searchbox.client.config.HttpClientConfig
import io.searchbox.core.Index

/**
 * @ClassName:ESUtil
 * @author: zhengkw
 * @description:
 * @date: 20/06/03下午 2:30
 * @version:1.0
 * @since: jdk 1.8 scala 2.11.8
 */
object ESDemo {
  def main(args: Array[String]): Unit = {
    val factory = new JestClientFactory
    val serverUrl = "http://hadoop102:9200"
    val conf = new HttpClientConfig.Builder(serverUrl)
      .maxTotalConnection(100) // 允許的最多客戶端的個數
      .connTimeout(10000) // 連接es的超時時間
      .readTimeout(10000) // 讀取數據的超時時間
      .multiThreaded(true) //是否多線程
      .build()
    //3.配置conf
    factory.setHttpClientConfig(conf)

    //2.獲得客戶端對象
    val client = factory.getObject

    /*val source =
      """
        |{
        |   "name":"KJ",
        |   "age":12
        |}
        |""".stripMargin*/

    //fuc2
    val source = User("java", 13)
    //向es寫數據
    val action = new Index.Builder(source)
      .index("javademo")
      .`type`("test")
      .id("2")
      .build()
    client.execute(action)

  }
}

case class User(name: String, age: Int)

注意

scala中type是關鍵字,所以在demo中需要用到反引號來避免歧義!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章