Scala Api 操作 Elasticsearch数据库

1、操作前先导Maven包,注意版本一定要一致,新旧版本不兼容

        <!--es 相关依赖开始 es客户端的版本必须和服务器版本一致-->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>commons-compiler</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!-- es 相关依赖结束 -->

2、单条数据插入,使用 json 字符串作为数据源

package com.zyj.gmall.common

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

object ESUtil {

  // 端口号,服务器如果没有配置,默认是9200
  val esUrl = "http://hadoop103:9200"

  def main(args: Array[String]): Unit = {
    // 向es写数据

    //1. 先有es的客户端
    //1.1 创建一个客户端工厂
    val factory = new JestClientFactory
    val conf = new HttpClientConfig.Builder(esUrl)
      .maxTotalConnection(100) // 最多同时可以有100个到es的连接 一般是分区数的1.5倍
      .connTimeout(10 * 1000) // 连接到es的超时时间
      .readTimeout(10 * 1000) // 读取数据的最大超时时间
      .multiThreaded(true) // 是否允许多线程
      .build()
    factory.setHttpClientConfig(conf)
    //1.2 从工厂获取一个客户端
    val client = factory.getObject

    //2. es需要的数据(json,样例类)
    val data =
      """
        |{
        |  "name": "zs",
        |  "age": 20
        |}
        |""".stripMargin

    //3. 写入(单次,批次)
    val index = new Index.Builder(data)
      .index("user")
      .`type`("_doc")
//      .id("1") // 可选 如果没有设置 id自动生成
      .build()
    client.execute(index)

    //4. 关闭客户端(其实是把客户端还给工厂)
    client.shutdownClient() // 虽然过时了,但比较稳定

  }
}

3、单条数据插入,使用样例类作为数据源

package com.zyj.gmall.common

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

object ESUtil {

  // 端口号,服务器如果没有配置,默认是9200
  val esUrl = "http://hadoop103:9200"

  def main(args: Array[String]): Unit = {
    // 向es写数据

    //1. 先有es的客户端
    //1.1 创建一个客户端工厂
    val factory = new JestClientFactory
    val conf = new HttpClientConfig.Builder(esUrl)
      .maxTotalConnection(100) // 最多同时可以有100个到es的连接 一般是分区数的1.5倍
      .connTimeout(10 * 1000) // 连接到es的超时时间
      .readTimeout(10 * 1000) // 读取数据的最大超时时间
      .multiThreaded(true) // 是否允许多线程
      .build()
    factory.setHttpClientConfig(conf)
    //1.2 从工厂获取一个客户端
    val client = factory.getObject

    //2. es需要的数据(json,样例类)
    val data = User(30, "ww")

    //3. 写入(单次,批次)
    val index = new Index.Builder(data)
      .index("user")
      .`type`("_doc")
      //      .id("1") // 可选 如果没有设置 id自动生成
      .build()
    client.execute(index)

    //4. 关闭客户端(其实是把客户端还给工厂)
    client.shutdownClient() // 虽然过时了,但比较稳定

  }
}

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

 4、单条数据插入,将对数据库的连接进行封装

package com.zyj.gmall.common

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

object ESUtil {

  // 端口号,服务器如果没有配置,默认是9200
  val esUrl = "http://hadoop103:9200"

  // 创建一个客户端工厂
  val factory = new JestClientFactory
  val conf = new HttpClientConfig.Builder(esUrl)
    .maxTotalConnection(100) // 最多同时可以有100个到es的连接 一般是分区数的1.5倍
    .connTimeout(10 * 1000) // 连接到es的超时时间
    .readTimeout(10 * 1000) // 读取数据的最大超时时间
    .multiThreaded(true) // 是否允许多线程
    .build()
  factory.setHttpClientConfig(conf)

  /*
  * 向es中插入单条数据
  * */
  def insertSingle(index: String, source: Object, id: String = null) = {
    // 从工厂获取一个客户端
    val client = factory.getObject
    // 写入(单次,批次)
    val action = new Index.Builder(source)
      .index(index)
      .`type`("_doc")
      .id(id) // id 如果是null,相当于没有传
      .build()
    client.execute(action)
    // 关闭客户端(其实是把客户端还给工厂)
    client.shutdownClient()
  }

  def main(args: Array[String]): Unit = {

    val data = User(30, "xx")

    insertSingle("user", data)

  }
}

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

5、批量插入,样例类作为数据源,并封装连接

package com.zyj.gmall.common

import io.searchbox.client.JestClientFactory
import io.searchbox.client.config.HttpClientConfig
import io.searchbox.core.{Bulk, Index}

object ESUtil {

  // 端口号,服务器如果没有配置,默认是9200
  val esUrl = "http://hadoop103:9200"

  // 创建一个客户端工厂
  val factory = new JestClientFactory
  val conf = new HttpClientConfig.Builder(esUrl)
    .maxTotalConnection(100) // 最多同时可以有100个到es的连接 一般是分区数的1.5倍
    .connTimeout(10 * 1000) // 连接到es的超时时间
    .readTimeout(10 * 1000) // 读取数据的最大超时时间
    .multiThreaded(true) // 是否允许多线程
    .build()
  factory.setHttpClientConfig(conf)

  /*
  * 向es批量插入数据
  * */
  def insertBulk(index: String, sources: Iterator[Any]) = {
    val client = factory.getObject

    val bulk = new Bulk.Builder()
      .defaultIndex(index)
      .defaultType("_doc")

    sources.foreach {
      case (id: String, data) =>
        val action = new Index.Builder(data).id(id).build()
        bulk.addAction(action)
      case data =>
        val action = new Index.Builder(data).build()
        bulk.addAction(action)
    }

    client.execute(bulk.build())
    client.shutdownClient()
  }

  def main(args: Array[String]): Unit = {

    val list = User(1, "aa") :: User(2, "bb") :: User(3, "cc") :: Nil
    insertBulk("user", list.toIterator)

    val list2 = ("100", User(1, "a")) :: ("200", User(1, "b")) :: ("300", User(3, "c")) :: Nil
    insertBulk("user", list2.toIterator)

  }
}

case class User(age: Int, name: String)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值