使用Spark连接ElasticSearch 并保存数据的具体步骤

1.在ES 上建好索引

即使不提前建立索引,ES也是可以将数据保存进去的。这种情况,ES会根据第一条要插入的数据进行推断,但是ES的这种推断往往不够准确。
比如:要区分字段要不要进行索引,字段要不要进行分词,如果分词选用哪个分词器等等。
建立索引语句(包含Mapping)

PUT gmall_coupon_alert
{
   "mappings": {
     "_doc":{
       "properties":{
         "mid":{
           "type":"keyword"
         },
         "uids":{
           "type":"keyword"
         },
         "itemIds":{
           "type":"keyword"
         },
         "events":{
           "type":"keyword"
         },
        "ts":{
           "type":"date"
         } 
       }
     }
   }
}

2.保存ES

2.1 pom.xml 增加依赖

<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</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>

2.2保存ES的工具类

import java.util
import java.util.Objects

import io.searchbox.client.{JestClient, JestClientFactory}
import io.searchbox.client.config.HttpClientConfig
import io.searchbox.core.{Bulk, BulkResult, Index}
import collection.JavaConversions._

object MyEsUtil {
  private val ES_HOST = "http://hadoop100"
  private val ES_HTTP_PORT = 9200
  private var factory:JestClientFactory = null //JestClientFactory是Elasticsearch HTTP Rest接口的 client
  /**
    * 获取客户端
    *
    * @return jestclient
    */
  def getClient: JestClient = {
    if (factory == null) build()
    factory.getObject
  }

  /**
    * 关闭客户端
    */
  def close(client: JestClient): Unit = {
    if ( client!=null) try
      client.shutdownClient()
    catch {
      case e: Exception =>
        e.printStackTrace()
    }
  }

  /**
    * 建立连接,将参数传入factory设置完成以后 factory就可以在获取客户端操作里传递给getClient了
    */
  private def build(): Unit = {
    factory = new JestClientFactory
   
    factory
    .setHttpClientConfig(new HttpClientConfig
    						.Builder(ES_HOST + ":" + ES_HTTP_PORT) //主机地址+端口
    						.multiThreaded(true)//是否支持多线程
         					.maxTotalConnection(20) //连接总数
      						.connTimeout(10000)//连接超时时间设置
      						.readTimeout(10000)//读取超时时间设置
      						.build)
  }
  
 // 创建批量插入数据到ES的通用方法,作为一个公共方法可以被调用
  def  insertBulk(indexName:String,docList:List[(String,Any)]): Unit ={//传递参数为索引名字,[(String--id,Any--数据)]
   
    if(docList.size>0){
      val jest: JestClient = getClient
      val bulkBuilder = new Bulk
     					 .Builder()
    					 .defaultIndex(indexName)
     					 .defaultType("_doc")
     //创建应用于批处理的index
      for ((id,doc) <- docList ) {
        val indexBuilder = new Index.Builder(doc)
        if(id!=null){
          indexBuilder.id(id)
        }
        val index: Index = indexBuilder.build()
        bulkBuilder.addAction(index)
      }
      val bulk: Bulk = bulkBuilder.build()
      var items: util.List[BulkResult#BulkResultItem] = null
      try {
        items = jest.execute(bulkBuilder.build()).getItems

      } catch {
        case ex: Exception => println(ex.toString)
      } finally {
        close(jest)
        println("保存" + items.size() + "条数据")
        for (item <- items) {
          if (item.error != null && item.error.nonEmpty) {
            println(item.error)
            println(item.errorReason)
          }
        }

      }
    }
  }


//执行入口方法
  def main(args: Array[String]): Unit = {
    val jest: JestClient = getClient
    //构造插入动作
     val index: Index = new Index
     					.Builder(Stud("zs","zhangsan"))
     					.index("gmall2020_stud")//索引名称
     					.`type`("_doc")//类型
     					.id("stu3")//id号
     					.build() 
    jest.execute(index)//此处需要传入一个action操作传入的action可以是写(index)读、改、查等操作
close(jest)//关闭
  }
  case class Stud(name:String ,nickname:String){
  }

总结一下:
1.先在es上建立索引;
2.添加pom文件;
3.使用工具类(主要做的是选择主机名、端口号、JestClientFactory)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值